Getting Started

GetJar is a gaming platform API for player progress, virtual currency, and leaderboards.

Quick Start

1. Load the SDK

IIFE (browser script tag) — pin a version in production:

html
<!-- Production: pin to a specific version -->
<script src="https://cdn.getjar.com/v5.2.0/getjar-iap-sdk.min.js"></script>

<!-- Development only: latest (5-minute CDN TTL) -->
<script src="https://cdn.getjar.com/latest/getjar-iap-sdk.min.js"></script>

ESM (bundler or modern browser):

javascript
import { Configuration, Leaderboard, UserGame, bridge } from 'https://cdn.getjar.com/v5.2.0/getjar-iap-sdk.esm.js';

If your game only needs the host integration and no HTTP calls, load the bridge-only bundle instead — getjar-iap-bridge.min.js (~6 KB gzipped, no axios), global GetjarIAPBridge.

2. Get Your API Key

  1. Sign in at https://dev.getjar.com
  2. Copy your API key from the dashboard

3. Initialize the SDK

Create a Configuration instance, then pass it to each SDK class. IIFE global is GetjarIAP:

javascript
// After script loads, GetjarIAP is available globally
const cfg = new GetjarIAP.Configuration({
  basePath: 'https://sdk-new.getjar.com',
  apiKey: 'your-api-key'
});

const leaderboardClient = new GetjarIAP.Leaderboard(cfg);
const userGameClient = new GetjarIAP.UserGame(cfg);

ESM / bundler:

javascript
import { Configuration, Leaderboard, UserGame, bridge } from 'https://cdn.getjar.com/v5.2.0/getjar-iap-sdk.esm.js';

const cfg = new Configuration({
  basePath: 'https://sdk-new.getjar.com',
  apiKey: 'your-api-key'
});

const leaderboardClient = new Leaderboard(cfg);
const userGameClient = new UserGame(cfg);

Build the clients once and keep them. They read userId and accessToken from the shared Configuration on every call, so the connect step below fills both in place as soon as the host supplies them.

4. Connect to the Host

Identity arrives from the host page over postMessage, not as a URL parameter. Use the SDK bridge — bridge.connect() — for that handshake and for every other message. It runs the GAME_LOADED exchange, pins the origin in both directions, falls back to a guest session when the host stays silent, and writes the resolved identity into your Configuration.

javascript
const host = await GetjarIAP.bridge.connect({
  configuration: cfg,
  hostOrigin: 'https://getjar.com'
});

// Resolved before the promise settles — never a race, never a wait of your own.
console.log(host.identity.status);       // 'authenticated' | 'guest'
console.log(host.identity.anonymousId);
console.log(host.identity.eightpointId);

startGame();

connect() resolves as a guest rather than rejecting when the player is signed out — that is a normal state, not an error. It also re-applies the token on every later PARENT_DATA the host sends, so a cross-tab sign-in updates your clients without any work from you. When the host answers nothing at all, identity resolves as a guest with a null anonymousId — there is no id to write, so userId stays unset and requests go out without x-user-id. Call cfg.setUserId() with your own local guest id if your game needs one.

Do not hand-write window.parent.postMessage or a message listener. Raw postMessage integration is not a supported path: it skips the origin pinning, the retry cadence, the guest fallback, the ack correlation and the request timeouts the bridge exists to provide. See PostMessage Integration for the full API and the wire contract behind it.

5. Submit a Score

There is one route: end the round through the bridge and the host submits the score on your behalf. The Leaderboard SDK is read-only — it has no score submission method, so there is nothing to choose between.

javascript
const roundId = crypto.randomUUID();

// Terminal end of round — the host validates, acks, then submits the score.
await host.gameOver({ roundId, score: 1250, durationMs: 94000 });

// Mid-game progress, distinct from the terminal end of round.
await host.levelComplete({ roundId, level: 3 });

The promise resolving means the host accepted the round, not that the score reached the backend — never block your end-of-round UI on it. Invalid input rejects with BridgeValidationError before anything is sent, and a host that never answers rejects with BridgeTimeoutError.

Send one gameOver() per round — a second call with a fresh roundId creates a second entry. Coin awards follow the same rule: host.grantCoins() is the only supported path, and the host is authoritative over the resulting balance.

Configuration Options

OptionTypeEffect
basePathstringAPI base URL. Defaults to http://localhost:8000.
apiKeystring | Promise<string> | (name) => string | Promise<string>Sent as the x-api-key header.
userIdstringSent as the x-user-id header, and omitted while unset. Set for you by bridge.connect() from eightpointId ?? anonymousId whenever the host supplies one; setUserId() overrides it.
accessTokenstring | Promise<string> | (name?, scopes?) => string | Promise<string>Sent as Authorization: Bearer. Set for you by bridge.connect() on every identity that carries a token, and cleared when the player signs out or a token refresh fails.
usernamestringWith password, sends Authorization: Basic, overwriting any Bearer token.
passwordstringPaired with username.
timeoutnumberRequest timeout in milliseconds. Defaults to 30000.
headersRecord<string, string>Extra headers merged into every request.
baseOptionsanyRaw axios options spread onto the instance.
Guest players have no signed-in user record — the host sends only anonymousId and eightpointId. Leaderboard queries accept identityId, userId, anonymousId, and eightpointId as filters, so a guest can still be located in a ranking.

Features

  • Game Bridge — Typed host integration: identity handshake, round lifecycle, coin grants, token refresh
  • Leaderboard SDK — Read per-game cumulative and daily rankings
  • User Game SDK — Update player progress
Coins are awarded with host.grantCoins(), which the host fulfils against the economy API server-side. There is no client-side balance or spend API — the SDK classes above cover leaderboards and progress only.