Set "stream": true on POST /v1/chat/completions. Ngamia returns a text/event-stream response containing one JSON chunk per data: line and a final data: [DONE] marker.
How the stream is shaped
Each chunk uses OpenAI’s chat.completion.chunk object. Append the text in choices[0].delta.content as it arrives.
data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1752500000,"model":"openai/gpt-4o-mini","choices":[{"index":0,"delta":{"content":"Ha"},"finish_reason":null}]}
data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1752500000,"model":"openai/gpt-4o-mini","choices":[{"index":0,"delta":{"content":"bari!"},"finish_reason":"stop"}],"usage":{"prompt_tokens":12,"completion_tokens":3,"total_tokens":15}}
data: [DONE]The final chunk may include usage. The stream is complete only after the [DONE] marker.
Consume a stream
curl -N https://api.ngamia.cc/v1/chat/completions \\
-H "Authorization: Bearer $NGAMIA_API_KEY" \\
-H "Content-Type: application/json" \\
-d '{
"model": "openai/gpt-4o-mini",
"stream": true,
"messages": [{"role":"user","content":"Count to three."}]
}'Production notes
| Situation | Recommendation |
|---|---|
| Connection drops | Retry the logical request with an Idempotency-Key when supported; see Idempotency. |
| User cancels generation | Close the stream and record the request id if available. |
| Mobile or browser client | Use an SSE-aware client or read the response as a stream and split events on blank lines. Keep the API key on your server. |
| Billing | Ngamia bills completed responses using actual token counts. A client-disconnected stream is recorded as cancelled and is not charged. |
For the non-streaming request and response shape, see Chat completions.