Integrate11 / 12
SDKs and API
Your game talks to Stacked through an SDK, or through the HTTP API the SDKs wrap. This page lists the SDKs, how to authenticate, and every endpoint a game calls.
SDKs
| SDK | Runs in | Install |
|---|---|---|
| Node.js | Your game backend | npm install @pixels-online/pixels-analytics-node-sdk |
| Go | Your game backend | go get gitlab.com/pixels-online-oss/pixels-analytics-go-sdk |
| JavaScript | A web game client | npm install @pixels-online/pixels-client-js-sdk |
| Unity and .NET | A Unity or C# game client | dotnet add package PixelsOnline.Client.SDK |
What each side does:
| Task | Server SDKs (Node.js, Go) | Client SDKs (JavaScript, Unity) |
|---|---|---|
| Send events | Yes | No |
| Sign player tokens | signJwt / SignJwt | No |
| Fetch a player's offers | fetchPlayerCampaigns / FetchPlayerCampaigns | refreshOffersAndPlayer / RefreshOffersAndPlayerAsync |
| Claim an offer | Go: ClaimRewards. Node.js: over HTTP | claimReward / ClaimRewardAsync |
| Live ops | initializeLiveOpsEvents, claimMilestoneRewards | initializeLiveOpsEvents, claimMilestoneRewards |
| Leaderboards | Over HTTP | initLeaderboards, listLeaderboards, getLeaderboard, joinLeaderboard, claimableLeaderboardRewards, claimLeaderboardReward |
| Realtime updates | No | client.events.on(...) |
The Go and Unity SDKs name the same methods in their own style (InitializeLiveOpsEvents, ClaimMilestoneRewardsAsync, ListLeaderboardsAsync).
Environments
Every game has two environments with separate data, credentials and webhooks.
| Test | Live | |
|---|---|---|
| Use it for | Building and trying things out | Real players |
| API | https://api.sandbox.stacked.xyz | https://api.stacked.xyz |
| SDK option | env: 'test' | env: 'live' |
Authentication
There are two credentials, for the two places your game runs.
API key: your backend
Send your game's API key and client id as headers. Both are on the Credentials page of the dashboard, one pair for Test and one for Live.
curl -X POST https://api.sandbox.stacked.xyz/v1/player/campaigns/refresh \
-H "x-api-key: your_api_key" \
-H "x-client-id: your_client_id" \
-H "Content-Type: application/json" \
-d '{ "playerId": "player-1" }'The API key stays on your server. It can read and claim for any player of your game.
Player token: your game client
A game client never holds the API key. Your backend signs a short-lived token for one player, and the client sends it as Authorization: Bearer <token>.
// On your backend, with the Node.js SDK
const token = await analytics.signJwt({ playerId: 'player-1' });The client SDKs ask for a token through the tokenProvider you give them, and ask again shortly before it expires. A token lasts one hour unless you set jwtExpirySeconds.
Without a server SDK, sign it yourself. It is a JWT signed with HS256, keyed with your API key:
| Part | Field | Value |
|---|---|---|
| Header | alg, typ | HS256, JWT |
| Header | kid | Your client id |
| Payload | iss | Your game id (gameId in the answer of POST /v1/player/campaigns/refresh) |
| Payload | sub | The player's id |
| Payload | exp | When it expires, in seconds |
| Payload | iat, nbf | When it was issued, and the earliest it is valid, in seconds |
Endpoints
Paths are relative to the API of the environment. Key means the API key headers, Player a player token. Bodies and answers are JSON. Send {} when a POST needs no fields.
Where a path exists under both /v1 and /v2, use /v2: the two differ only in how an offer's conditions are laid out, and /v2 is the current layout.
Offers
| Endpoint | Auth | What it does |
|---|---|---|
POST /v1/player/campaigns/refresh | Key | Looks for new offers for playerId, updates the ones they have, and returns them all |
POST /v2/client/player/campaigns | Player | The same, for the signed-in player |
POST /v2/client/offers/finished | Player | The player's past offers by status (claimed, expired or declined), in pages |
POST /v1/player/offer/status | Key | Sets the status of one offer instance, for example to reopen an expired one. Never to claimed |
POST /v1/reward/sdk/decline | Key | Declines an offer for playerId |
POST /v1/client/offer/decline | Player | Declines an offer, when your game's settings allow players to |
Send viewingCampaigns: true with a fetch when the player is looking at the offers: it marks the surfaced ones as viewed. The answer holds offers, offerGroups and the player's snapshot. An offer carries its instanceId, status, rewards and progress.
Claim rewards
| Endpoint | Auth |
|---|---|
POST /v1/reward/sdk/claim | Key |
POST /v1/client/reward/claim | Player |
| Field | Meaning |
|---|---|
kind | offer, milestone for a live ops milestone, or leaderboard for a leaderboard reward |
instanceId | What to claim: an offer's instanceId, a milestone claim id, or a leaderboard reward's id |
instanceIds | Up to 25 of them, to claim in one call, in place of instanceId |
playerId | Key only: the player to claim for |
targetId | Player only: claim for a linked entity |
extra | Optional. Up to 10 string values that are passed to your reward webhook unchanged |
skipWebhook | Key only. Sends no reward webhook: grant from the answer's rewards instead |
curl -X POST https://api.sandbox.stacked.xyz/v1/reward/sdk/claim \
-H "x-api-key: your_api_key" \
-H "x-client-id: your_client_id" \
-H "Content-Type: application/json" \
-d '{ "kind": "offer", "playerId": "player-1", "instanceId": "68a1f3c2e4b0a1d2c3f4a5b6" }'A claim with one instanceId answers { "message": "success" } (with rewards on the Key endpoint), or an error. A claim with instanceIds always answers 200 with one result per id, each with claimed and, when it failed, error.
A reward is claimed at most once. Claiming it again answers player-offer-not-found (for a leaderboard reward, already-claimed), not a second success.
Live ops
| Endpoint | Auth | What it does |
|---|---|---|
POST /v2/client/liveops/initialize | Player | The events live now, with the player's progress, daily rewards and event offers |
POST /v1/player/liveops/initialize | Key | The same, for playerId |
GET /v1/sdk/liveops/events | Key | The events live now, without a player |
POST /v1/player/liveops/group/upsert | Key | Creates or changes a group of players for a grouped track |
POST /v1/player/liveops/group/delete | Key | Removes a group |
Leaderboards
| Endpoint | Auth | What it does |
|---|---|---|
POST /v2/client/leaderboards/init | Player | The decimal places each currency a leaderboard can pay is rounded down to, and the player's leaderboard rewards to claim |
POST /v2/client/leaderboards | Player | The game's leaderboards, with status, prizes and first ranks |
POST /v2/client/leaderboard | Player | One page of standings, and the player's own row |
POST /v2/client/leaderboard/join | Player | Enters the player in a leaderboard that needs joining |
GET /v1/sdk/leaderboards/init | Key | The same decimal places, for a backend. With playerId, that player's leaderboard rewards to claim too |
GET /v1/sdk/leaderboards | Key | The game's leaderboards |
POST /v1/player/leaderboard | Key | One page of standings, with playerId's row when given |
POST /v1/player/leaderboard/join | Key | Enters playerId |
The fields are on the Leaderboards page.
Shop
| Endpoint | Auth | What it does |
|---|---|---|
GET /v1/client/shop/items | Player | The items your game sells in its Stacked shop |
POST /v1/client/shop/purchase | Player | Buys quantity of shopItemId with currencyId |
The shop is switched on per game.
An item can be limited to some players with conditions, the same ones offers use. The list only has the items the player meets the conditions of, and buying any other item is refused with shop-item-conditions-not-met.
Realtime events
A game client can hold one connection open and be told when something changes, instead of asking again.
GET/v2/sse/connect
It is a server-sent event stream, authenticated with a player token. The client SDKs open it, keep it alive and reconnect for you.
| Event | Sent when | Data |
|---|---|---|
offer_surfaced | An offer surfaces outside a fetch: one surfaced by a context your game called | The offer |
offer_completed | Such an offer becomes complete or claimable | The offer |
player_delta | The player's offer progress or snapshot changes | The changes. The SDKs apply them to what they hold |
milestone_progress | The player's points change on an individual or community milestone track, or they claim on one | liveOpsEventId, track, trackId, and the track's whole progress |
leaderboard_entry_updated | A player's entry on a leaderboard changes. Today that is when its rewards become claimable: the leaderboard's rewards were generated and the player won some | The entry as it is now: id, leaderboardId, playerId, score, rank, and its rewards with rewardsStatus. The SDKs add it to the player's claimable rewards |
import { OfferEvent } from '@pixels-online/pixels-client-js-sdk';
client.events.on(OfferEvent.MILESTONE_PROGRESS, ({ liveOpsEventId, track, progress }) =>
redrawTrack(liveOpsEventId, track, progress),
);An event only reaches the clients that are connected at that moment: one that connects later never gets it. Use events to update what is on screen sooner, and the fetches for what is true: they always answer the current state.
Errors
An error answers with a status and one code:
{ "error": "invalid-api-key" }A request with a missing or malformed field answers 400 with "error": "Validation failed" and details, a list of path and message.
| Status | Code | Meaning |
|---|---|---|
| 401 | invalid-api-key | The API key is unknown, or the client id is not the key's |
| 400 | jwt-expired, jwt-invalid | The player token is no good. Sign a new one |
| 403 | cannot-impersonate-target-player | targetId is not an entity linked to the player |
| 400 | player-offer-not-found | No such offer instance for this player, or it is already claimed, expired or declined |
| 400 | completion-conditions-not-met, claimable-conditions-not-met | The offer cannot be claimed yet |
| 400 | claim-failed | The claim could not be made. The usual cause: webhooks are not set up for this environment yet |
| 400 | nothing-to-claim | A milestone claim with no reward waiting |
| 400 | leaderboard-reward-not-found, already-claimed, player-blacklisted | A leaderboard reward that is not this player's, was claimed before, or belongs to a blacklisted player |
| 404 | leaderboard-not-found | No such leaderboard for this game |
| 400 | leaderboard-not-started, leaderboard-ended | A join outside the leaderboard's dates |
| 429 | Too many requests. The body is the text too-many-requests |
Limits
| Limit | Value |
|---|---|
| Requests | 10 a second and 50 in 30 seconds, per IP address |
| Request body | 100 KB |
| Ids in one claim | 25 |
| A player id | 250 characters |
A backend that calls from one address shares one allowance across all its players, so let game clients fetch their own offers with a player token.