Skip to main content
This guide builds an agent from scratch through the API alone — no flow-building in the Agent Studio UI. By the end you’ll have created an agent, given it a behavior and a knowledge base topic, held a test conversation with it, and promoted it to production. Every step below has a curl and a Python tab. The Python snippets run top to bottom as one script — each reuses variables (agent_id, branch_id, …) set by the step before — so copy them in order. For an overview of the API families, see the API overview. This page uses the Agents API to build and ship, and the Debug Chat API to test — the same API key and host work for both.

What you’ll build

1. Create

Stand up a new agent with a greeting.

2. Configure

Branch, add a behavior and a knowledge base topic, then merge.

3. Test

Hold a live conversation with it in Sandbox.

4. Deploy

Promote through Pre-release into Live traffic.

5. Observe

Pull back the conversations it has.

Prerequisites

1

Get access to a PolyAI workspace

You build agents inside a PolyAI workspace, so you need access to one first.
  • Enterprise customers — PolyAI provisions your workspace during onboarding; your PolyAI representative sets it up and grants you access. Enterprise workspaces are region-specific (US, UK, or EU).
  • Getting started via the website — sign up at poly.ai to create a self-serve workspace, which lives in the Studio region.
2

Get an API key

Create a workspace-scoped API key from the API Keys tab on your workspace homepage in Agent Studio (see API keys). Copy the value when it’s shown — the full key only appears once. The same key authenticates the Agents API and the Data API’s debug chat — everything through step 4 of this guide.
  • Step 5 (pulling call data back out) uses the Conversations v3 API, which needs a separate project-scoped key. The Chat API needs its own connector token too. Request either only once you’re integrating a real channel or data pipeline — you don’t need them for steps 1–4.
Treat the key like a password. Don’t commit it or put it in client-side code.
3

Find your account ID

Open Agent Studio. Your account ID is the first path segment in the URL:
For example, https://studio.uk.poly.ai/acme-uk/acme-team-4/agentaccount_id=acme-uk.
“Account ID” and “Workspace ID” are the same thing. Agent Studio’s UI calls this the Workspace ID and shows it in a prefixed form (ws-xxxxxxxx). The API parameter is named accountId (Agents and Data APIs) or account_id (Conversations, Chat, Webhooks, and most other APIs) — same value, different casing convention depending on which API family you’re calling. Both the slug form from the URL (acme-uk) and the prefixed form (ws-xxxxxxxx) work in API calls.
4

Pick the right base URL

The Agents and Data APIs — everything in steps 1–4 of this guide — share one regional host family:
RegionBase URL
UShttps://api.us.poly.ai
UKhttps://api.uk.poly.ai
EUhttps://api.eu.poly.ai
Studiohttps://api.studio.poly.ai
The Conversations v3 API (used in step 5) is on a different host — api.{region}-1.platform.polyai.app, with a -1 suffix. Mixing these up is the most common cause of 404s. See base URLs for the full table across every API family.
5

Set environment variables

All examples below assume these are set.

Step 1: Create an agent

Response
Save agentId — every remaining call in this guide uses it. The agent starts with one branch, main.

Step 2: Configure it

You can’t edit main directly — the API rejects writes to it with 422 Cannot directly update main branch. Instead, create a working branch, make your edits there, then merge it back into main. This is the same branch-and-merge model Agent Studio and Studio Assistant use. Create a working branch:
The response returns the branch’s ID — save it, every edit below targets it:

Set the behavior

The behavior is the system prompt that governs how the agent responds.

Add a knowledge base topic

Topics are what the agent draws on to answer questions — each one pairs content with example queries that should trigger it.
See Knowledge base for the full schema, including actions and isActive.

Merge the branch into main

Merging applies the branch’s edits to main and publishes them to Sandbox in one step — there’s no separate publish call.

Step 3: Test it

