JavaScript SDK — Client

Client — mp / MultiplayerClient

constructor
new MultiplayerClient(apiToken, options?)

Creates the client. apiToken is the public game token (safe in client code). Options: apiPrivateToken (admin — server-side only), baseUrl, timeout ms (default 30000), retries (GET retries, default 1), fetch (custom impl for tests).

service facades
mp.players · mp.rooms · mp.actions · mp.updates · mp.matchmaking · mp.matchmakingRequests · mp.leaderboard · mp.friends · mp.games · mp.moderation · mp.time · mp.realtime

One facade per API family — same coverage as the other SDKs. mp.createRealtime(options) builds the realtime client.

send
await mp.send(method, url, body?, { playerToken?, usePrivateToken? })

Low-level request primitive used by every facade. Credentials go in headers only. API failures are returned ({success:false,error}), not thrown — transport failures throw TransportError.

url
mp.url(endpoint, query?)

Builds an API URL; skips undefined query values. Credentials are never placed in the URL.

errors
MultiplayerError · TransportError · ConfigurationError · RealtimeError

TransportError carries .url/.cause (timeout, DNS, non-JSON). ConfigurationError = misuse (missing tokens, admin call without private token, UDP in a browser). RealtimeError = socket-level failure with .code/.reason.

Client lifecycle and request model

One client instance carries your game's credentials and connection settings — the public X-Api-Token, the secret X-Api-Private-Token where admin calls are needed, plus the API base URL, timeouts and JSON plumbing. Construct it once at startup and share it for the process lifetime: every facade on this page borrows that configuration, so creating a client per request only re-pays connection setup.

Two credential tiers ride on the same client. Player-facing calls present the X-Game-Player-Token issued at registration; administrative calls — bans, data writes, player lists — authenticate with the token pair alone. Credentials travel in headers only, never in URLs, so they cannot leak through access logs or referrers.

  • Typed facades cover each API family; the low-level send primitive reaches any endpoint that has no facade yet.
  • API failures come back as a structured error envelope — only transport faults (DNS, timeouts, non-JSON replies) surface as transport errors.
  • Pass the player token per call rather than embedding it in the client — one client can serve every local player.
  • Every call returns a Promise — await inside async functions. In browsers requests run over fetch with CORS; on Node.js 22.12+ the identical API uses the built-in fetch.