Skip to main content
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.

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

  1. Client opens SSE connection with quote parameters
  2. Server immediately responds with init event containing quote_id
  3. Server sends quote events as each protocol responds
  4. Server sends final event with all quotes aggregated
  5. Server sends finished event 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.
Fields:
  • quote_id - UUID to use for deposit/status endpoints
  • total - 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.
Key Fields:
  • protocol - Which protocol provided this quote
  • data.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

Browser Compatibility

EventSource is supported in all modern browsers:
  • ✅ Chrome 6+
  • ✅ Firefox 6+
  • ✅ Safari 5+
  • ✅ Edge 79+
  • ❌ IE 11 (use polyfill)
For IE11 support, use the EventSource polyfill:

Troubleshooting

Cause: Invalid API key or malformed parametersSolution: Check the error event for details. Verify API key is correct.
Cause: No protocols support the requested swap routeSolution: Check /leokit/assets for supported assets. Verify asset identifiers.
Cause: Network issue or server timeoutSolution: Implement client-side timeout (30s recommended). Close and retry.
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