Skip to content

Stream SSE tokens smoothly to React and web UIs

Deliver instant word-by-word Server-Sent Events from visual endpoints straight into React state with clean abort and error handling.

Copy the prompt and paste it into the AI assistant you already use.

Checklist for smooth frontend SSE streaming

  1. 01

    Use Fetch streams for POST routes

    Use standard fetch ReadableStream readers rather than GET-only EventSource when passing large bodies.

  2. 02

    Support clean user cancellation

    Wire AbortController signals so closing a drawer cancels upstream inference and saves tokens.

  3. 03

    Handle mid-stream error events

    Emit structured SSE error frames so UI shows a graceful retry button rather than hanging forever.

  4. 04

    Send metadata in final event

    Include trace_id and token totals inside the closing SSE frame for easy UI feedback links.

How it works in practice

Endpoint Stream Output (text/event-stream)

POST /v1/draft-email (stream: true)

Live SSE Chunks & React Handler

event: delta data: {"text":"Hi Alex, "} event: delta data: {"text":"thanks for sharing the logs..."} event: done data: {"trace_id":"tr_88a","tokens":94}

Users read the opening sentence in 350ms while React renders smooth progressive chunks with clean final telemetry.

Common questions

Does SSE streaming work behind Vercel or Cloudflare edge proxies?

Yes. Standard text/event-stream responses with proper Content-Type and no-buffering headers stream cleanly across modern CDNs.

Does canceling an SSE connection in the browser stop billing?

Closing the request stream triggers upstream cancellation immediately so you stop paying for unconsumed trailing tokens.

Can I still stream if my endpoint switches between providers?

Yes. APIkit normalizes chunk formats across OpenAI, Anthropic, and open models into one consistent SSE frame shape.