Skip to content
Longplay
Docs
Browse docs / Quickstart

Quickstart

Install the SDK, create a Customer, check access and report usage.

Markdown
API v1Updated

Before you begin#

  • Select an App for integration checks in the production Console (console.longplayai.com) and create a Sandbox Server Key with customers:write, entitlements:read and credits:consume. Sandbox data also belongs to production.
  • A metered Feature created under Features in Console; copy its ID.
  • A configured free default Product that includes the Feature, under Commerce → Catalog → Products in Console. New Customers receive it automatically.
.env.localShell
LONGPLAY_API_URL=https://api.longplayai.comLONGPLAY_SERVER_KEY=<SERVER_KEY>LONGPLAY_PROJECT_ID=<APP_UUID>LONGPLAY_FEATURE_ID=<FEATURE_UUID>

Keep configuration in an ignored backend environment file. Give the free Product enough allowance for at least one check and consumption; never expose the Server Key in browser-public variables.

Related: Create a Server Key

Install the SDK#

npmShell
npm install --save-exact @tinkerlandai/longplay-sdk@latest

Create a Customer, check access, report usage#

Save the program below in your backend project and run it.

quickstart.tsTypeScript
import { createLongplayServer } from "@tinkerlandai/longplay-sdk/server"; const longplay = createLongplayServer({  baseUrl: process.env.LONGPLAY_API_URL!,  apiKey: process.env.LONGPLAY_SERVER_KEY!,  scope: { projectId: process.env.LONGPLAY_PROJECT_ID!, environment: "sandbox" },});const featureId = process.env.LONGPLAY_FEATURE_ID!; async function main() {  // 1. Find or create the Customer for your signed-in user.  const { customer } = await longplay.getOrCreateCustomer({ externalUserId: "user-42" });   // 2. Check access before doing the work.  const { check } = await longplay.checkEntitlement({    customerId: customer.id,    requirement: { internal_feature_id: featureId, quantity: "1", properties: {} },  });  if (!check.allowed) return console.log("blocked:", check.reason);   // 3. Report usage after the work succeeds. Use your own event ID in production.  const { receipts } = await longplay.trackUsage({    customerId: customer.id,    command: { internal_feature_id: featureId, quantity: "1", idempotencyKey: crypto.randomUUID() },  });  console.log("recorded:", receipts[0]?.accepted);} main().catch((error) => {  console.error(error);  process.exitCode = 1;});
terminalShell
node --env-file=.env.local --experimental-strip-types quickstart.ts

The command requires Node.js 22.6+. For Python/Go, load the same environment variables using your project's environment loader, then run python quickstart.py / go run . Use authenticated user IDs in the real application.

Check the result#

Success prints recorded: true. Running it again returns the same Customer and records another usage.

Also check the Customer ID, App/Sandbox response scope, check.allowed and usage receipt. The example generates a new usage ID on every run; persist and reuse the original idempotency key when recovering the same business action instead of rerunning it as a new debit.

Troubleshooting
SymptomFix
blocked: insufficient_balanceThe Customer has no available balance for this Feature. Check the default Product's configuration, that it includes the Feature, and that it was assigned to this Customer. Default Products are not reassigned when an existing Customer signs in again.
401 UNAUTHENTICATEDThe Key is incomplete, expired or revoked.
403 FORBIDDENThe Key lacks a scope listed above.
403 SCOPE_MISMATCHLONGPLAY_PROJECT_ID or environment does not match the Key's App and environment.
An error before any request is sentThe SDK validates input locally: for example, when LONGPLAY_FEATURE_ID is not a UUID, TypeScript throws a validation error (ZodError) and Python and Go raise a local INVALID_REQUEST with no HTTP status.

Troubleshooting

  • blocked: insufficient_balance

    Fix
    The Customer has no available balance for this Feature. Check the default Product's configuration, that it includes the Feature, and that it was assigned to this Customer. Default Products are not reassigned when an existing Customer signs in again.
  • 401 UNAUTHENTICATED

    Fix
    The Key is incomplete, expired or revoked.
  • 403 FORBIDDEN

    Fix
    The Key lacks a scope listed above.
  • 403 SCOPE_MISMATCH

    Fix
    LONGPLAY_PROJECT_ID or environment does not match the Key's App and environment.
  • An error before any request is sent

    Fix
    The SDK validates input locally: for example, when LONGPLAY_FEATURE_ID is not a UUID, TypeScript throws a validation error (ZodError) and Python and Go raise a local INVALID_REQUEST with no HTTP status.

Next steps#