Skip to content
GameCoinDocs
Dashboard

Client API

The routes a game client calls with a publishable key and a player token, including the catalog, the wallet, purchases, checkouts, gift codes and e-mail sign-in.

On this page

The client API is the part of GameCoin that your game client calls directly, from a browser or from the game itself. It uses a publishable key, which is safe to ship in a client, and for everything about one player it uses a player token. It can read the catalog, read the player's wallet and inventory, spend, buy an item with a currency, consume an item, pay for a pack and sign a player in with an e-mail. It can never credit anything, with one exception: the player can redeem a gift code, which gives free coins and items from a code that your studio issued. All its routes live under /client.

In a browser you will usually not call these routes yourself: the browser SDK does. This page documents the routes, for the SDK, for game clients that are not browsers, and for debugging. The guide for a game without a backend and the one for a game with a backend show the two usual setups.

Setup for the examples

Every example has two tabs: curl, for the HTTP request, and Browser, for the same thing with the browser SDK. In curl you need your publishable key, and a player token for the routes that act on a player. In the Browser tab, gc is the client returned by GameCoin.init; playerToken is a token your backend got from Issue a player token. Without a playerToken, the SDK creates an anonymous player on its own: see Create an anonymous player.

LangageLanguage
export GAMECOIN_PUBLISHABLE_KEY="gc_pk_test_…"   # a publishable test key: safe in a browser
export GAMECOIN_PLAYER_TOKEN="gc_pt_…"           # a player token, issued by your server for ext:user-42

The player of these examples is the ext:user-42 of the server API pages, so that you find the same wallet and inventory.

Keys and tokens

