Widget API
The HTTP API the embed uses — sessions, team, live events, messages, files and actions — under app.teamshelf.ai/w/<team key>.
The embed is built on a small HTTP API. Most sites never call it directly — the script tag does everything — but it’s documented here so you know exactly what the widget does, and so you can build on it.
Basics
Every endpoint lives under your team’s key:
https://app.teamshelf.ai/w/<team key>/…- No API keys. Access is controlled by the team key plus the team’s allowed origins. Browser requests from other origins get
403. - Sessions. Everything happens on behalf of one visitor. Start a session first, then send its token with every other call.
- JSON in, JSON out, except the event stream (SSE), file uploads (multipart) and file downloads (raw bytes).
- CORS allows
GETandPOSTwithcontent-typeandx-teamshelf-sessionheaders from allowed origins. - Bot IDs come from the team response. A bot that isn’t on the team returns
404.
Errors
Errors are JSON with a human-readable message:
{ "error": "That bot isn't part of this team" }Errors you may need to handle in code also carry a code. Check the code, not the message:
{ "error": "Your session expired; reconnecting", "code": "session_expired" }| Status | When |
|---|---|
400 | Invalid input, or the request was rejected by the bot service. identity_disabled when a user token is sent to a team without an identity secret. |
401 | session_expired: the session is missing, invalid or expired. invalid_user_token: the user token was rejected. |
403 | The request’s Origin isn’t allowed for this team. |
404 | Unknown team key, or a bot that isn’t on the team. |
409 | The team isn’t fully set up yet in the portal. |
413 | Uploaded file is larger than 20 MB. |
429 | Too many new sessions from one address. |
502 | TeamShelf couldn’t reach or sign in to the bot service for this team. |
500 | Unexpected server error. |
Start a session
POST /w/<team key>/sessionEvery visitor needs a session. An anonymous visitor sends the id it was given last time, if it has one:
{ "visitorId": "0b6c7f0e-4f3a-4b8e-9d1c-2a5e7c1f9b34" }A signed-in user sends the token your backend signed instead:
{ "userToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9…" }Both fields are optional. With neither, or with a missing or malformed visitorId (it must be a UUID v4), you get a new anonymous visitor. The response:
{
"token": "…",
"expiresAt": 1795392000000,
"kind": "anonymous",
"visitorId": "0b6c7f0e-4f3a-4b8e-9d1c-2a5e7c1f9b34"
}| Field | Meaning |
|---|---|
token | The session token. Send it with every other call. |
expiresAt | When the session expires, in milliseconds since the epoch — 12 hours from now. |
kind | anonymous or identified. |
visitorId | The anonymous id to keep and send next time. null for signed-in users. |
Errors:
| Status | code | When |
|---|---|---|
401 | invalid_user_token | The user token’s signature, algorithm or claims were rejected, or it expired. |
400 | identity_disabled | A user token was sent, but the team has no identity secret. |
429 | One address started more than 30 sessions in a minute. Try again shortly. |
Using the session
Send the token on every other call in the x-teamshelf-session header:
GET /w/<team key>/bots/b_ops/messages
x-teamshelf-session: <token>Where you can’t set headers — EventSource and file links — pass it as a query parameter instead: ?session=<token>.
A missing, invalid or expired session gets 401 with the code session_expired. Start a new session and retry. A session only ever reaches its own team and visitor.
Get team
GET /w/<team key>/teamReturns the team as the shelf renders it: the team name, the primary bot, every bot with its look and live status, the sections, and the bots in no section.
This is the one call that works without a session. Without one, every bot is idle. With one, the statuses are that visitor’s.
{
"teamName": "Logistics Ops",
"primaryBotId": "b_ops",
"bots": [
{
"id": "b_ops",
"name": "Ops Lead",
"initials": "OL",
"color": "#EDE9FE",
"tile": "#4C4570",
"accent": "#A78BFA",
"icon": null,
"description": "Takes requests, delegates, summarizes",
"sectionId": null,
"isPrimary": true,
"status": { "botId": "b_ops", "state": "idle", "runStatus": "idle", "line": "Idle", "lineIsActivity": false, "quiet": true }
}
],
"sections": [{ "id": "s_track", "name": "Tracking", "botIds": ["b_carrier", "b_wh"] }],
"ungroupedBotIds": [],
"manageUrl": "https://app.teamshelf.ai/portal/#/teams/…"
}icon is a data: URL when the bot has a custom icon. For the status fields, see Events.
Live events
GET /w/<team key>/eventsA Server-Sent Events stream of the visitor’s team, status, message and live-reply events. EventSource can’t set headers, so pass the session as ?session=<token>. See Events.
List messages
GET /w/<team key>/bots/<botId>/messages?before=<cursor>Returns the newest page of the visitor’s conversation with a bot, oldest first:
{ "messages": [ { "id": "m_1", "seq": 41, "role": "user", "blocks": [{ "kind": "text", "text": "Where's SH-20930?" }], "createdAt": "…" } ], "olderCursor": 12 }To load older messages, pass the previous response’s olderCursor as before. olderCursor is null when there’s nothing older. Messages are made of blocks — text, images, files, question cards, bot-to-bot handoffs and more.
Send a message
POST /w/<team key>/bots/<botId>/send{
"text": "@Warehouse can you check the pick logs?",
"mentionBotIds": ["b_wh"],
"artifactIds": [],
"clientNonce": "ts-8f0c…"
}| Field | Rules |
|---|---|
text | Up to 100,000 characters. |
mentionBotIds | Up to 64 bot IDs. IDs not on the team, or the recipient itself, are ignored. |
artifactIds | Up to 20 IDs from uploads. |
clientNonce | Optional, up to 200 characters. Use a fresh value per send to make retries safe. |
You need text, at least one attachment, or both. The response identifies the run that started:
{ "runId": "r_…", "seq": 42 }The reply arrives on the event stream.
Upload a file
POST /w/<team key>/bots/<botId>/artifacts
Content-Type: multipart/form-dataSend the file in a field named file, up to 20 MB. The file is private to the visitor who uploaded it. The response (201) has the ID to pass in artifactIds:
{ "id": "a_…", "name": "invoice.pdf", "mimeType": "application/pdf" }Download a file
GET /w/<team key>/bots/<botId>/artifacts/<artifactId>Returns the file’s bytes. For links and <img> tags, pass the session as ?session=<token>. A visitor can only download files from their own conversations. PNG, JPEG, GIF and WebP images are served inline so they can be shown in an <img>; everything else downloads as application/octet-stream. See Security.
Answer a card
POST /w/<team key>/bots/<botId>/answer{ "runId": "r_…", "messageId": "m_…", "answer": "allow" }Answers a question or approval card a bot posted (an ask block). For approval cards, answer is the ID of the chosen action, such as allow, always or deny. For questions, it’s the reply text (up to 10,000 characters). Returns { "ok": true }.
Actions
Each returns { "ok": true }:
| Endpoint | What it does |
|---|---|
POST /w/<team key>/bots/<botId>/read | Marks the bot’s replies read for this visitor. Clears its new-reply dot for them only. |
POST /w/<team key>/bots/<botId>/clear | Empties this visitor’s conversation with the bot. Other visitors aren’t affected. |
POST /w/<team key>/bots/<botId>/stop | Stops the bot’s current run for this visitor. |