Use the verified user ID from your system as externalUserId. Calling again with the same ID returns the same Customer (created: false) and leaves its profile unchanged.
After your backend verifies the login, call createCustomerSession and return the complete session credential pair to that user.
The browser calls identify(session). The SDK uses the Access Token for business requests and the Refresh Token for automatic rotation.
On sign-out or account switch, the browser calls reset() and your backend calls revokeCustomerSessions.
server/session.tsTypeScript
1import { createLongplayServer } from"@tinkerlandai/longplay-sdk/server";23const longplay = createLongplayServer({4 baseUrl: process.env.LONGPLAY_API_URL!,5 apiKey: process.env.LONGPLAY_SERVER_KEY!,6 scope: { projectId: process.env.LONGPLAY_PROJECT_ID!, environment: "sandbox" },7});89// Call only after your own login check succeeds.10exportasyncfunctionissueCustomerSession(verifiedUserId: string) {11const { customer } = await longplay.getOrCreateCustomer({12 externalUserId: verifiedUserId,13 });14return longplay.createCustomerSession(customer.id);15}
Register your website origin under Integrations → Website origins in Console.
The issuing Key needs sessions:create. The token carries only these scopes, and only those the Key has: entitlements:read, credits:read, billing:read, billing:write, offers:resolve, funnels:run, features:read, events:write, content:read.
The SDK shares concurrent refreshes before the Access Token expires and never replays an already-sent business write. Use expiresAt and refreshExpiresAt; both are bounded by the original session and issuing Key. After a definitive refresh refusal, verify the user's login again before issuing a new session.
Generate a guestToken for the visitor, keep it in an HttpOnly cookie and exchange it through resumeGuestSession for a complete credential pair. Pass that pair to the browser's identify(session). After login, call claimGuestCustomer to bind the visitor to the account, then issue a regular Customer session.
guest.tsTypeScript
1import { createGuestToken, createLongplayServer } from"@tinkerlandai/longplay-sdk/server";23const longplay = createLongplayServer({4 baseUrl: process.env.LONGPLAY_API_URL!,5 apiKey: process.env.LONGPLAY_SERVER_KEY!,6 scope: { projectId: process.env.LONGPLAY_PROJECT_ID!, environment: "sandbox" },7});89// 1. First visit: create the guest token once and keep it in an HttpOnly cookie.10exportasyncfunctionstartGuest(savedGuestToken?: string) {11const guestToken = savedGuestToken ?? createGuestToken();12await longplay.createGuestCustomer({ guestToken });13const session = await longplay.resumeGuestSession({ guestToken });14return { guestToken, session };15}1617// 2. After your own login succeeds: bind the guest to the verified account.18exportasyncfunctionclaimGuest(input: {19 guestToken: string;20 verifiedUserId: string;21 requestId: string;22}) {23const { customer, switched } = await longplay.claimGuestCustomer({24 guestToken: input.guestToken,25 externalUserId: input.verifiedUserId,26 requestId: input.requestId,27 });28const session = await longplay.createCustomerSession(customer.id);29return { session, switched };30}
A guest token can read standalone content, run Funnels, read Features and send events: it carries only the issuing Key's content:read, funnels:run, features:read and events:write. Checking access, buying and redeeming offers require the user to sign in first.
The Key that issues guest tokens needs sessions:create plus at least one of those four scopes.
Use the returned expiresAt for the Guest session; it cannot outlive the guestToken's expiry (guestExpiresAt).