Streaming
Overview
ChatBotKit API endpoints support both streaming and non-streaming responses, allowing you to receive data incrementally as it becomes available or wait for a complete response. Streaming is particularly useful for large datasets, long-running operations, and real-time updates.
How Streaming Works
The API determines whether to stream responses based on the Accept header
you send with your request:
- Non-streaming (default): Set
Accept: application/jsonto receive a complete JSON response once the operation finishes - Streaming: Set
Acceptto a streaming format (application/jsonl,text/event-stream, ortext/csv) to receive data incrementally
For non-streaming requests that take longer than expected, the API automatically falls back to streaming mode to ensure you receive data before any timeout limits.
Supported Response Formats
JSON (Non-Streaming)
Accept Header: application/json
Returns a complete JSON response when the operation finishes. This is the default format and is ideal for most API calls that return small amounts of data quickly.
Example Request:
GET /api/v1/bot/list HTTP/1.1
Accept: application/json
Authorization: Bearer YOUR_API_KEY
Example Response:
JSON Lines (Streaming)
Accept Header: application/jsonl
Returns newline-delimited JSON objects, with each line representing a separate event. This format is ideal for processing large datasets or receiving real-time updates as they occur.
Example Request:
GET /api/v1/bot/list HTTP/1.1
Accept: application/jsonl
Authorization: Bearer YOUR_API_KEY
Example Response:
{"type":"item","data":{"id":"bot1","name":"My Bot"}}
{"type":"item","data":{"id":"bot2","name":"Another Bot"}}
{"type":"result","data":{"complete":true}}
Server-Sent Events (Streaming)
Accept Header: text/event-stream
Returns data using the Server-Sent Events (SSE) protocol, which is designed
for real-time server-to-client communication. This format works seamlessly
with the browser's built-in EventSource API.
Example Request:
GET /api/v1/bot/list HTTP/1.1
Accept: text/event-stream
Authorization: Bearer YOUR_API_KEY
Example Response:
event: item
data: {"id":"bot1","name":"My Bot"}
event: item
data: {"id":"bot2","name":"Another Bot"}
event: result
data: {"complete":true}
CSV (Streaming)
Accept Header: text/csv
Returns data in CSV (Comma-Separated Values) format with headers automatically generated from the first data item. This format is ideal for data exports and spreadsheet applications.
Example Request:
GET /api/v1/bot/list HTTP/1.1
Accept: text/csv
Authorization: Bearer YOUR_API_KEY
Example Response:
Stream Event Types
When using streaming formats (JSON Lines or Server-Sent Events), the API sends different types of events:
item: Individual data records (e.g., each bot, conversation, or file)result: Final result indicating the operation is completeerror: Error information if something goes wrongnop: Keep-alive signal sent during long operations to maintain the connectionping: Periodic heartbeat to ensure the connection stays active
When to Use Streaming
Choose streaming when you need:
- Large datasets: Listing hundreds or thousands of items
- Long-running operations: Tasks that may take more than a few seconds
- Real-time updates: Progress notifications or live event feeds
- Data exports: Downloading data for backup or analysis
- Better user experience: Show data as it arrives instead of waiting
Choose non-streaming (JSON) when you need:
- Small responses: Single records or small lists
- Fast operations: Quick lookups or updates
- Simpler code: Standard REST API patterns without streaming complexity
Best Practices
- Handle connection interruptions: Implement retry logic for streaming connections that may be interrupted
- Process events incrementally: Don't wait for the entire stream to complete before processing data
- Use appropriate timeouts: Set reasonable timeout values for both streaming and non-streaming requests
- Monitor keep-alive events: Watch for
nopandpingevents to detect connection health - Close connections properly: Always close streaming connections when done to free up resources