7 min read

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:

Text
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 GET and POST with content-type and x-teamshelf-session headers 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:

JSON
{ "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:

JSON
{ "error": "Your session expired; reconnecting", "code": "session_expired" }
StatusWhen
400Invalid 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.
401session_expired: the session is missing, invalid or expired. invalid_user_token: the user token was rejected.
403The request’s Origin isn’t allowed for this team.
404Unknown team key, or a bot that isn’t on the team.
409The team isn’t fully set up yet in the portal.
413Uploaded file is larger than 20 MB.
429Too many new sessions from one address.
502TeamShelf couldn’t reach or sign in to the bot service for this team.
500Unexpected server error.

Start a session

HTTP
POST /w/<team key>/session

Every visitor needs a session. An anonymous visitor sends the id it was given last time, if it has one:

JSON
{ "visitorId": "0b6c7f0e-4f3a-4b8e-9d1c-2a5e7c1f9b34" }

A signed-in user sends the token your backend signed instead:

JSON
{ "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:

JSON
{
  "token": "…",
  "expiresAt": 1795392000000,
  "kind": "anonymous",
  "visitorId": "0b6c7f0e-4f3a-4b8e-9d1c-2a5e7c1f9b34"
}
FieldMeaning
tokenThe session token. Send it with every other call.
expiresAtWhen the session expires, in milliseconds since the epoch — 12 hours from now.
kindanonymous or identified.
visitorIdThe anonymous id to keep and send next time. null for signed-in users.

Errors:

StatuscodeWhen
401invalid_user_tokenThe user token’s signature, algorithm or claims were rejected, or it expired.
400identity_disabledA user token was sent, but the team has no identity secret.
429One 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:

HTTP
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

HTTP
GET /w/<team key>/team

Returns 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.

JSON
{
  "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

HTTP
GET /w/<team key>/events

A 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

HTTP
GET /w/<team key>/bots/<botId>/messages?before=<cursor>

Returns the newest page of the visitor’s conversation with a bot, oldest first:

JSON
{ "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

HTTP
POST /w/<team key>/bots/<botId>/send
JSON
{
  "text": "@Warehouse can you check the pick logs?",
  "mentionBotIds": ["b_wh"],
  "artifactIds": [],
  "clientNonce": "ts-8f0c…"
}
FieldRules
textUp to 100,000 characters.
mentionBotIdsUp to 64 bot IDs. IDs not on the team, or the recipient itself, are ignored.
artifactIdsUp to 20 IDs from uploads.
clientNonceOptional, 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:

JSON
{ "runId": "r_…", "seq": 42 }

The reply arrives on the event stream.

Upload a file

HTTP
POST /w/<team key>/bots/<botId>/artifacts
Content-Type: multipart/form-data

Send 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:

JSON
{ "id": "a_…", "name": "invoice.pdf", "mimeType": "application/pdf" }

Download a file

HTTP
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

HTTP
POST /w/<team key>/bots/<botId>/answer
JSON
{ "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 }:

EndpointWhat it does
POST /w/<team key>/bots/<botId>/readMarks the bot’s replies read for this visitor. Clears its new-reply dot for them only.
POST /w/<team key>/bots/<botId>/clearEmpties this visitor’s conversation with the bot. Other visitors aren’t affected.
POST /w/<team key>/bots/<botId>/stopStops the bot’s current run for this visitor.