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

SDKRuns inInstall
Node.jsYour game backendnpm install @pixels-online/pixels-analytics-node-sdk
GoYour game backendgo get gitlab.com/pixels-online-oss/pixels-analytics-go-sdk
JavaScriptA web game clientnpm install @pixels-online/pixels-client-js-sdk
Unity and .NETA Unity or C# game clientdotnet add package PixelsOnline.Client.SDK

What each side does:

TaskServer SDKs (Node.js, Go)Client SDKs (JavaScript, Unity)
Send eventsYesNo
Sign player tokenssignJwt / SignJwtNo
Fetch a player's offersfetchPlayerCampaigns / FetchPlayerCampaignsrefreshOffersAndPlayer / RefreshOffersAndPlayerAsync
Claim an offerGo: ClaimRewards. Node.js: over HTTPclaimReward / ClaimRewardAsync
Live opsinitializeLiveOpsEvents, claimMilestoneRewardsinitializeLiveOpsEvents, claimMilestoneRewards
LeaderboardsOver HTTPinitLeaderboards, listLeaderboards, getLeaderboard, joinLeaderboard, claimableLeaderboardRewards, claimLeaderboardReward
Realtime updatesNoclient.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.

TestLive
Use it forBuilding and trying things outReal players
APIhttps://api.sandbox.stacked.xyzhttps://api.stacked.xyz
SDK optionenv: '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.

Shell
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>.

TypeScript
// 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:

PartFieldValue
Headeralg, typHS256, JWT
HeaderkidYour client id
PayloadissYour game id (gameId in the answer of POST /v1/player/campaigns/refresh)
PayloadsubThe player's id
PayloadexpWhen it expires, in seconds
Payloadiat, nbfWhen 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

EndpointAuthWhat it does
POST /v1/player/campaigns/refreshKeyLooks for new offers for playerId, updates the ones they have, and returns them all
POST /v2/client/player/campaignsPlayerThe same, for the signed-in player
POST /v2/client/offers/finishedPlayerThe player's past offers by status (claimed, expired or declined), in pages
POST /v1/player/offer/statusKeySets the status of one offer instance, for example to reopen an expired one. Never to claimed
POST /v1/reward/sdk/declineKeyDeclines an offer for playerId
POST /v1/client/offer/declinePlayerDeclines 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

EndpointAuth
POST /v1/reward/sdk/claimKey
POST /v1/client/reward/claimPlayer
FieldMeaning
kindoffer, milestone for a live ops milestone, or leaderboard for a leaderboard reward
instanceIdWhat to claim: an offer's instanceId, a milestone claim id, or a leaderboard reward's id
instanceIdsUp to 25 of them, to claim in one call, in place of instanceId
playerIdKey only: the player to claim for
targetIdPlayer only: claim for a linked entity
extraOptional. Up to 10 string values that are passed to your reward webhook unchanged
skipWebhookKey only. Sends no reward webhook: grant from the answer's rewards instead
Shell
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

EndpointAuthWhat it does
POST /v2/client/liveops/initializePlayerThe events live now, with the player's progress, daily rewards and event offers
POST /v1/player/liveops/initializeKeyThe same, for playerId
GET /v1/sdk/liveops/eventsKeyThe events live now, without a player
POST /v1/player/liveops/group/upsertKeyCreates or changes a group of players for a grouped track
POST /v1/player/liveops/group/deleteKeyRemoves a group

Leaderboards

EndpointAuthWhat it does
POST /v2/client/leaderboards/initPlayerThe decimal places each currency a leaderboard can pay is rounded down to, and the player's leaderboard rewards to claim
POST /v2/client/leaderboardsPlayerThe game's leaderboards, with status, prizes and first ranks
POST /v2/client/leaderboardPlayerOne page of standings, and the player's own row
POST /v2/client/leaderboard/joinPlayerEnters the player in a leaderboard that needs joining
GET /v1/sdk/leaderboards/initKeyThe same decimal places, for a backend. With playerId, that player's leaderboard rewards to claim too
GET /v1/sdk/leaderboardsKeyThe game's leaderboards
POST /v1/player/leaderboardKeyOne page of standings, with playerId's row when given
POST /v1/player/leaderboard/joinKeyEnters playerId

The fields are on the Leaderboards page.

Shop

EndpointAuthWhat it does
GET /v1/client/shop/itemsPlayerThe items your game sells in its Stacked shop
POST /v1/client/shop/purchasePlayerBuys 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.

EventSent whenData
offer_surfacedAn offer surfaces outside a fetch: one surfaced by a context your game calledThe offer
offer_completedSuch an offer becomes complete or claimableThe offer
player_deltaThe player's offer progress or snapshot changesThe changes. The SDKs apply them to what they hold
milestone_progressThe player's points change on an individual or community milestone track, or they claim on oneliveOpsEventId, track, trackId, and the track's whole progress
leaderboard_entry_updatedA 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 someThe 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
TypeScript
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:

JSON
{ "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.

StatusCodeMeaning
401invalid-api-keyThe API key is unknown, or the client id is not the key's
400jwt-expired, jwt-invalidThe player token is no good. Sign a new one
403cannot-impersonate-target-playertargetId is not an entity linked to the player
400player-offer-not-foundNo such offer instance for this player, or it is already claimed, expired or declined
400completion-conditions-not-met, claimable-conditions-not-metThe offer cannot be claimed yet
400claim-failedThe claim could not be made. The usual cause: webhooks are not set up for this environment yet
400nothing-to-claimA milestone claim with no reward waiting
400leaderboard-reward-not-found, already-claimed, player-blacklistedA leaderboard reward that is not this player's, was claimed before, or belongs to a blacklisted player
404leaderboard-not-foundNo such leaderboard for this game
400leaderboard-not-started, leaderboard-endedA join outside the leaderboard's dates
429Too many requests. The body is the text too-many-requests

Limits

LimitValue
Requests10 a second and 50 in 30 seconds, per IP address
Request body100 KB
Ids in one claim25
A player id250 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.