One page: a working game
Save one HTML file, serve it, and sell a pack and an item with the browser SDK and your publishable key.
On this page
What you get
One page. It shows the player's gems and potions, sells the pack gems-500 for euros (simulated in test), sells a potion for 20 gems, and lets the player drink it. There is no server of yours. The same calls are written step by step in the quickstart and in A game without a backend.
Set up the game once
The page expects these codes. The labels below are the French ones; the dashboard also speaks English and Dutch.
- Create an account and a game (« Nouveau jeu »). The test keys are created with it.
- Create the paid currency
gems(« Monnaies », type « Payante »). - Create the item
potion(« Objets »): type « Consommable », price 20gems, and tick « Achetable depuis le jeu (API cliente) ». - Create the pack
gems-500(« Packs »): 500 paidgems, 50 free, price 4,99 €. A new pack is a draft: use « Publier ». - Copy the publishable key
gc_pk_test_…from « Clés d'API ». Leave « Autoriser les joueurs anonymes » on (« Réglages »). It is on by default.
The page
Replace gc_pk_test_… with your publishable key. Save the file as index.html.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Hello GameCoin</title>
<style>
body { font: 16px/1.5 system-ui, sans-serif; max-width: 36rem; margin: 2rem auto; padding: 0 1rem; }
button { min-height: 44px; margin: 0.25rem 0.25rem 0.25rem 0; }
#status { min-height: 1.5rem; }
</style>
</head>
<body>
<h1>Hello GameCoin</h1>
<p>Gems: <strong id="gems">…</strong> · Potions: <strong id="potions">…</strong></p>
<p id="status" role="status"></p>
<button type="button" id="buy-pack">Buy 500 gems</button>
<button type="button" id="buy-potion">Buy a potion (20 gems)</button>
<button type="button" id="use-potion">Drink a potion</button>
<script src="https://gamecoin.apilow.com/sdk/gamecoin-1.2.0.js"></script>
<script type="module">
const status = document.getElementById("status");
const gc = await GameCoin.init({ publishableKey: "gc_pk_test_…" });
const pack = (await gc.getCatalog()).packs.find((item) => item.sku === "gems-500");
if (pack) document.getElementById("buy-pack").textContent = `Buy ${pack.name} (${(pack.priceCents / 100).toFixed(2)} €)`;
const render = () => {
document.getElementById("gems").textContent = String(gc.balance("gems"));
document.getElementById("potions").textContent = String(gc.owned("potion"));
};
gc.on("change", render);
render();
const fail = (error) => { status.textContent = error.code ?? "The call failed."; };
// checkout() opens the payment window. Call it from the click, or the browser blocks the window.
document.getElementById("buy-pack").onclick = async () => {
try {
const order = await gc.checkout("gems-500");
status.textContent = `Pack order: ${order.status}`;
} catch (error) {
fail(error);
}
};
document.getElementById("buy-potion").onclick = () => gc.buyItem("potion", 1).then(() => { status.textContent = "Potion added."; }).catch(fail);
document.getElementById("use-potion").onclick = () => gc.consume("potion", 1).then(() => { status.textContent = "Potion used."; }).catch(fail);
</script>
</body>
</html>Open it
Open the file from a small web server, not from a file:// address. From the folder that contains index.html:
python -m http.server 8080Then open http://localhost:8080. The first visit creates an anonymous player and remembers them in this browser.
What you should see
Gems and potions start at 0. The pack button takes its name and price from the catalog.
Click the pack button. The payment page opens. It follows the player's language: in English the simulated buttons are « Pay » and « Decline »; in French they are « Payer » and « Refuser ». No money moves. After « Pay », the status line says Pack order: fulfilled and the gems show 550 (500 paid and 50 free).
Buy a potion: gems go to 530, potions to 1. Drink it: potions go back to 0. Reload the page: the same player, the same balance.
When a button fails
| What you see | What to change |
|---|---|
| The page stays blank, or the SDK never starts | Serve the file over http://, and check the publishable key. |
NOT_FOUND on the pack | Publish gems-500 (« Publier »). A draft is invisible to the game. |
FORBIDDEN on the potion | Tick « Achetable depuis le jeu (API cliente) » on potion. |
INSUFFICIENT_FUNDS | Buy the pack first. A new player has no gems. |
FORBIDDEN on start | Anonymous players are off. Turn « Autoriser les joueurs anonymes » back on. |
| The payment window never opens | The call has to run directly from the click. The file above does that. |
Where next
| You want to… | Read |
|---|---|
| Walk the same path in smaller steps | Quickstart |
| Keep the player when they change device | Sign in with e-mail |
| Credit gems from your own server | A game with a backend |
| Look up every browser call | Browser SDK |
| Look up a route, its body and its response | API reference |