Your merge in step 2 already published to Sandbox, so the agent is live there and ready to talk. Hold a test conversation using the Debug Chat API — it authenticates with the same key and host as the Agents API, so there’s no extra credential to request.
Edits reach an environment only when you merge or promote into it. If debug chat replies with stale behavior, you edited the branch but haven’t merged it to main. Merge again to push the change into Sandbox.
Prefer a UI? Agent Studio has a built-in Test panel that talks to a branch directly, no merge required — see Test your agent.
Start a session:
(metadata has more fields than shown — trimmed here for readability. The conversationId is an AS_CHAT_… string; the curl examples below hard-code the one from this response — swap in your own.) Send a message — try the knowledge base topic you just added:
The agent answers from the topic you added:
To confirm the topic was actually used, check metadata.retrievedTopics — it lists the knowledge base topics the agent pulled in (["Password reset"] here), a more reliable signal than scanning the reply text for acme.com/reset. Keep sending messages against the same conversationId to continue the conversation, matching clientEnv to whichever environment you’re checking.
Building a real webchat, SMS, or in-app integration instead of a one-off test? Use the Chat API — it’s built for driving conversations from an end-user-facing client and requires its own connector token.

Step 4: Deploy it

Sandbox is for testing, not customer traffic. Promote the Sandbox deployment through Pre-release and into Live — that’s what real callers hit. See Environments for the full model. Promotion works on a deployment ID, and the merge in step 2 didn’t return one — so fetch the active Sandbox deployment first:
Then promote it up the chain. Each promote returns a new deployment under deployment.id for the target environment — feed that into the next call:
Omit targetEnvironment and the promote defaults to the next stage in sequence (sandbox → pre-release → live). A Sandbox deployment can also promote straight to live — going through Pre-release first is a safety choice, not an API requirement. Made a mistake? Roll back to the previous deployment in any environment.

Step 5: Work with the calls it takes

Once your agent is live (or you’ve made test calls), pull the data back with the Conversations API. This uses a different host and a project-scoped key — see prerequisites above.
The project_id this API expects is the same value as the agentId you’ve used throughout this guide — “Agent” and “Project” are the current and legacy names for the same resource.
This is the one step on the -1.platform.polyai.app host with its own project-scoped key, so set that key first:
This returns a conversations array and a cursor for pagination. Fetch one by ID to get its full turn-by-turn transcript:
From here:

Download call audio

Voice calls have a recording available on a separate binary endpoint.

Subscribe to webhooks

Get a signed POST when a call completes instead of polling for it.

Clean up

Built this agent just to try the API? Delete it when you’re done — this removes the agent along with all its branches and deployments, and can’t be undone.
A successful delete returns 204 No Content.

Troubleshooting

SymptomLikely causeFix
401 UnauthorizedMissing or wrong x-api-key, or a key from the wrong region.Confirm the key was issued for the region in your base URL — see region mismatches.
403 ForbiddenKey lacks permission for this account or agent.Confirm the key was provisioned for the account ID / agent ID you’re using.
400 on create agentMissing responseSettings.greeting, which is required.Include a non-empty greeting — it’s the agent’s opening line.
422 Cannot directly update main branchYou sent a behavior or knowledge base write to branches/main.Create a working branch, make edits on it, then merge to main — see step 2. main is read-only.
Debug chat replies with the old behavior/topicYou edited a branch but haven’t merged it into main yet.Merge the branch again — edits reach Sandbox only once they’re merged into main.
404 on debug-chat messageWrong or expired conversationId.Start a new session with POST /debug-chat and use the returned conversationId.
409 Deployments already publishedYou called publish after a merge, which already deployed to Sandbox.Skip the explicit publish — merging a branch publishes to Sandbox for you. Move on to promote.
404 on promoteWrong deploymentId.Use the deployment.id from the most recent publish/promote response, not an old one.
400 on promotetargetEnvironment isn’t a later stage than the deployment’s current one.Sandbox can promote to pre-release or live; pre-release can only promote to live.
404 on Conversations endpoint (step 5)Wrong base URL — usually the build host (api.us.poly.ai) instead of the platform host.Conversations v3 uses api.{region}-1.platform.polyai.app, with the -1 suffix.
Empty conversations arrayNo calls yet in the time window, or wrong client_env.Place a test call via debug chat, widen the window, or try client_env=sandbox.
429 Too Many RequestsRate limit hit.Back off per the Retry-After header; use cursor pagination for large pulls.
See Error codes for the full reference.

Next steps

Agents API

Branches, telephony, real-time configs, and variants for multi-site agents.

Chat API

Wire a real webchat, web SDK, or SMS integration into a live conversation.

Conversations API

Full schema, pagination, and retrieval modes for call data.

Webhooks API

Event types, retries, and signature verification.

Outbound Calling API

Have your new agent place a real call out — needs outbound enabled on the project first.

WebRTC Gateway

Talk to your agent by voice from a browser tab instead of typing.
Last modified on July 13, 2026