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

  1. Write an HTTPS handler in your game backend (see Verify and grant).
  2. Open Webhooks in the dashboard, on Test or Live, and add the handler's URL.
  3. Subscribe it to user.gain_rewards.
  4. Copy the endpoint's signing secret (it starts with whsec_) into your backend's configuration.
  5. 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 typeSent when
user.gain_rewardsA 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.rewardAn offer claim only. Legacy: same claims and same payload as user.gain_rewards.
claim.offerAn 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.

FieldTypeMeaning
sourcestringWhat paid out: offer, shop, milestone or leaderboard
campaignIdstringThe id of the offer, shop item, milestone track or leaderboard
instanceIdstringWhat was claimed: the player's offer, the shop purchase, the milestone claim or the leaderboard reward (the id it was claimed with)
campaignKindstring, optionaloffer, referral, shop, milestone or leaderboard
entityKindstring, optionalThe player's entity kind, when it has one
playerIdstringYour game's id for the player, as your game sent it
extraobject, optionalThe string values your game sent as extra with the claim, unchanged
rank, scorenumber, optionalLeaderboard payouts only: the player's final rank (1 is first) and score
namestringThe name of what was claimed
descriptionstring, optionalIts description
rewardsarrayWhat to grant. Each entry is a reward: kind, rewardId, amount, name
dedupeIdstringUnique per message. It differs between the three event types of one claim
JSON
{
  "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).

extra from a game client is the player's word. When the claim came from your game client, extra holds 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.

HeaderValue
svix-idThe message id. The same on every retry of one message
svix-timestampWhen this attempt was sent, in seconds
svix-signaturev1, 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.

TypeScript
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 same dedupeId and the same svix-id.
  • Subscribe to one event type. The three messages of one offer claim have three different dedupeIds.
  • source and instanceId say 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 same instanceId for 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:

AttemptSent
1At once
25 seconds later
35 minutes later
430 minutes later
52 hours later
65 hours later
7 and 810 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. Answer webhook-delivery: disable to 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

LimitValue
Endpoints per game50
Extra request headers per endpoint20
URLhttps://, on a public address
Response time30 seconds