Kuma for developers Early access API reference Manage keys

Website leaderboard

Show your server's top members on your site, kept fresh without hitting rate limits.

You need a key with levels:read. The key stays on your server: your web page asks your server, your server asks Kuma.

Your server: fetch and cache

A leaderboard doesn't need to be live to the second. Caching it for a minute means one request to Kuma a minute however many people visit, well inside the 120 requests a minute each key gets.

Node + Express
import express from "express";

const API = "https://api.kumabot.xyz/v1";
const SERVER_ID = process.env.KUMA_SERVER_ID; // from GET /v1/key
const headers = { Authorization: `Bearer ${process.env.KUMA_KEY}` };

let cache = { at: 0, rows: [] };

async function topMembers(count = 50) {
  if (Date.now() - cache.at < 60_000) return cache.rows;
  const rows = [];
  let cursor = null;
  do {
    const url = new URL(`${API}/servers/${SERVER_ID}/levels/leaderboard`);
    url.searchParams.set("limit", "100");
    if (cursor) url.searchParams.set("cursor", cursor);
    const res = await fetch(url, { headers });
    if (!res.ok) {
      const { error } = await res.json();
      // feature_disabled: the server switched levels off. Keep showing the old board.
      if (cache.rows.length) return cache.rows;
      throw new Error(`${error.code}: ${error.message}`);
    }
    const page = await res.json();
    rows.push(...page.data);
    cursor = page.next_cursor;
  } while (cursor && rows.length < count);
  cache = { at: Date.now(), rows: rows.slice(0, count) };
  return cache.rows;
}

const app = express();
app.get("/leaderboard.json", async (req, res) => {
  const rows = await topMembers();
  res.json(rows.map((r) => ({
    rank: r.rank,
    name: r.user.username ?? "Unknown member",
    avatar: r.user.avatar_url,
    level: r.level,
    xp: r.xp,
  })));
});
app.listen(3000);

Your page

HTML
<ol id="board"></ol>
<script>
  fetch("/leaderboard.json").then((r) => r.json()).then((rows) => {
    document.getElementById("board").innerHTML = rows.map((r) =>
      `<li>${r.name.replace(/[<>&]/g, "")} · level ${r.level}</li>`).join("");
  });
</script>

Ranks and ties

Members with the same XP share a rank (1, 2, 2, 4). Members with no XP aren't listed. To show one member's position, use GET /v1/servers/{server_id}/members/{user_id}/level: it answers for anyone, with a null rank if they have no XP yet.

The coin leaderboard works the same way at GET /v1/servers/{server_id}/economy/leaderboard with economy:read.