LeoKit’s streaming quotes endpoint uses Server-Sent Events (SSE) to deliver real-time quote updates as they arrive from different protocols. This provides a better user experience than waiting for all quotes to resolve before displaying results.Documentation Index
Fetch the complete documentation index at: https://docs.leokit.dev/llms.txt
Use this file to discover all available pages before exploring further.
Why Use Streaming?
Faster Time-to-First-Quote
Display the first quote in ~1-2 seconds instead of waiting 5-10 seconds for all protocols
Progressive UI Updates
Update your UI as each quote arrives, showing users better rates in real-time
Better UX
Users see immediate feedback instead of loading spinners
Lower Latency
Reduce perceived wait time with incremental updates
How It Works
- Client opens SSE connection with quote parameters
- Server immediately responds with
initevent containing quote_id - Server sends
quoteevents as each protocol responds - Server sends
finalevent with all quotes aggregated - Server sends
finishedevent and closes connection
Implementation
Basic Example
Event Types
1. init Event
Sent immediately when the connection opens. Contains the quote_id you’ll use for the swap.
quote_id- UUID to use for deposit/status endpointstotal- Expected number of quotes (one per protocol)timestamp- When the quote request was received
2. quote Event
Sent each time a protocol returns a quote. May arrive in any order.
protocol- Which protocol provided this quotedata.expected_amount_out- Output amount (in base units)data.flags- Array of flags like["OPTIMAL", "FASTEST"]
3. final Event
Sent after all protocols have responded. Contains complete array of quotes sorted by best rate.
4. finished Event
Signals the stream is complete. Close the connection after receiving this.
5. error Event
Sent if an error occurs during quote generation.
React Integration
Here’s a React hook for streaming quotes:Handling Disconnects
SSE connections can drop due to network issues. Implement automatic reconnection:Best Practices
Always close connections - Call
eventSource.close() when done to prevent memory leaks.Handle all event types - Don’t assume you’ll only receive
quote events. Handle error and finished too.Save quote_id from init - You need this for the deposit endpoint. Don’t wait for the
final event.Show incremental updates - Display quotes as they arrive for better UX. Don’t wait for
final.Implement timeouts - Close the connection if no events arrive within 30 seconds.
SSE vs Regular Quote Endpoint
| Feature | Streaming (/streaming-quotes) | Regular (/quote) |
|---|---|---|
| Response time | 1-2s for first quote | 5-10s for all quotes |
| Updates | Incremental | Single response |
| Connection | Persistent (SSE) | One-time HTTP |
| Use case | Interactive UIs | Batch processing |
Browser Compatibility
EventSource is supported in all modern browsers:- ✅ Chrome 6+
- ✅ Firefox 6+
- ✅ Safari 5+
- ✅ Edge 79+
- ❌ IE 11 (use polyfill)
Troubleshooting
Connection immediately closes
Connection immediately closes
Cause: Invalid API key or malformed parametersSolution: Check the
error event for details. Verify API key is correct.No quotes received
No quotes received
Cause: No protocols support the requested swap routeSolution: Check
/leokit/assets for supported assets. Verify asset identifiers.Stream hangs after init
Stream hangs after init
Cause: Network issue or server timeoutSolution: Implement client-side timeout (30s recommended). Close and retry.
Quotes out of order
Quotes out of order
Cause: Protocols respond at different speedsSolution: This is normal. Sort quotes by
expected_amount_out in your UI.Next Steps
Complete Swap Flow
Use the quote_id from streaming to execute a swap
API Reference
Full API documentation for streaming quotes