curl --request POST \
--url https://api.example.com/v1/chat/stream \
--header 'Content-Type: application/json' \
--data '
{
"sessionId": "<string>",
"message": "<string>"
}
'{
"type": "<string>",
"conversationId": "<string>",
"token": "<string>",
"message": "<string>",
"error": "<string>"
}Send a chat message and receive a streaming response in NDJSON format
curl --request POST \
--url https://api.example.com/v1/chat/stream \
--header 'Content-Type: application/json' \
--data '
{
"sessionId": "<string>",
"message": "<string>"
}
'{
"type": "<string>",
"conversationId": "<string>",
"token": "<string>",
"message": "<string>",
"error": "<string>"
}POST /v1/chat/stream
POST /chat is an alias for this endpoint and behaves identically. The widget uses /chat by default for backward compatibility.x-api-key: Your widget API keyx-widget-api-key: Your widget API key (alternative)^[A-Za-z0-9._:-]{1,128}$.Used to track conversation history across multiple requests. Use the same sessionId for continuation of a conversation.{
"sessionId": "user-123",
"message": "Explain what your company does"
}
Content-Type: application/x-ndjson.
Each line is a JSON object representing a stream event.
"start"{"type":"start","conversationId":"j97ch2kj8xm6rpct8te5w9b1fh6pc7k8"}
"token"{"type":"token","token":"We"}
{"type":"token","token":" are"}
{"type":"token","token":" a"}
{"type":"token","token":" customer"}
{"type":"token","token":" support"}
"done"{"type":"done","message":"We are a customer support platform that helps businesses engage with their customers.","conversationId":"j97ch2kj8xm6rpct8te5w9b1fh6pc7k8"}
"error"{"type":"error","error":"Internal server error"}
{
"error": "Invalid request payload"
}
{
"error": "Unauthorized"
}
{
"error": "Too many requests"
}
{
"error": "Internal server error"
}
curl -X POST https://your-api-domain.com/v1/chat/stream \
-H "Content-Type: application/json" \
-H "x-api-key: your-api-key-here" \
-d '{
"sessionId": "user-123",
"message": "Tell me about your services"
}'
const response = await fetch('https://your-api-domain.com/v1/chat/stream', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': 'your-api-key-here'
},
body: JSON.stringify({
sessionId: 'user-123',
message: 'Tell me about your services'
})
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
const lines = chunk.split('\n').filter(line => line.trim());
for (const line of lines) {
const event = JSON.parse(line);
switch (event.type) {
case 'start':
console.log('Stream started:', event.conversationId);
break;
case 'token':
process.stdout.write(event.token);
break;
case 'done':
console.log('\nComplete message:', event.message);
break;
case 'error':
console.error('Error:', event.error);
break;
}
}
}
import requests
import json
response = requests.post(
'https://your-api-domain.com/v1/chat/stream',
headers={
'Content-Type': 'application/json',
'x-api-key': 'your-api-key-here'
},
json={
'sessionId': 'user-123',
'message': 'Tell me about your services'
},
stream=True
)
for line in response.iter_lines():
if line:
event = json.loads(line)
if event['type'] == 'start':
print(f"Stream started: {event['conversationId']}")
elif event['type'] == 'token':
print(event['token'], end='', flush=True)
elif event['type'] == 'done':
print(f"\nComplete message: {event['message']}")
elif event['type'] == 'error':
print(f"Error: {event['error']}")
start event containing the conversationIddone event containing the full messageerror event is sent/chat (POST) is also available with the same behavior