JavaScript SDK
spawnle.js is one dependency-free file (UMD). It works as a <script>
(window.Spawnle), with CommonJS (require("@spawnle/sdk")) and as an ES
module (import Spawnle from "@spawnle/sdk"). TypeScript types ship with the
npm package and at /sdk/v1/spawnle.d.ts.
Spawnle.init(options?)
Section titled “Spawnle.init(options?)”Connects to Spawnle and resolves to a Session. Call it once,
early; later calls return the same promise.
| Option | Type | |
|---|---|---|
requireSpawnle |
boolean |
Reject with no_ticket instead of running in local mode. |
localPlayer |
{ name?: string } |
The stand-in player’s name in local mode. |
ticket |
string |
Use this ticket instead of the one in the URL (tests). |
api |
string |
Call another API address. |
Rejects with a SpawnleError when the ticket is invalid or
expired (unauthorized).
Session
Section titled “Session”| Property | Type | |
|---|---|---|
mode |
"spawnle" | "local" |
"spawnle" when started by the launcher. |
ticket |
string | null |
Send it to your server. |
game |
string |
Your game’s id. |
user |
Player |
Who is playing. |
saves |
object | Cloud saves. |
achievements |
object | Achievements. |
leaderboards |
object | Leaderboards. |
presence |
object | Rich presence. |
Player
Section titled “Player”interface Player { handle: string; // unique and stable displayName: string; // falls back to the handle avatarUrl: string | null; avatarColor: string; // CSS colour level: number; studio: { id: string; name: string; title: string; color: string } | null;}| Method | Resolves to |
|---|---|
saves.list() |
{ slot, size, updatedAt: Date }[] |
saves.load(slot) |
string | null |
saves.save(slot, data) |
void — data is a string up to 256 KB |
saves.delete(slot) |
void |
achievements
Section titled “achievements”| Method | Resolves to |
|---|---|
achievements.list() |
Achievement[] |
achievements.unlock(key) |
Achievement & { newly: boolean } |
interface Achievement { key: string; name: string; // "" while hidden and locked description: string; xp: number; hidden: boolean; unlocked: boolean; unlockedAt: Date | null; percent: number; // share of the game's players who have it, 0–100}leaderboards
Section titled “leaderboards”| Method | Resolves to |
|---|---|
leaderboards.get(key, { scope?, limit? }) |
Leaderboard |
leaderboards.submit(key, score) |
{ best, rank, improved } |
scope is "global" (default) or "friends"; limit is 1–100 (default 10).
interface Leaderboard { key: string; name: string; lowerIsBetter: boolean; format: "number" | "time"; scope: "global" | "friends"; total: number; // players with a score entries: LeaderboardEntry[]; // best first me: LeaderboardEntry | null; // the player, even outside `entries`}
interface LeaderboardEntry { rank: number; handle: string; displayName: string; avatarUrl: string | null; avatarColor: string; score: number;}presence
Section titled “presence”| Method | |
|---|---|
presence.set(details) |
Up to 80 characters, one line. |
presence.clear() |
Removes it. |
Spawnle.formatTime(ms)
Section titled “Spawnle.formatTime(ms)”Formats a time score: formatTime(83250) → "1:23.250".
SpawnleError
Section titled “SpawnleError”Every failure rejects with a SpawnleError:
try { await spawnle.saves.save("slot1", hugeString);} catch (error) { if (error instanceof Spawnle.SpawnleError && error.code === "too_large") { // … }}| Property | |
|---|---|
code |
Machine-readable: see Errors and limits. |
message |
Human-readable, in English. |
status |
The HTTP status, when the server answered. |
Spawnle.VERSION
Section titled “Spawnle.VERSION”The SDK’s version, e.g. "1.0.0".