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

Call the model

Once a user is signed in, your app talks to an OpenAI-compatible API with their access token. There is no SDK to install: any OpenAI client works, in any language.

Call it with an OpenAI client

Give the client the base URL https://api.plangate.dev/<app>/v1 and the user's access token as its API key:

import OpenAI from "openai"

const ai = new OpenAI({
  baseURL: "https://api.plangate.dev/my-agent/v1",
  apiKey: session.access_token,
  dangerouslyAllowBrowser: true, // the token is the user's own, not your provider key
})
const stream = await ai.chat.completions.create({ model: "my-model", messages, stream: true })
for await (const chunk of stream) render(chunk.choices[0]?.delta?.content ?? "")

model is optional: left out, or naming a model your app doesn't sell, the user gets their plan's first model.

Call it without a client

POST https://api.plangate.dev/<app>/v1/chat/completions
Authorization: Bearer <access token>
Content-Type: application/json

{"messages": [{"role": "user", "content": "Hello"}], "stream": true}

React Native's fetch doesn't stream: ask without stream, or use expo/fetch.

Keep your system prompt on the server

Anything your app sends can be read in the browser's devtools or a decompiled app. To keep a system prompt out of the app:

  1. In the panel, open your app's Settings → AI and models, then Presets, and save the prompt under a name, such as support-v3, with the model it runs on.

  2. In the app, call the preset by its name, with an @:

    const answer = await ai.chat.completions.create({ model: "@support-v3", messages, tools })
    

PlanGate puts the preset's prompt first, in place of any system or developer message the app sends. Tools work as usual: the app declares them and runs the tool calls.

A preset without a prompt is a plain model alias: change its model in the panel, and every app already shipped follows.

A preset doesn't hide the tools' names and schemas, and it can't stop a user from asking the model to repeat its instructions: write the prompt with that in mind.

Handle a refused call

Answer What to do
401 unauthorized Renew the session
402 subscription_required The user has no plan: show your plans
429 rate_limit_5h, rate_limit_week The budget of that window is spent: say when it comes back
402 free_tier_full, 429 free_daily_limit The free plan has nothing left for today
403 model_not_in_plan Your app sells that model, but not in the user's plan: offer a plan that includes it
404 model_not_found @<name> names no preset of your app: check its name in the panel

Every error is listed in the API reference.

❌🕸️