Install
#npm install @playcode/sdk
The package on npm: @playcode/sdk. It needs Node.js 22 or newer. It uses web-standard APIs only and imports no Node.js module, so it also bundles for Bun and edge runtimes. It ships ESM and CommonJS builds.
Quick start
#import { Playcode } from '@playcode/sdk'
// The client reads PLAYCODE_SECRET_KEY when a call needs it.
const playcode = new Playcode()
// The models your app can call, live from Playcode.
const models = await playcode.ai.models()
if (models.length === 0) throw new Error('Switch on AI for this app first')
console.log(models.map((card) => card.id))
const { text, usage } = await playcode.ai.generate({
model: models[0].id,
prompt: 'Write a one-line welcome for a bakery website.',
})
console.log(text, `${usage.credits} credits`)
Inside a Cloud app, PLAYCODE_SECRET_KEY and PLAYCODE_AI_URL are in the app's environment once AI is on for the app. If they are missing, ask Playcode's agent to switch on AI: see Add AI to your app. The first model of the list is fine for a first call. To pick one for a feature, see Choosing a model.
What you can build
#| You want | Use | Read |
|---|---|---|
| A draft, a summary, a translation, a label | playcode.ai.generate() |
Call AI models |
| Typed fields from a photo or a PDF: an invoice, a receipt, a form | generate() with files and schema |
Images and PDFs, an example |
| An answer that shows as the model writes it | playcode.ai.stream() |
Streaming |
| An assistant that works in your app: finds orders, books slots, files tickets | playcode.ai.agent() with your functions as tools |
Build an agent |
| An agent that works through documents or records: finds, reads, updates | File-style tools over your own data | A file system over your data |
| An action that waits for a person to approve it | approval on a tool |
Approvals |
| Your app's features for its pages, an assistant and MCP clients, each change on a person's yes | @playcode/sdk/operations |
Your app's operations |
| Rules at every step: which tools, what the model reads, what it may do | hooks, budgets, fallback models, sub-agents | Agent controls |
| A chat your users talk to, each with their own conversations | agent.handler() and useAgentChat() |
Build a chat with the SDK |
| Work that survives a restart: a long run, a nightly batch | agent.start() and agent.recover() |
Durable runs |
| Tests that need no network and no credits | @playcode/sdk/testing |
Testing with the SDK |
| Email, video, the Playcode API and webhooks | playcode.email, playcode.media, playcode.api, verifyWebhook() |
Email, video, the API and webhooks |
Every use case with its code: SDK examples. Every option, event, error and default: SDK reference.
What an agent does by default
#An agent built with the SDK has these safeguards on, with no code of yours:
- Cheap steps. Each step reads the repeated part of the prompt at the model's cache price: the SDK marks it on Claude models, and GPT and Gemini models cache it on their own.
- Long conversations. As a conversation nears 160,000 tokens, or the model's smaller window, old tool results are cleared. If that is not enough, a brief, a short summary the model writes, stands in for the older messages. A call that still does not fit is not sent: the run ends with
finish: "context-limit". - Brakes. A run that repeats the same tool calls, or keeps calling a tool that fails, is told to change course, then stopped. A run that repeats its own text is stopped at once.
- Tool limits. After 60 seconds, the model reads that a tool call timed out, and the call's
signalaborts. The model reads at most 50,000 characters of a result. - Safe retries. The SDK retries a call only when Playcode shows that nothing was charged.
- Cost you can see. Every result has
usage.credits, and every call can carry your end user's id.
The first four can be tuned or turned off: Agent controls.
In a Cloud app
#A new Cloud app has the SDK wired into its backend:
AiServiceinbackend-nest/src/ai/isplaycode.ai, with one log line per model call.AiAgentskeeps each agent's conversations and runs in the app's database, so they survive a restart.- The
/chatpage talks to an example agent throughuseAgentChat.
The app's own docs/08-ai.md explains each part. You can also ask Playcode's agent to build the feature: Add AI to your app and Add an AI chat to your app.
Keys
#| Key | Environment variable | For |
|---|---|---|
The app's secret key, pcsk_... |
PLAYCODE_SECRET_KEY |
AI, email and video. A Cloud app has it in its environment |
An access token, pc_live_... |
PLAYCODE_TOKEN |
playcode.api, the Playcode API. See Access tokens and permissions |
The browser entries, @playcode/sdk/react and @playcode/sdk/client, call your own endpoint and never hold a key.
Creating the client never throws and never calls the network. A missing key fails on the first call that needs it, with a PlaycodeConfigError that names it.
Versions
#The SDK is in 0.x, so a minor version can change behavior. The package's CHANGELOG.md says what changed in each version and what to do when you upgrade: read its Changed items first. The installed version's copy is node_modules/@playcode/sdk/CHANGELOG.md.
Limits
#- The SDK names no AI model. Which models exist, what each reads and what it costs come from
playcode.ai.models()at run time. - Every AI call is paid from your workspace's credits. It stops when the credits run out, or at the app's daily AI limit or monthly AI budget if you set one.
- One AI request carries at most 32 MiB, its files included.
- Image generation from the app's own key is not available yet.
- An operation added to the Playcode API after the SDK's release needs a newer SDK.