Quickstart
Add a leaderboard to a browser game in a few minutes, with no backend of your own.
Steps
- Sign in to the dashboard with GitHub or Google.
- Create an app for your game.
- In the app, under API keys, create a publishable key. It starts with
lb_pk_and is safe to ship in the game. Keep secret keys (lb_sk_) on a server. - Under Allowed origins, add the origin your game runs on, for example
https://game.example, one per line. Browsers on any other origin are refused with403. - Under Metrics, create a metric with the key
highscoreand the aggregationmax, so each player keeps their best score. - Under Leaderboards, create a leaderboard with the key
alltimeon thehighscoremetric, ranked highest first. - Install the SDK:
npm i @oneleader/sdkThe install only works once
@oneleader/sdkis on npm. If npm can't find it yet, check back soon. - Start a session, submit a score and read the leaderboard:
import { createClient, OneLeaderError } from "@oneleader/sdk";
// Keep the session so a returning player stays the same player (tokens never expire).
const saved = localStorage.getItem("oneleader-session");
const client = createClient({ publishableKey: "lb_pk_...", session: saved ? JSON.parse(saved) : undefined });
if (!saved) localStorage.setItem("oneleader-session", JSON.stringify(await client.startSession()));
await client.setDisplayName("Ann");
try {
const receipt = await client.submitScore("highscore", 42);
console.log(receipt.leaderboards); // this score's rank on every board that uses the metric
} catch (e) {
if (!(e instanceof OneLeaderError)) throw e;
if (e.code === "quota_exceeded") { /* 402: the app is over its plan's monthly event cap; reads still work */ }
else if (e.code === "rate_limited") { /* 429: too many requests, try again shortly */ }
else if (e.code === "unauthorized") { /* 401: wrong or revoked key, or a session from another app */ }
else throw e;
}
const board = await client.getLeaderboard("alltime", { limit: 10 });
console.log(board.rankings, board.user); // `user` is the current player's own row and rank
Errors
Every failed call throws a OneLeaderError. Branch on code (or the HTTP status):
| Status | code | Meaning |
|---|---|---|
| 402 | quota_exceeded | The app is over its plan's monthly event cap. The score isn't recorded; reads keep working. Usage resets on the 1st of each month (UTC). |
| 429 | rate_limited | Too many requests from the app or this player. Wait and try again. |
| 401 | unauthorized | The key is wrong or revoked, or the saved session belongs to another app. |
| 400 | bad_request | Invalid input, for example a score outside the metric's bounds. |
| 403 | forbidden | The page's origin isn't in the app's allowed origins, or the player is banned. |
| 404 | not_found | No metric or leaderboard with that key in this app. |
| 409 | conflict | An Idempotency-Key was reused for a different event. |
Every endpoint is in the API reference.