Integrate10 / 12
Reward webhooks
Stacked decides what a player has earned. Your game decides how to hand it over. When a player claims a reward, Stacked sends a signed webhook to your backend that says who to give what, and your backend grants it.
Set up an endpoint
- Write an HTTPS handler in your game backend (see Verify and grant).
- Open Webhooks in the dashboard, on Test or Live, and add the handler's URL.
- Subscribe it to
user.gain_rewards. - Copy the endpoint's signing secret (it starts with
whsec_) into your backend's configuration. - Send a test event from the page and check the delivery.
Test and Live have separate endpoints and secrets. An AI agent can do all of this with the webhook tools.
Event types
| Event type | Sent when |
|---|---|
user.gain_rewards | A player is granted rewards: an offer claim, a shop purchase, a live ops milestone claim or a leaderboard reward claim. Subscribe to this one. |
claim.reward | An offer claim only. Legacy: same claims and same payload as user.gain_rewards. |
claim.offer | An offer claim only. Legacy: same payload. |
Subscribe to one event type. An offer claim sends all three. An endpoint subscribed to all of them is told to grant the same reward three times.
Payload
Every reward webhook has the same JSON body.
| Field | Type | Meaning |
|---|---|---|
source | string | What paid out: offer, shop, milestone or leaderboard |
campaignId | string | The id of the offer, shop item, milestone track or leaderboard |
instanceId | string | What was claimed: the player's offer, the shop purchase, the milestone claim or the leaderboard reward (the id it was claimed with) |
campaignKind | string, optional | offer, referral, shop, milestone or leaderboard |
entityKind | string, optional | The player's entity kind, when it has one |
playerId | string | Your game's id for the player, as your game sent it |
extra | object, optional | The string values your game sent as extra with the claim, unchanged |
rank, score | number, optional | Leaderboard payouts only: the player's final rank (1 is first) and score |
name | string | The name of what was claimed |
description | string, optional | Its description |
rewards | array | What to grant. Each entry is a reward: kind, rewardId, amount, name |
dedupeId | string | Unique per message. It differs between the three event types of one claim |
{
"source": "offer",
"campaignId": "6844c430e3db6fb7612cc126",
"campaignKind": "offer",
"instanceId": "68a1f3c2e4b0a1d2c3f4a5b6",
"playerId": "playerId_from_your_game",
"name": "Welcome back",
"description": "Sign in three days in a row",
"rewards": [
{ "kind": "coins", "rewardId": "currency_gold", "amount": 5000, "name": "Gold Coins" },
{ "kind": "item", "rewardId": "item_popberry", "amount": 12, "name": "Popberry Fruit" },
{ "kind": "exp", "rewardId": "skill_ranger", "amount": 69, "name": "Ranger EXP" }
],
"dedupeId": "68a1f3c2e4b0a1d2c3f4a5b7"
}Two reward kinds never need your game to act: Stacked credits loyalty_currency rewards to the player's Stacked balance itself and leaves them out of rewards, and it adds milestone_points to the live ops track itself (they are listed so your game can show them).
extrafrom a game client is the player's word. When the claim came from your game client,extraholds whatever that client sent. Check it before you act on it.
Verify and grant
Each delivery is signed with the endpoint's secret, following the Standard Webhooks scheme. The svix libraries verify it in most languages.
| Header | Value |
|---|---|
svix-id | The message id. The same on every retry of one message |
svix-timestamp | When this attempt was sent, in seconds |
svix-signature | v1, and the signature. More than one, separated by spaces, while a secret is being rotated |
The same three values are also sent as webhook-id, webhook-timestamp and webhook-signature.
import express from 'express';
import { Webhook } from 'svix';
const app = express();
const webhook = new Webhook(process.env.STACKED_WEBHOOK_SECRET); // whsec_...
// The signature covers the raw body, so read it before any JSON parsing.
app.post('/stacked/rewards', express.raw({ type: 'application/json' }), async (req, res) => {
let reward;
try {
reward = webhook.verify(req.body, {
'svix-id': req.header('svix-id'),
'svix-timestamp': req.header('svix-timestamp'),
'svix-signature': req.header('svix-signature'),
});
} catch {
return res.status(400).end();
}
// Grant once per message, however many times it is delivered.
await grantOnce(reward.dedupeId, () => giveRewards(reward.playerId, reward.rewards));
res.status(204).end();
});grantOnce and giveRewards are yours: record the key and grant in one transaction, and do nothing when the key is already there.
To verify without a library: the signed content is <svix-id>.<svix-timestamp>.<raw body>, the key is the secret without whsec_, decoded from base64, and the signature is the base64 HMAC-SHA256 of the content. Accept the request if any signature in the header matches, and reject a timestamp more than five minutes from your clock.
Grant each reward once
A webhook can reach you more than once: a retry after a slow answer, or a resend from the dashboard. Make granting idempotent.
- Key on
dedupeId. Record it when you grant, and do nothing when you have seen it before. A retry or a resend of a message carries the samededupeIdand the samesvix-id. - Subscribe to one event type. The three messages of one offer claim have three different
dedupeIds. sourceandinstanceIdsay what was claimed, which is useful in your own records. They identify one grant in every case but one: an offer a player can claim several times keeps the sameinstanceIdfor each claim.
Delivery and retries
Answer with a 2xx status within 30 seconds. Anything else, a redirect included, is a failed attempt and is retried:
| Attempt | Sent |
|---|---|
| 1 | At once |
| 2 | 5 seconds later |
| 3 | 5 minutes later |
| 4 | 30 minutes later |
| 5 | 2 hours later |
| 6 | 5 hours later |
| 7 and 8 | 10 hours later, each |
After the eighth attempt the message is failed. You can send it again from the dashboard: one message, or every message that failed since a time you choose.
- Stop the retries of one message by answering with the header
webhook-delivery: abort-message. Answerwebhook-delivery: disableto disable the endpoint. - An endpoint that only fails for five and a half days is disabled. Enable it again on the Webhooks page.
- Rotating a secret keeps the old one valid for 24 hours. Deliveries carry a signature for both, so switch at any time in that window.
- Messages and their delivery logs are kept for 30 days.
Limits
| Limit | Value |
|---|---|
| Endpoints per game | 50 |
| Extra request headers per endpoint | 20 |
| URL | https://, on a public address |
| Response time | 30 seconds |