Stream a chat response
Read ShareAI chat-completions server-sent events, append content deltas once and handle completion, errors and interrupted streams.
On this page
Set stream: true to receive the response incrementally. Keep the HTTP connection open, parse complete SSE events and append each content delta once.
https://api.shareai.now/api/v1/chat/completionsReturn a server-sent event stream.
- Base URL
https://api.shareai.now- Authentication
- Bearer API key or approved OAuth access token
cURL
curl --no-buffer --fail-with-body https://api.shareai.now/api/v1/chat/completions \
-H "Authorization: Bearer $SHAREAI_API_KEY" \
-H "Content-Type: application/json" \
--data '{"model":"MODEL_IDENTIFIER","messages":[{"role":"user","content":"Explain server-sent events briefly."}],"stream":true}'
Read event boundaries#
SSE separates events with a blank line. Network chunks can split a JSON value or contain multiple events, so do not parse each network read as a complete JSON response. Decode UTF-8 incrementally and retain incomplete data until the event is complete.
Plain text
data: {"object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}
data: {"object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: [DONE]
Render content once#
Append choices[0].delta.content when present. Preserve whitespace and repeated words. A role-only or final chunk can have no content. Treat [DONE] as the stream terminator.
Handle stream errors#
Check the HTTP status before entering the SSE parser. After streaming starts, handle an event: error event and unexpected disconnection. Mark an interrupted answer as incomplete and let the user retry explicitly. Do not append a second independently generated answer to partial output.
Cancel from your application#
Use your HTTP client’s cancellation mechanism when the user stops generation. Keep cancellation, a network failure and a completed answer as separate UI states. Do not promise that closing a client connection reverses usage already processed.
Last updated September 15, 2026