OneLeader

Quickstart

Add a leaderboard to a browser game in a few minutes, with no backend of your own.

Steps

  1. Sign in to the dashboard with GitHub or Google.
  2. Create an app for your game.
  3. 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.
  4. 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 with 403.
  5. Under Metrics, create a metric with the key highscore and the aggregation max, so each player keeps their best score.
  6. Under Leaderboards, create a leaderboard with the key alltime on the highscore metric, ranked highest first.
  7. Install the SDK:
    npm i @oneleader/sdk

    The install only works once @oneleader/sdk is on npm. If npm can't find it yet, check back soon.

  8. 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):

StatuscodeMeaning
402quota_exceededThe 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).
429rate_limitedToo many requests from the app or this player. Wait and try again.
401unauthorizedThe key is wrong or revoked, or the saved session belongs to another app.
400bad_requestInvalid input, for example a score outside the metric's bounds.
403forbiddenThe page's origin isn't in the app's allowed origins, or the player is banned.
404not_foundNo metric or leaderboard with that key in this app.
409conflictAn Idempotency-Key was reused for a different event.

Every endpoint is in the API reference.