Skip to the content

Chat API

Let your own software talk to an assistant with the OpenAI Chat Completions format. The assistant answers with its own knowledge, tools, procedures and guardrails, and every conversation shows in your dashboard.

Where
Workspace menuDeployDevelopersAPI keys
Permission
Needs Manage keys and webhooks
Time
7 min read
Markdown
01 / 04Send the customer's message. One POST with the latest message and a stable user id. stream: true asks for the reply as it is written.
  1. Send the customer's message. One POST with the latest message and a stable user id. stream: true asks for the reply as it is written.
  2. The reply streams back. Chunks in OpenAI's format arrive a sentence at a time.
  3. It ends with [DONE]. A last chunk with finish_reason: "stop", then data: [DONE].
  4. The conversation is in the dashboard. It is filed under the key's channel, here API, with the full transcript, like any other conversation.

What it is

The Chat API takes a customer's message and returns the assistant's reply. It speaks the wire format of OpenAI's Chat Completions, so the official OpenAI SDKs and tools that accept a custom OpenAI address work with it, for example a voice platform's Custom LLM setting. Behind it runs the same turn as WhatsApp or the website chat: knowledge, tools, procedures, handover and guardrails all apply.

  • Use it from your server. Never call it from a browser: the key would be exposed. The website widget is for browsers.
  • The assistant keeps its own memory per conversation. You send only the newest message.
  • Each reply the assistant writes uses credits like on any other channel. The messages you send are free.

Endpoints

RequestWhat it does
POST /v1/agents/{agent_id}/chat/completionsChat with the assistant named in the path. A key from another assistant gets 401.
POST /v1/chat/completionsThe key alone picks the assistant. For clients that always add /v1/chat/completions to a base address.
GET /v1/agents/{agent_id}/models, GET /v1/modelsLists one model whose id is the assistant's id, owned_by: "agent-platform". For clients that ask which models exist.
POST /v1/visitor-context, POST /v1/agents/{agent_id}/visitor-contextTell the assistant who a customer is before they write. See Who the customer is.

The base is your dashboard's address; examples use https://app.heyservo.ai. The Endpoint box on DevelopersAPI keys shows the exact address, with the assistant's id filled in when you filter by an assistant.

Authentication

Send a chat key as a Bearer token: Authorization: Bearer sk-agent-…. Create one on DevelopersAPI keys with the purpose Chat (OpenAI-compatible API). Its Channel decides where the conversations are filed: API, Website, Phone or WhatsApp. See API keys.

Send a message

curl -X POST "https://app.heyservo.ai/v1/agents/AGENT_ID/chat/completions" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "user": "customer-42",
    "messages": [
      {"role": "user", "content": "Do you have a table for two tonight?"}
    ]
  }'
Replace YOUR_API_KEY and AGENT_ID with your chat key and the assistant's id.

The request body

Body fields5 controls
messagesList
Required. An array of {role, content}. Only the newest user message with content is used. Earlier messages and system prompts in the array are ignored: the assistant keeps its own history and follows its own instructions. Without a user message the request fails with 400.
userText
Strongly recommended. A stable id for the customer or conversation, such as your own customer id. The same user continues the same conversation. It also becomes the conversation's contact id. Values starting with conv: are reserved.
streamSwitch
true streams the reply as server-sent events.
Default false
modelText
Ignored for behaviour, and echoed back in the response. When you leave it out, the response carries the assistant's id.
Other OpenAI fieldsShown
temperature, max_tokens, tools and the rest are accepted and ignored. The assistant's own settings decide.

How a conversation is identified

HeyServo derives the conversation from the assistant and the user value. Send the same user and the assistant remembers what was said, and the messages join the same transcript in Conversations.

  • Send only the newest message each time. Sending the whole history does no harm, but only the last user message counts.
  • If the same message arrives again at the end of the same conversation within 120 seconds, it is treated as a retry and not stored twice.
  • While one message is still being answered, a second for the same conversation waits its turn. If it waits too long, it gets 409.
  • When a team member has taken over the conversation, you get an ordinary reply carrying the assistant's handover acknowledgement, set on Hand over to a person.

The response

{
  "id": "chatcmpl-6b1f0c2e9a4d4e7b8c3a1f20",
  "object": "chat.completion",
  "created": 1763900000,
  "model": "heyservo",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Yes, we have a table for two at 8pm. Shall I hold it for you?"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": { "prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0 }
}
200 OK

usage is always zeros. Credits are charged per reply and are not reported in tokens here: see Credits and billing.

Streaming

With "stream": true the answer is text/event-stream. Each line is data: and a chat.completion.chunk:

  1. A first chunk with delta: {"role": "assistant", "content": ""}.
  2. Content chunks, about a sentence each, as the reply is written.
  3. A last chunk with finish_reason: "stop".
  4. data: [DONE].
  • Lines starting with : (: keepalive) may arrive while the assistant uses a slow tool. Ignore them; most clients do.
  • If the assistant is set to say a short waiting line during tool calls, it arrives as ordinary content followed by a space.
  • An error in the middle of a stream arrives as content starting Sorry, something went wrong:.
curl -N -X POST "https://app.heyservo.ai/v1/agents/AGENT_ID/chat/completions" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"user": "customer-42", "stream": true,
       "messages": [{"role": "user", "content": "Hi"}]}'

Who the customer is

To let the assistant greet a customer by name or know their number, register them first with POST /v1/visitor-context (or /v1/agents/{agent_id}/visitor-context) using the same chat key. When a later chat request's user equals the caller_id, the assistant knows who it is talking to.

Visitor context body5 controls
caller_idText
Required. The same value you will send as user. callerId is accepted too.
full_nameText
The customer's name.
phoneText
International E.164 form, like +971501234567.
emailText
A valid email address.
source, pageText
Optional notes on where they came from.

At least one of full_name, phone or email is needed. The answer is {"ok": true, "agent_id": …, "visitor": {…}}.

curl -X POST "https://app.heyservo.ai/v1/agents/AGENT_ID/visitor-context" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"caller_id": "customer-42", "full_name": "Layla Hassan",
       "phone": "+971501234567", "email": "layla@example.com"}'

Errors and status codes

Errors come back as {"detail": "…"} with the message below.

StatusMessageWhat to do
200The reply. Also used for the cases in the next table.
400messages must include at least one user messageSend at least one user message with content.
400user values starting with conv: are reservedPick another user value.
400caller_id is required, phone must be E.164 (e.g. +9715…), email is invalid, at least one of full_name, phone, or email is requiredVisitor context only: fix the body.
401missing bearer tokenAdd the Authorization: Bearer header.
401invalid or revoked API keyCheck the key, and that it belongs to the assistant in the path.
401API key is not a chat keyUse a key whose purpose is Chat.
409Sorry, I'm still working on your previous message. Please give me a moment and try again.A message for this conversation is still being answered. Wait, then send again. When streaming, this text arrives as content.
502The error, or no reply producedThe assistant could not produce a reply. Try again.
SituationWhat you get
A team member has taken overA normal reply with the handover acknowledgement, by default "A human team member will pick this conversation up shortly."
The account is out of credits or suspendedA normal reply with the fallback line, not an HTTP error. It is not charged.
A repeat of the last message within 120 secondsTreated as a retry: the message is not stored twice.

Limits

Questions people ask

Keep going
Chat API · HeyServo Docs