HeaderValueNeeded on
X-GameCoin-KeyThe publishable key, gc_pk_test_… or gc_pk_live_…Every client route
AuthorizationBearer gc_pt_…, a player tokenThe routes that act on a player: /client/me/**

A player token lasts one hour and is tied to one player, one game and one environment. A secret key sent to a client route is refused with 403 FORBIDDEN, and a missing or invalid token with 401 UNAUTHENTICATED. When a request answers 401 and you hold a token, ask for a new one (your backend for a player it created, Get a new player token for an anonymous player) and send the same request again, with the same Idempotency-Key: if the first attempt did go through, you get its answer back instead of a second change.

A blocked player is refused on every client route with 403 PLAYER_BLOCKED.

CORS

Each game has a list of allowed origins (game settings in the dashboard, one origin per line, e.g. https://play.example.com). A client API request whose Origin is on the list is answered with that origin in Access-Control-Allow-Origin (and Vary: Origin). A request from another origin is refused with 403 FORBIDDEN (fieldErrors.Origin = ORIGIN_NOT_ALLOWED). With an empty list the test environment accepts any origin (*) and live accepts none. Requests without an Origin header (game engines, servers) are never affected. A preflight OPTIONS request is answered 204 without any authentication. The response lists the headers a browser may send (Authorization, Content-Type, X-GameCoin-Key, Idempotency-Key), and exposes Idempotent-Replayed and Retry-After to your code.

Bash
curl -i -X OPTIONS "https://gamecoin.apilow.com/api/v1/client/me/wallet/spend" \
  -H "Origin: https://example.com" \
  -H "Access-Control-Request-Method: POST" \
  -H "Access-Control-Request-Headers: authorization,x-gamecoin-key,idempotency-key,content-type"
HTTP
HTTP/1.1 204 No Content
Access-Control-Allow-Headers: Authorization, Content-Type, X-GameCoin-Key, Idempotency-Key
Access-Control-Allow-Methods: POST, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 600

The server API sends no CORS header: a browser cannot call it, and it must not, because it needs your secret key.

Return URLs of a checkout

A browser can start a checkout, but it cannot choose where the player goes next. successUrl and cancelUrl must have the same origin as the page that calls: the Origin header the browser adds to the request. Any other address is refused with 400 VALIDATION_FAILED and ORIGIN_MISMATCH in details.fieldErrors. A client that does not send an Origin header, such as curl, cannot pass return URLs at all. country is not accepted: the buyer picks it on the payment page.

The browser SDK handles this: it opens the payment page in a window and needs no return URL, or, in redirect mode, it returns to the current page. See Order a currency pack.

A client never credits, except with a gift code

There is no route to grant currency, to grant an item, to adjust a balance or to refund an order in the client API. This is the point of it: the publishable key is public, so anyone can call these routes with it, and nothing they can do creates value. A player can spend what they have and pay with real money; coins appear in a wallet only through your server (a grant), through a paid order, or from a gift code that you created.

The gift code is the one exception, and it is safe because the value is decided by your studio, not by the client: a code is random, limited in uses, works once per player, gives free coins only (never paid coins) and cannot be passed to another player. See Redeem a gift code and Security model.

Returns the active currencies, items and packs of the game. It needs only the publishable key, so you can show a shop before a player is known.

GET/api/v1/client/catalog
  • Authenticationpublishable key
  • Idempotencynot needed
  • Parametersnone

Response

200 OK: a Catalog, the same as the server API returns.

Errors

StatusCodeWhenWhat to do
401UNAUTHENTICATEDThe X-GameCoin-Key header is missing, or the key is unknown or revokedSend the publishable key of your game.
403FORBIDDENA secret key was sentNever use a secret key in a client: use the publishable key.
429RATE_LIMITEDMore than 120 requests a minute from one IP address without a tokenWait Retry-After seconds.

Notes

Creates a player for a game that has no backend, and returns its credentials. Use it the first time a device opens the game. The browser SDK does it for you.

POST/api/v1/client/players
  • Authenticationpublishable key
  • Idempotencynone: every call creates a new player

Call it once per device.

Request body

(JSON; optional, an empty body means {})

FieldTypeRequiredRules
displayNamestringnoName shown in your game: 1 to 80 characters once trimmed.

Response

201 Created: the player and their credentials.

FieldTypeDescription
playerPlayerThe new player: kind is hosted and externalId is null.
playerSecretstringThe secret of the player, returned only here. Store it on the device: it is the only way to get new tokens.
tokenstringA first player token.
expiresAtstringExpiry of token, one hour after issue.

Errors

StatusCodeWhenWhat to do
400VALIDATION_FAILEDAn unknown field, or a displayName that is empty or longer than 80 charactersRead details.fieldErrors.
401UNAUTHENTICATEDThe publishable key is missing, unknown or revokedSend the publishable key of your game.
403FORBIDDENThe game does not allow anonymous players, or a secret key was sentIssue tokens from your backend instead.
429RATE_LIMITEDMore than 30 anonymous players an hour from one IP addressWait Retry-After seconds.

Notes

  • Keep player.id and playerSecret on the device, in a place that survives a restart. A player whose secret is lost cannot be recovered. Never ship them to another player.
  • The player is anonymous: no ext: id, no way to find them again from your backend. If your game has accounts, use the server API and issue tokens for your own users.
  • The browser SDK stores the player in localStorage and renews the token itself.

Exchanges the secret of an anonymous player for a new token, when the previous one has expired. The browser SDK calls it for you when a request answers 401.

POST/api/v1/client/players/token
  • Authenticationpublishable key
  • Idempotencynot needed

Request body

FieldTypeRequiredRules
playerIdstringyesThe player.id returned by Create an anonymous player: 1 to 64 characters.
playerSecretstringyesThe playerSecret returned with it: 1 to 200 characters.

Response

200 OK: a new token.

FieldTypeDescription
tokenstringThe player token, gc_pt_….
expiresAtstringExpiry, one hour after issue.

Errors

StatusCodeWhenWhat to do
400VALIDATION_FAILEDA field is missing, too long or unknownRead details.fieldErrors.
401UNAUTHENTICATEDThe secret is wrong, the player is unknown, or the player is not anonymous: the three cases look the sameThe player cannot be recovered: create a new anonymous player.

Notes

  • It works for anonymous players only. A player created by your backend (ext:) has no secret: ask your server for a token with Issue a player token.

Returns the player of the token, with their wallet and their inventory, in one call. Use it when your game starts and after anything that may have changed the wallet.

GET/api/v1/client/me
  • Authenticationpublishable key and player token
  • Idempotencynot needed
  • Parametersnone

Response

200 OK: the player, their wallet and their owned items.

FieldTypeDescription
playerPlayerThe player of the token.
walletWalletTheir balances, one per active currency, zeros included.
inventoryarray of InventoryItemThe items they own (quantity above 0).

Errors

StatusCodeWhenWhat to do
401UNAUTHENTICATEDThe token is missing, invalid or expired (the cases look the same), or it was issued for another game or environmentGet a new token.
403PLAYER_BLOCKEDThe player is blockedTell the player; nothing can be done from the client.
429RATE_LIMITEDMore than 120 requests a minute for this playerWait Retry-After seconds.

Notes

  • The browser SDK reads this route at init() and keeps a copy: gc.player, gc.wallet and gc.inventory hold it, gc.balance("gems") and gc.owned("potion") answer without a request, and a change event fires when the copy moves. gc.refresh() reads the route again and returns { wallet, inventory }.

Debits a currency from the wallet of the token player. Use it when the player pays coins for something in your game.

POST/api/v1/client/me/wallet/spend
  • Authenticationpublishable key and player token
  • Idempotencyrequired (Idempotency-Key header)

Request body

FieldTypeRequiredRules
currencystringyesA currency code of the catalog (2 to 24 lowercase characters).
amountintegeryesA whole number from 1 to 1,000,000,000,000.
reasonstringnoFree text, at most 500 characters, recorded in the ledger. There is no metadata on the client API.

Response

200 OK: the ledger entry and the new balance, as for the server API.

Errors

StatusCodeWhenWhat to do
400IDEMPOTENCY_KEY_REQUIREDThe Idempotency-Key header is missing or emptySend one. The browser SDK always does.
400VALIDATION_FAILEDAn unknown field (metadata included); amount not an integer between 1 and 10¹²; currency malformedRead details.fieldErrors.
401UNAUTHENTICATEDThe token is missing, invalid or expiredGet a new token and send the request again with the same key.
403PLAYER_BLOCKEDThe player is blockedNothing can be done from the client.
404NOT_FOUNDThe currency is not in the catalogCheck the code against Get the catalog.
409INSUFFICIENT_FUNDSThe balance is lower than amount: details.required and details.availableOffer a pack. Nothing was spent.
409IDEMPOTENCY_CONFLICTThe key was already used with a different requestUse a new key for a new spend.

Notes

  • Same rules as the server route: bonus coins first, all or nothing. A key you choose makes a spend safe against a double click or a page reload: { idempotencyKey } is the last argument of every changing method of the SDK. Without it, the SDK uses a random key per call and reuses it when it replays the request.

Buys an item for the token player: the price is debited and the item delivered in one step, as for the server API. Only the items flagged clientPurchasable can be bought from a client.

POST/api/v1/client/me/purchases
  • Authenticationpublishable key and player token
  • Idempotencyrequired (Idempotency-Key header)

Request body

FieldTypeRequiredRules
skustringyesThe sku of an item that has a price and is clientPurchasable: 1 to 48 characters.
quantityintegernoA whole number from 1 to 1,000,000,000,000. Default 1.

Response

200 OK: the debit, the new balance and the item, as for the server API.

Errors

StatusCodeWhenWhat to do
400IDEMPOTENCY_KEY_REQUIREDThe Idempotency-Key header is missing or emptySend one. The browser SDK always does.
400VALIDATION_FAILEDAn unknown field; sku empty or too long; quantity not an integer between 1 and 10¹²Read details.fieldErrors.
401UNAUTHENTICATEDThe token is missing, invalid or expiredGet a new token and send the request again with the same key.
403FORBIDDENThe item is not clientPurchasableSell it from your backend with Buy an item with currency.
403PLAYER_BLOCKEDThe player is blockedNothing can be done from the client.
404NOT_FOUNDThe item is not in the catalogCheck the sku against Get the catalog.
409INSUFFICIENT_FUNDSThe wallet is lower than the price times quantityOffer a pack. Nothing was debited.
409LIMIT_REACHEDThe purchase would take the player over the item's maxOwnedThe player already has the item.
409CONFLICTThe item has no price in currencySell the item another way.
409IDEMPOTENCY_CONFLICTThe key was already used with a different requestUse a new key for a new purchase.

Notes

  • Tick « Achetable depuis le jeu (API cliente) » on an item in the dashboard only if a player may buy it without your server in the loop. Anything you want to check first (a level, a quest) belongs to your backend.

Removes units of a consumable item the token player owns.

POST/api/v1/client/me/inventory/consume
  • Authenticationpublishable key and player token
  • Idempotencyrequired (Idempotency-Key header)

Request body

FieldTypeRequiredRules
skustringyesThe sku of an item of the catalog: 1 to 48 characters.
quantityintegernoA whole number from 1 to 1,000,000,000,000. Default 1.

Response

200 OK: the item with its remaining quantity, even when it reaches 0.

Errors

StatusCodeWhenWhat to do
400IDEMPOTENCY_KEY_REQUIREDThe Idempotency-Key header is missing or emptySend one. The browser SDK always does.
400VALIDATION_FAILEDAn unknown field; sku empty or too long; quantity not an integer between 1 and 10¹²Read details.fieldErrors.
401UNAUTHENTICATEDThe token is missing, invalid or expiredGet a new token and send the request again with the same key.
403PLAYER_BLOCKEDThe player is blockedNothing can be done from the client.
404NOT_FOUNDThe item is not in the catalogCheck the sku against Get the catalog.
409INSUFFICIENT_FUNDSThe player owns fewer units than quantity: details.required and details.availableNothing was consumed.
409CONFLICTThe item is durable and cannot be consumedOnly consume consumable items.
409IDEMPOTENCY_CONFLICTThe key was already used with a different requestUse a new key for a new consumption.

Notes

  • Same rules as the server route. A client can consume what the player owns; it cannot give them anything.

Creates an order for a pack, for the token player, and returns the hosted payment page. In a browser, the SDK opens it in a window and tells you when the order is over. Payments are simulated in the test environment.

POST/api/v1/client/me/checkouts
  • Authenticationpublishable key and player token
  • Idempotencyrequired (Idempotency-Key header)

Request body

FieldTypeRequiredRules
packSkustringyesThe sku of an active pack: 1 to 48 characters.
successUrlstringnoWhere to send the player after a successful payment. Same origin as the request's Origin header: see Return URLs.
cancelUrlstringnoWhere to send the player when the payment fails. Same rule.
localestringnoLanguage of the hosted payment page, the receipt and the e-mails of this order: fr, en or nl. Any other value is ignored (no error). Without it, the payment page follows the player's browser language (Accept-Language), then French.
creatorCodestringnoA creator code typed by the buyer. A refused code answers 400 VALIDATION_FAILED with fieldErrors.creatorCode = ["CODE_INVALID"] and no order is created. The attempts are limited per player and per IP address.

country is not accepted here: it exists on the server route only.

Response

201 Created for a new order, 200 OK for a replay: the order and the payment page, as for the server route.

Errors

StatusCodeWhenWhat to do
400IDEMPOTENCY_KEY_REQUIREDThe Idempotency-Key header is missing or emptySend one. The browser SDK always does.
400VALIDATION_FAILEDAn unknown field (country included); packSku empty or too long; a return URL that is invalid, not https, or whose origin is not the one of the request (ORIGIN_MISMATCH)Read details.fieldErrors.
401UNAUTHENTICATEDThe token is missing, invalid or expiredGet a new token and send the request again with the same key.
402PAYMENT_FAILEDNo payment provider is available for the environmentUse a test key: payments are simulated there.
403PLAYER_BLOCKEDThe player is blockedNothing can be done from the client.
404NOT_FOUNDThe pack is not in the catalogCheck the sku against Get the catalog.
409LIMIT_REACHEDThe player already bought the pack as many times as its maxPerPlayer allowsHide the pack for this player.
409PACK_NOT_AVAILABLEThe pack is outside its sale window: details.startsAt and details.endsAtShow « coming soon » or « ended » with these dates.
409PACK_SOLD_OUTThe total stock of the pack is used upShow « sold out ». The stock can come back if a pending order expires.
409IDEMPOTENCY_CONFLICTThe key was already used with a different requestUse a new key for a new order.

Notes

  • gc.checkout(packSku, options) resolves with the order read from the API once it is over (fulfilled, failed, expired…), or with its current status if the player closes the window without paying. A fulfilled order refreshes the wallet before the promise resolves. When the browser blocks the window, or with { mode: "redirect" }, the SDK sends the current tab to the payment page and back to the current URL: init() then reads the order and exposes it as gc.returnedOrder.
  • As on the server, an order is created pending; the coins arrive when it is fulfilled. Read the order, not the query string of the return URL.
  • Return URLs are optional: the payment page has a built-in confirmation page that tells the window that opened it with postMessage({ type: "gamecoin:order", orderId, status }).

Redeem a gift code

The token player types a gift code and receives its free coins and items, once. The route is POST /client/me/codes/redeem, with the body { "code": "…" } and an Idempotency-Key. It is described, with its errors, in Redeem a gift code: any refused code answers 400 VALIDATION_FAILED with fieldErrors.code = ["CODE_INVALID"], and attempts are limited per player and per IP address.

Returns an order of the token player, to follow a payment from the client.

GET/api/v1/client/me/orders/{orderId}
  • Authenticationpublishable key and player token
  • Idempotencynot needed

Parameters

ParameterTypeDescription
orderIdstringThe id of an order of this player: 24 hexadecimal characters.

Response

200 OK: an Order.

Errors

StatusCodeWhenWhat to do
401UNAUTHENTICATEDThe token is missing, invalid or expiredGet a new token.
404NOT_FOUNDNo order has this id for this player. An order of another player answers 404 tooCheck the id.

Notes

  • The browser SDK has no method to read an order by id: gc.checkout() follows the order it creates, and gc.returnedOrder is the order of a redirect checkout.
  • Your server reads any order of the game with Get an order.

E-mail sign-in

An anonymous player can link an e-mail address, and find the same player again on another device, with a six-digit code and no password. The four routes below are that contract. What the code does to the session, and why two wallets are never merged, is in Sign in players with their e-mail.

The routes answer 403 FORBIDDEN for a player your server created with ext:<your id>. They are for the anonymous players of POST /client/players.

Asks GameCoin to e-mail a six-digit code. The answer is the same whether the address is already used or not.

POST/api/v1/client/me/email
  • Authenticationpublishable key and player token
  • Idempotencyrequired (Idempotency-Key header)

The player is the one of the token: this device already has a player, and the code links the address or switches to the player that already owns it.

POST/api/v1/client/sign-in/email
  • Authenticationpublishable key
  • Idempotencyrequired (Idempotency-Key header)

No player token: this device is new, or its storage was cleared.

Request body

(JSON, any other field is refused)

FieldTypeRequiredRules
emailstringyesA valid address of at most 254 characters. It is trimmed and lower-cased.
localestringnoLanguage of the e-mail: fr, en or nl (a tag such as nl-BE is accepted). Without it, the Accept-Language header of the request, then French.

Response

200 OK.

FieldTypeDescription
sentbooleanAlways true when the call succeeds.
expiresInSecondsintegerHow long the code can be used. A code lasts 10 minutes (600).

Errors

StatusCodeWhenWhat to do
400VALIDATION_FAILEDThe address is invalid, or the body has an unknown fieldRead details.fieldErrors.
400IDEMPOTENCY_KEY_REQUIREDThe Idempotency-Key header is missing or emptySend one.
401UNAUTHENTICATEDThe player token is missing, invalid or expired on /client/me/emailGet a new token.
403FORBIDDENThe player is not anonymous (it is an ext: player of your backend)Sign-in by e-mail is for anonymous players.
409IDEMPOTENCY_CONFLICTThe key was already used with another addressUse a new key.
429RATE_LIMITEDToo many codes were asked. Retry-After is the seconds to waitWait, then ask again.

Notes

  • The e-mail carries the code and nothing else. It reads the same for a player who links an address and for one who looks for a player.
  • A code lasts 10 minutes, works once, and allows 5 wrong tries. A new code replaces the previous one.
  • Limits: 3 codes per hour per address, 10 per hour per player on /client/me/email, 10 per hour per IP address.
  • Replaying the same Idempotency-Key sends no second e-mail. Keys on these routes are kept 24 hours.

Checks the six digits and returns the session. A wrong, expired, used or unknown code is one answer, on purpose.

POST/api/v1/client/me/email/verify
  • Authenticationpublishable key and player token
  • Idempotencyrequired (Idempotency-Key header)
POST/api/v1/client/sign-in/email/verify
  • Authenticationpublishable key
  • Idempotencyrequired (Idempotency-Key header)

Request body

(JSON, any other field is refused)

FieldTypeRequiredRules
emailstringyesThe same address as when you asked for the code.
codestringyesThe six digits. Spaces and dashes are ignored (123 456).

Response

200 OK.

FieldTypeDescription
tokenstringA player token (one hour) for the player the session now belongs to.
expiresAtstringWhen that token expires, ISO 8601 UTC.
playerPlayerThat player.
mergedbooleantrue when the address was already linked to another player and the session switched to it. No wallet is merged.
playerSecretstring or nullA new secret when the player changed, to keep like the one from Create an anonymous player. null when the player did not change.

Errors

StatusCodeWhenWhat to do
400VALIDATION_FAILEDThe address is invalid, the body has an unknown field, or the code is not six digitsRead details.fieldErrors.
400VALIDATION_FAILEDdetails.fieldErrors.code is ["CODE_INVALID"]: the code is wrong, expired, used up (5 tries), already used, or was never asked forAsk for a new code. The answer is the same for all of these, on purpose.
401UNAUTHENTICATEDThe player token is missing, invalid or expired on /client/me/email/verifyGet a new token.
403FORBIDDENThe player is not anonymousSign-in by e-mail is for anonymous players.
403PLAYER_BLOCKEDThe player that the address belongs to is blockedNothing can be written for this player until they are unblocked.
404NOT_FOUNDsign-in/email/verify with a valid code, but no player is linked to that addressThe code was right; the address was never linked. Only the holder of the code can see this.
409IDEMPOTENCY_CONFLICTThe key was already used with another address or codeUse a new key.
429RATE_LIMITEDToo many checks. Retry-After is the seconds to waitWait, then try again.

Notes

  • When playerSecret is not null, store it in place of the old one, with the new player.id. The browser SDK does this, and fires change when switched is true. See E-mail sign-in.
  • GameCoin never merges two wallets. The player the device had before keeps their coins, items and secret.
  • A replayed check returns the same result with a fresh token. Keys on these routes are kept 24 hours.
  • Checks from one IP address are limited to 30 per 15 minutes. IP limits need TRUST_PROXY=true behind the reverse proxy, like the other rate limits.