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.
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<script src="https://gamecoin.apilow.com/sdk/gamecoin-1.2.0.js"></script>
<script type="module">
const gc = await GameCoin.init({ publishableKey: "gc_pk_test_…", playerToken }); // playerToken: from your backend
// ... the example you want to run goes here
</script>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
| Header | Value | Needed on |
|---|---|---|
X-GameCoin-Key | The publishable key, gc_pk_test_… or gc_pk_live_… | Every client route |
Authorization | Bearer gc_pt_…, a player token | The 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.
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/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: 600The 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.
/api/v1/client/catalog- Authenticationpublishable key
- Idempotencynot needed
- Parametersnone
Response
200 OK: a Catalog, the same as the server API returns.
Errors
| Status | Code | When | What to do |
|---|---|---|---|
| 401 | UNAUTHENTICATED | The X-GameCoin-Key header is missing, or the key is unknown or revoked | Send the publishable key of your game. |
| 403 | FORBIDDEN | A secret key was sent | Never use a secret key in a client: use the publishable key. |
| 429 | RATE_LIMITED | More than 120 requests a minute from one IP address without a token | Wait Retry-After seconds. |
Notes
items[].clientPurchasabletells which items Buy an item with currency accepts from a client.
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.
/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 {})
| Field | Type | Required | Rules |
|---|---|---|---|
displayName | string | no | Name shown in your game: 1 to 80 characters once trimmed. |
Response
201 Created: the player and their credentials.
| Field | Type | Description |
|---|---|---|
player | Player | The new player: kind is hosted and externalId is null. |
playerSecret | string | The secret of the player, returned only here. Store it on the device: it is the only way to get new tokens. |
token | string | A first player token. |
expiresAt | string | Expiry of token, one hour after issue. |
Errors
| Status | Code | When | What to do |
|---|---|---|---|
| 400 | VALIDATION_FAILED | An unknown field, or a displayName that is empty or longer than 80 characters | Read details.fieldErrors. |
| 401 | UNAUTHENTICATED | The publishable key is missing, unknown or revoked | Send the publishable key of your game. |
| 403 | FORBIDDEN | The game does not allow anonymous players, or a secret key was sent | Issue tokens from your backend instead. |
| 429 | RATE_LIMITED | More than 30 anonymous players an hour from one IP address | Wait Retry-After seconds. |
Notes
- Keep
player.idandplayerSecreton 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
localStorageand 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.
/api/v1/client/players/token- Authenticationpublishable key
- Idempotencynot needed
Request body
| Field | Type | Required | Rules |
|---|---|---|---|
playerId | string | yes | The player.id returned by Create an anonymous player: 1 to 64 characters. |
playerSecret | string | yes | The playerSecret returned with it: 1 to 200 characters. |
Response
200 OK: a new token.
| Field | Type | Description |
|---|---|---|
token | string | The player token, gc_pt_…. |
expiresAt | string | Expiry, one hour after issue. |
Errors
| Status | Code | When | What to do |
|---|---|---|---|
| 400 | VALIDATION_FAILED | A field is missing, too long or unknown | Read details.fieldErrors. |
| 401 | UNAUTHENTICATED | The secret is wrong, the player is unknown, or the player is not anonymous: the three cases look the same | The 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.
/api/v1/client/me- Authenticationpublishable key and player token
- Idempotencynot needed
- Parametersnone
Response
200 OK: the player, their wallet and their owned items.
| Field | Type | Description |
|---|---|---|
player | Player | The player of the token. |
wallet | Wallet | Their balances, one per active currency, zeros included. |
inventory | array of InventoryItem | The items they own (quantity above 0). |
Errors
| Status | Code | When | What to do |
|---|---|---|---|
| 401 | UNAUTHENTICATED | The token is missing, invalid or expired (the cases look the same), or it was issued for another game or environment | Get a new token. |
| 403 | PLAYER_BLOCKED | The player is blocked | Tell the player; nothing can be done from the client. |
| 429 | RATE_LIMITED | More than 120 requests a minute for this player | Wait Retry-After seconds. |
Notes
- The browser SDK reads this route at
init()and keeps a copy:gc.player,gc.walletandgc.inventoryhold it,gc.balance("gems")andgc.owned("potion")answer without a request, and achangeevent 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.
/api/v1/client/me/wallet/spend- Authenticationpublishable key and player token
- Idempotencyrequired (Idempotency-Key header)
Request body
| Field | Type | Required | Rules |
|---|---|---|---|
currency | string | yes | A currency code of the catalog (2 to 24 lowercase characters). |
amount | integer | yes | A whole number from 1 to 1,000,000,000,000. |
reason | string | no | Free 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
| Status | Code | When | What to do |
|---|---|---|---|
| 400 | IDEMPOTENCY_KEY_REQUIRED | The Idempotency-Key header is missing or empty | Send one. The browser SDK always does. |
| 400 | VALIDATION_FAILED | An unknown field (metadata included); amount not an integer between 1 and 10¹²; currency malformed | Read details.fieldErrors. |
| 401 | UNAUTHENTICATED | The token is missing, invalid or expired | Get a new token and send the request again with the same key. |
| 403 | PLAYER_BLOCKED | The player is blocked | Nothing can be done from the client. |
| 404 | NOT_FOUND | The currency is not in the catalog | Check the code against Get the catalog. |
| 409 | INSUFFICIENT_FUNDS | The balance is lower than amount: details.required and details.available | Offer a pack. Nothing was spent. |
| 409 | IDEMPOTENCY_CONFLICT | The key was already used with a different request | Use 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.
/api/v1/client/me/purchases- Authenticationpublishable key and player token
- Idempotencyrequired (Idempotency-Key header)
Request body
| Field | Type | Required | Rules |
|---|---|---|---|
sku | string | yes | The sku of an item that has a price and is clientPurchasable: 1 to 48 characters. |
quantity | integer | no | A 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
| Status | Code | When | What to do |
|---|---|---|---|
| 400 | IDEMPOTENCY_KEY_REQUIRED | The Idempotency-Key header is missing or empty | Send one. The browser SDK always does. |
| 400 | VALIDATION_FAILED | An unknown field; sku empty or too long; quantity not an integer between 1 and 10¹² | Read details.fieldErrors. |
| 401 | UNAUTHENTICATED | The token is missing, invalid or expired | Get a new token and send the request again with the same key. |
| 403 | FORBIDDEN | The item is not clientPurchasable | Sell it from your backend with Buy an item with currency. |
| 403 | PLAYER_BLOCKED | The player is blocked | Nothing can be done from the client. |
| 404 | NOT_FOUND | The item is not in the catalog | Check the sku against Get the catalog. |
| 409 | INSUFFICIENT_FUNDS | The wallet is lower than the price times quantity | Offer a pack. Nothing was debited. |
| 409 | LIMIT_REACHED | The purchase would take the player over the item's maxOwned | The player already has the item. |
| 409 | CONFLICT | The item has no price in currency | Sell the item another way. |
| 409 | IDEMPOTENCY_CONFLICT | The key was already used with a different request | Use 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.
/api/v1/client/me/inventory/consume- Authenticationpublishable key and player token
- Idempotencyrequired (Idempotency-Key header)
Request body
| Field | Type | Required | Rules |
|---|---|---|---|
sku | string | yes | The sku of an item of the catalog: 1 to 48 characters. |
quantity | integer | no | A 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
| Status | Code | When | What to do |
|---|---|---|---|
| 400 | IDEMPOTENCY_KEY_REQUIRED | The Idempotency-Key header is missing or empty | Send one. The browser SDK always does. |
| 400 | VALIDATION_FAILED | An unknown field; sku empty or too long; quantity not an integer between 1 and 10¹² | Read details.fieldErrors. |
| 401 | UNAUTHENTICATED | The token is missing, invalid or expired | Get a new token and send the request again with the same key. |
| 403 | PLAYER_BLOCKED | The player is blocked | Nothing can be done from the client. |
| 404 | NOT_FOUND | The item is not in the catalog | Check the sku against Get the catalog. |
| 409 | INSUFFICIENT_FUNDS | The player owns fewer units than quantity: details.required and details.available | Nothing was consumed. |
| 409 | CONFLICT | The item is durable and cannot be consumed | Only consume consumable items. |
| 409 | IDEMPOTENCY_CONFLICT | The key was already used with a different request | Use 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.
/api/v1/client/me/checkouts- Authenticationpublishable key and player token
- Idempotencyrequired (Idempotency-Key header)
Request body
| Field | Type | Required | Rules |
|---|---|---|---|
packSku | string | yes | The sku of an active pack: 1 to 48 characters. |
successUrl | string | no | Where to send the player after a successful payment. Same origin as the request's Origin header: see Return URLs. |
cancelUrl | string | no | Where to send the player when the payment fails. Same rule. |
locale | string | no | Language 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. |
creatorCode | string | no | A 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
| Status | Code | When | What to do |
|---|---|---|---|
| 400 | IDEMPOTENCY_KEY_REQUIRED | The Idempotency-Key header is missing or empty | Send one. The browser SDK always does. |
| 400 | VALIDATION_FAILED | An 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. |
| 401 | UNAUTHENTICATED | The token is missing, invalid or expired | Get a new token and send the request again with the same key. |
| 402 | PAYMENT_FAILED | No payment provider is available for the environment | Use a test key: payments are simulated there. |
| 403 | PLAYER_BLOCKED | The player is blocked | Nothing can be done from the client. |
| 404 | NOT_FOUND | The pack is not in the catalog | Check the sku against Get the catalog. |
| 409 | LIMIT_REACHED | The player already bought the pack as many times as its maxPerPlayer allows | Hide the pack for this player. |
| 409 | PACK_NOT_AVAILABLE | The pack is outside its sale window: details.startsAt and details.endsAt | Show « coming soon » or « ended » with these dates. |
| 409 | PACK_SOLD_OUT | The total stock of the pack is used up | Show « sold out ». The stock can come back if a pending order expires. |
| 409 | IDEMPOTENCY_CONFLICT | The key was already used with a different request | Use 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. Afulfilledorder 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 asgc.returnedOrder.- As on the server, an order is created
pending; the coins arrive when it isfulfilled. 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.
/api/v1/client/me/orders/{orderId}- Authenticationpublishable key and player token
- Idempotencynot needed
Parameters
| Parameter | Type | Description |
|---|---|---|
orderId | string | The id of an order of this player: 24 hexadecimal characters. |
Response
200 OK: an Order.
Errors
| Status | Code | When | What to do |
|---|---|---|---|
| 401 | UNAUTHENTICATED | The token is missing, invalid or expired | Get a new token. |
| 404 | NOT_FOUND | No order has this id for this player. An order of another player answers 404 too | Check the id. |
Notes
- The browser SDK has no method to read an order by id:
gc.checkout()follows the order it creates, andgc.returnedOrderis 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.
/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.
/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)
| Field | Type | Required | Rules |
|---|---|---|---|
email | string | yes | A valid address of at most 254 characters. It is trimmed and lower-cased. |
locale | string | no | Language 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.
| Field | Type | Description |
|---|---|---|
sent | boolean | Always true when the call succeeds. |
expiresInSeconds | integer | How long the code can be used. A code lasts 10 minutes (600). |
Errors
| Status | Code | When | What to do |
|---|---|---|---|
| 400 | VALIDATION_FAILED | The address is invalid, or the body has an unknown field | Read details.fieldErrors. |
| 400 | IDEMPOTENCY_KEY_REQUIRED | The Idempotency-Key header is missing or empty | Send one. |
| 401 | UNAUTHENTICATED | The player token is missing, invalid or expired on /client/me/email | Get a new token. |
| 403 | FORBIDDEN | The player is not anonymous (it is an ext: player of your backend) | Sign-in by e-mail is for anonymous players. |
| 409 | IDEMPOTENCY_CONFLICT | The key was already used with another address | Use a new key. |
| 429 | RATE_LIMITED | Too many codes were asked. Retry-After is the seconds to wait | Wait, 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-Keysends 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.
/api/v1/client/me/email/verify- Authenticationpublishable key and player token
- Idempotencyrequired (Idempotency-Key header)
/api/v1/client/sign-in/email/verify- Authenticationpublishable key
- Idempotencyrequired (Idempotency-Key header)
Request body
(JSON, any other field is refused)
| Field | Type | Required | Rules |
|---|---|---|---|
email | string | yes | The same address as when you asked for the code. |
code | string | yes | The six digits. Spaces and dashes are ignored (123 456). |
Response
200 OK.
| Field | Type | Description |
|---|---|---|
token | string | A player token (one hour) for the player the session now belongs to. |
expiresAt | string | When that token expires, ISO 8601 UTC. |
player | Player | That player. |
merged | boolean | true when the address was already linked to another player and the session switched to it. No wallet is merged. |
playerSecret | string or null | A new secret when the player changed, to keep like the one from Create an anonymous player. null when the player did not change. |
Errors
| Status | Code | When | What to do |
|---|---|---|---|
| 400 | VALIDATION_FAILED | The address is invalid, the body has an unknown field, or the code is not six digits | Read details.fieldErrors. |
| 400 | VALIDATION_FAILED | details.fieldErrors.code is ["CODE_INVALID"]: the code is wrong, expired, used up (5 tries), already used, or was never asked for | Ask for a new code. The answer is the same for all of these, on purpose. |
| 401 | UNAUTHENTICATED | The player token is missing, invalid or expired on /client/me/email/verify | Get a new token. |
| 403 | FORBIDDEN | The player is not anonymous | Sign-in by e-mail is for anonymous players. |
| 403 | PLAYER_BLOCKED | The player that the address belongs to is blocked | Nothing can be written for this player until they are unblocked. |
| 404 | NOT_FOUND | sign-in/email/verify with a valid code, but no player is linked to that address | The code was right; the address was never linked. Only the holder of the code can see this. |
| 409 | IDEMPOTENCY_CONFLICT | The key was already used with another address or code | Use a new key. |
| 429 | RATE_LIMITED | Too many checks. Retry-After is the seconds to wait | Wait, then try again. |
Notes
- When
playerSecretis notnull, store it in place of the old one, with the newplayer.id. The browser SDK does this, and fireschangewhenswitchedis 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=truebehind the reverse proxy, like the other rate limits.