PlanGate
Type to search every docs page
↑ ↓ to move↵ to openesc to close
DocsSign in

API reference

Base URL: https://api.plangate.dev. <app> is your app's short name. Bodies are JSON. Calls marked 🔒 send the user's access token: Authorization: Bearer <access token>. Errors have OpenAI's shape: {"error": {"message": "…", "type": "…"}}.

Every call and every answer is listed here. For a first run, see Getting started; for step-by-step tasks, the guides, such as Sign users in.

Sessions

Method Path What
POST /apps/<app>/sign-in {"token": "<ID token>"} → a session of that user. For apps that sign their users in themselves
POST /apps/<app>/server-sign-in From your backend, the app's server key as bearer token: {"user_id": "42", "email"?, "name"?} → a session of that user (server key). A wrong or removed key: 401
POST /auth/refresh {"refresh_token": "…"} → a new session. A refresh token works once
POST /auth/sign-out 🔒 Ends the session

A session is {"access_token": "…", "refresh_token": "…", "expires_in": 3600}: the access token lasts an hour, the refresh token 90 days. A session belongs to one user of one app: it is refused on any other app.

ID tokens accepted on /sign-in

Set in the panel (Settings → Sign-in, with My app signs them in chosen). An app can trust several issuers.

Provider Issuer (iss) Audience (aud)
Google (Android's Credential Manager, Google Sign-In, Google Identity Services) https://accounts.google.com Your web OAuth client ID (Android's serverClientId; on iOS set it as serverClientID, or add the iOS client ID too)
Sign in with Apple https://appleid.apple.com Your bundle ID (your Services ID on the web)
Another provider, or your backend The one you give The one you give

A token must be a JWT signed with RS256 or ES256 by a key of its issuer (its JWKS, or the public key you pasted), name one of your audiences, and not be expired (exp is required). The user is its sub, or whoever already has its verified email in your app; email is kept only with email_verified: true (from Google, only for a Gmail or Workspace address: another may have changed hands since Google checked it), name when present.

Sign in with PlanGate

For apps without a sign-in of their own, on any platform. Open in a browser:

https://account.plangate.dev/connect?app=<app>&return=<return url>&state=<random>

The browser comes back to the return URL with access_token, refresh_token, expires_in and state: in the fragment for an address under the app's https Return link (a web app), in the query for a loopback address (http://127.0.0.1:<port>/…) or the app's own URL scheme.

The model

Method Path What
POST /<app>/v1/chat/completions 🔒 OpenAI Chat Completions, streaming included
GET /<app>/v1/models Models sold, OpenAI format
GET /apps/<app>/models Models sold, with the USD price per million tokens billed

Any OpenAI client works with the base URL https://api.plangate.dev/<app>/v1 and the access token as its API key. A missing model, or one your app doesn't sell, is served by the user's plan's first model. A model your app sells but their plan doesn't include is a 403 model_not_in_plan. max_tokens is lowered to what the remaining budget can pay.

model: "@<name>" calls a preset set in the panel: its model, with its system prompt (if any) in place of the request's system and developer messages. An unknown preset is a 404 model_not_found, never the plan's first model.

The user and their plan

Method Path What
GET /apps/<app> The app's public name and branding
GET /apps/<app>/me 🔒 The user, their quota, the plans on sale
POST /apps/<app>/plans/<plan>/switch 🔒 Moves their subscription to another plan; answers like /me
POST /apps/<app>/purchases 🔒 Links a store purchase to the user; answers like /me
GET /apps/<app>/microsoft-store-ticket 🔒 {"service_ticket": "…"}, for StoreContext.GetCustomerPurchaseIdAsync with the user's id as publisherUserId

/me answers:

