Brewser Docs
Features

brewser.js SDK Reference

The optional drop-in SDK for cloud saves and leaderboards

brewser.js is a tiny, optional SDK that gives your app cloud saves and leaderboards with almost no code. This page is the API index; the Saves & Leaderboards guide has the narrative, worked examples and gotchas.

It's a file you ship, not a runtime API

Brewser makes a point of not having proprietary brewser.* runtime globals — apps stay portable, and if it runs on Brewser it runs in Chrome. brewser.js keeps that promise: it's an ordinary script you bundle with your app, not something the runtime injects. It installs one global, brewser, and degrades gracefully everywhere:

  • Signed outsave()/load() still work instantly against local storage; cloud sync just waits until the user signs in. leaderboards.list() still works (it's public).
  • In a desktop browser — the same file runs; the cloud calls hit the same public API.

Ship it and include it before your own script:

<script src="./brewser.js"></script>
<script>
  brewser.save({ level: 3, coins: 120 });
  const data = brewser.load();   // { level: 3, coins: 120 }
</script>

The SDK auto-detects your package id from the catalogue URL (.../apps/{group}/{id}/...). If you're serving from somewhere else, set it: brewser.configure({ packageId: 'com.you.mygame' }).

The only cloud dependency is the user being signed in — nothing here needs a manifest permission.

API at a glance

Synchronous calls return immediately; the rest return Promises. Full semantics, return shapes and gotchas are in Saves & Leaderboards.

Whole-save (local-first, background cloud sync)

CallReturnsAuth
brewser.save(data)boolean — writes local instantly, schedules a cloud pushworks signed-out (local); cloud needs sign-in
brewser.load()your data or null
brewser.info(){ updatedAt } or null
brewser.pull(opts)Promise<{ ok, data, updatedAt, reason? }> — the account copy; { adopt: true } also writes it locallysigned in
brewser.sync()Promise — force an immediate pushsigned in
brewser.clearLocal()boolean — clears local only
brewser.canSync()boolean — is a cross-device sync possible now

Records (CRUD over the save blob)

The SDK stamps id, createdAt, updatedAt — you never set those.

CallReturns
brewser.put(record)string (new id)
brewser.get(id)record or null
brewser.update(id, changes)updated record or null
brewser.remove(id)boolean
brewser.list(opts)object[]opts = { sortBy, desc }

Leaderboards (brewser.leaderboards)

CallReturnsAuth
.config({ order })the leaderboards object — 'desc' (default) or 'asc'
.order()'desc' | 'asc'
.submit(score, { name })Promise<{ ok, best, rank, updated, onBoard }> — best-keptsigned in
.list(n = 10)Promise<{ ok, order, count, top, me? }>public
.aroundMe(n = 3)Promise<{ ok, order, count, top, me, window }>signed in
.me()Promise<{ ok, me }>signed in
.remove()Promise<{ ok }> — deletes your own entrysigned in

Authenticated calls that fail for lack of sign-in resolve with { ok: false, reason: 'unauth' } rather than throwing. Other failure reasons: 'offline', 'server', 'nothing-local', 'bad-score'.

brewser.configure(opts)

Merges options and returns brewser:

OptionDefaultPurpose
packageIdauto from URLYour app's save box & board.
tokenProvide an auth token explicitly.
onSyncnullSync-status callback: 'pushing' | 'synced' | 'offline' | 'unauth' | 'error'.
apiBasehttps://brewser.io/wp-json/brewser/v1The save/leaderboard API root.
nsPrefixbrewser_save_localStorage key prefix.
pushDebounceMs1500Delay before a background push coalesces writes.

Service limits

The cloud API enforces a few limits, worth designing around:

Limit
Save blob size256 KB per app, per user
Save writes~60 per minute per user
Leaderboardkeeps your best; a worse score never lowers you
Leaderboard entries retained~100 per app
Leaderboard submits~30 per minute per user
Score range|score| ≤ 1e12

Sign-in, and the on-device seam

Inside the Brewser player your app doesn't manage auth — the SDK finds the token for you, in order:

  1. a token you passed to brewser.configure({ token });
  2. window.__brewserAuthToken(), if the host page exposes it;
  3. a postMessage of { type: 'brewser-token', token } from the Brewser origin.

Signature verification of the token happens on the server — the SDK and runtime never decode it.

On this page