Przejdź do treści
InstaPlay
Sekcje dokumentacji SDK

The player in the InstaPlay SDK

InstaPlay.player.get()

const player = await InstaPlay.player.get()
// { id: 'p3k9x2…', kind: 'guest', nickname: 'Swift Fox 48', avatar: null }
  • id — the player's pseudonym for your studio: stable across your studio's games and different for other studios. The real player id, cookies and tokens are never passed to the game.
  • kind — guest or account (the player signed in on the portal with VK ID, Yandex ID, Google or email — in the portal header or from the game, player.login() below). The pseudonym does not change on the first sign-in: the guest's progress becomes the account's progress.
  • nickname — the guest nickname, derived from the id, in the portal language; for a signed-in player — the nickname they chose in their profile. A chosen nickname is global (the same in every game — the player is warned); if none is chosen, the guest nickname for your studio is used.
  • avatar — null for a guest: the portal draws an identicon itself. For an account with a chosen avatar — an image path on your game's origin: /_ip/avatars/v1/<key>.webp (CSP img-src 'self' allows it). Players do not upload avatars — only portal presets.
// { id: 'p3k9x2…', kind: 'account', nickname: 'Foxy', avatar: '/_ip/avatars/v1/fox-03.webp' }

Email, sign-in provider ids, account cookies and tokens are never passed to the game.

InstaPlay.player.getToken()

SDK 1.1. A player token signed by the portal — for your own game server: player.id is public (it is visible in leaderboards) and is not good enough to sign in to a server.

if (InstaPlay.isSupported('token')) {
  const token = await InstaPlay.player.getToken() // JWT ES256, lives 10 minutes
  await fetch('https://api.my-game.example/login', { method: 'POST', body: token })
}
  • The token is a JWT (typ: ip-player+jwt): sub is the same player.id, aud is the game id, kind, lifetime 10 minutes. The game server verifies the signature with the portal keys at https://instaplay.ru/.well-known/instaplay-jwks.json.
  • The SDK caches the token until a minute before it expires; repeated calls do not reach the portal.
  • No token feature (local/disabled modes, no session) — not_supported.
  • In a preview aud is preview:<game id>: a production server rejects such a token by itself.

InstaPlay.player.login()

SDK 1.4. The portal sign-in window over the game — the counterpart of CrazyGames user.showAuthPrompt(): Google, VK ID, Yandex ID (if configured on the portal) and email with a one-time code. Examples, patterns and the CrazyGames SDK mapping are in the Player and sign-in guide.

InstaPlay.player.login(): Promise<LoginResult>

type LoginResult =
  | { status: 'signed_in'; player: PlayerInfo } // signed in
  | { status: 'already_signed_in'; player: PlayerInfo } // already an account, no window
  | { status: 'cancelled' } // closed the window
loginButton.hidden = !(InstaPlay.isSupported('login') && me.kind === 'guest')
loginButton.addEventListener('click', async () => {
  const result = await InstaPlay.player.login() // only in a player action handler
  if (result.status !== 'cancelled') loginButton.hidden = true
})
  • Only on a player gesture — in a click, tap or key handler, right away, before long awaits. Otherwise — the gesture_required error. At most 3 times a minute and not within 10 s after the window was closed without signing in (rate_limited).
  • One window at a time: a repeated call while the window is open returns the same promise. A window left open for 15 minutes closes by itself — cancelled; the SDK waits 16 minutes for a reply (timeout).
  • Pause: while the window is open the game gets pause { reason: 'overlay' }, then resume; on pause the SDK sends the save buffer immediately.
  • signed_in, same player.id — the first sign-in: the guest became an account in place. The pseudonym and saves stay the same, the game goes on without a restart; player in the reply is already kind: 'account'. The getToken() cache is dropped: the next token has kind: account.
  • signed_in, different player.id — the player signed in to an existing account (for example, from another device); the portal itself asks whether to move the guest's progress (the newer wins). Then the game gets playerChanging { reason: 'login' }, and in about 1.5 s the portal restarts it under the account. In this window send data only to your own server, not to saves.
  • already_signed_in — the player is already an account, no window was shown. cancelled — the player closed the window.
  • Errors: not_supported — no login capability (local/disabled modes, preview, isSupported('login') === false); unavailable — the portal has no sign-in methods right now or the API is unavailable; gesture_required, rate_limited, not_initialized, timeout (errors and limits).
  • The capability is isSupported('login'); the old reserved name authorize is its alias (it used to be always false).

Signing in and out restarts the game

Signing in from the portal header navigates to the provider's page: before that the portal pauses the game (pause with the overlay reason), the SDK sends unsaved data immediately, and the game restarts after the sign-in. If the player signs in or out in another tab while this tab with the game is open, the portal restarts the game too: the player has changed, and one player's progress must not go to another player's cloud. The game starts again with the new player's data — no special handling is needed. Only the first sign-in from the game itself (player.login() above) goes without a restart.

SDK 1.1: about 1.5 s before the restart the game receives the playerChanging event (reason: login, logout or switch) — send what you need to your own server in time (getToken returns the cached token in this window). Portal saves are no longer available in this window. The exception is a sign-in from the game to another account: after signed_in the token cache is already dropped, and the previous guest's token is gone in this window — send data with the session your server issued earlier.

A guest player is created on the first game launch; progress is stored on the server. If the browser blocks cookies, every page load is a new guest who cannot see the previous guest's progress: the cloud belongs to someone else, and the SDK save buffer on the game origin is bound to the player. A buffer written by another guest is neither returned to the new one nor sent — the SDK moves it to __ip:orphan:<key> (it lives only in this browser, for debugging the game). The game then starts from scratch — this is expected, not data loss: the previous guest's progress is intact on the server.