{
  "user": {"id": "…", "email": "[email protected]", "name": "Ada"},
  "app": {"name": "My Agent", "slug": "my-agent", "models": ["model-a", "model-b"]},
  "quota": {
    "plan_name": "Pro",
    "budget_5h_usd": 0.7, "used_5h_usd": 0.12,
    "budget_week_usd": 3.5, "used_week_usd": 1.4,
    "portal_url": "https://…",
    "ends_at": null,
    "pending_plan_id": null, "pending_plan_name": null, "pending_at": null,
    "free": false, "free_full": false, "resets_at": null
  },
  "plans": [
    {
      "id": "…", "name": "Pro", "price_usd": 20.0, "price_label": "$20.00 / month",
      "budget_5h_usd": 0.7, "budget_week_usd": 3.5,
      "checkout_url": "https://…", "subscribed": true,
      "store_products": {"google_play": "pro_monthly"},
      "models": ["model-a", "model-b"]
    }
  ],
  "plans_url": "https://…"
}
  • user.email is null for a user whose ID token carried no verified email.
  • quota is null without an active plan. Budgets are per rolling 5 hours and per rolling 7 days. ends_at is set when the subscription is cancelled: it serves until then. portal_url opens the payment provider's customer portal, signed in (invoices, payment method, cancelling).
  • On an app with a free plan, a user without a paid one gets a quota with free: true: budget_week_usd (and budget_5h_usd, the same) is their share of today and used_week_usd what they used today, both starting again at resets_at (the next midnight UTC). free_full: true means every free place of the day is taken: nothing until resets_at, or a paid plan. plans lists the paid plans only.
  • checkout_url opens the plan's checkout in a browser; null when the plan is sold in stores only. Add &return=<url> (URL-encoded) to bring the user back once paid: your app's URL scheme, a loopback address, or an address under the Return link set in the panel.
  • plans_url opens the user's plan page on PlanGate, under your brand; it takes &return=<url> too. For an app with its own sign-in the link signs the user in there and lasts a day.

When a user switches plans, a plan as dear or dearer applies right away, the prorated difference charged now. A cheaper one waits for the renewal, with no refund: until then quota.pending_plan_id, pending_plan_name and pending_at say what is scheduled. Switching to the current plan drops a scheduled change.

Store purchases

POST /apps/<app>/purchases takes the purchase with the fields of its store:

Store Body
Google Play {"channel": "google_play", "product_id": "…", "purchase_token": "…"}
App Store {"channel": "app_store", "signed_transaction": "<the transaction's JWS>"}
Microsoft Store {"channel": "microsoft_store", "store_id_key": "…", "product_store_id": "…"}

Attach the user's id (user.id from /me) to the purchase in the store's own API before buying (Play Billing setObfuscatedAccountId, StoreKit appAccountToken, Microsoft Store publisherUserId): a purchase made for another user is refused, and a purchase is linked to one user only. An App Store purchase without appAccountToken (an offer code redeemed in the App Store, a subscription older than your app's use of PlanGate, a family member's share) is taken when signed_transaction is the transaction exactly as StoreKit gave it, signed by Apple; PlanGate then writes the user's id on it at Apple.

Test purchases (TestFlight and App Review, Play license testers) give a plan, unless the app turned them off for that store in the panel: they are then refused (test_purchase_refused), and those already taken stop serving.

Set-up steps for each store are in Sell in app stores.

Webhooks

PlanGate also calls your server: a signed POST for each subscription change, exhausted quota and new user, to the address set in the panel. The events, their JSON and how to verify them are in Webhooks.

Errors

Status Type Meaning
401 unauthorized No token, an expired one, or an ID token your app doesn't accept: refresh, or sign in again
400 own_login_off /sign-in on an app that signs its users in with PlanGate: switch it to My app signs them in in the panel
502 login_keys_unreachable Your identity provider's keys (JWKS) can't be read right now
402 subscription_required The user has no active plan: show /me's plans
429 rate_limit_5h, rate_limit_week The budget of that window is spent
402 free_tier_full The free plan has no place left today: error.checkout_url opens the paid plans, error.resets_at says when to try again
429 free_daily_limit The user's free share of today is used: it starts again at error.resets_at
400 already_subscribed A second checkout for a user who has a plan: switch instead
400 switch_in_store The subscription is billed by a store: change plans from the app
400 purchase_not_found, purchase_mismatch The store doesn't know this purchase, or it is for another product
400 purchase_not_bound An App Store purchase without appAccountToken, sent other than as Apple signed it
400 purchase_local A purchase from Xcode's local StoreKit testing: test in the sandbox or TestFlight
400 test_purchase_refused A test purchase, and the app turned them off for this store
400 purchase_of_another_account, purchase_already_linked The purchase belongs to another user
400 purchase_key_expired The Microsoft Store ID key expired or was revoked: make a new one in the app and send it
503 store_not_configured, store_not_supported The app isn't connected to this store, or PlanGate doesn't sell through it
402 developer_plan_expired The app's developer has to renew PlanGate
503 app_not_configured No key or no model yet
404 model_not_found model: "@<name>" names no preset of this app
403 model_not_in_plan The user's plan doesn't include that model, or the preset's
❌🕸️