Live quotes
Keep a quote on screen fresh without polling. Stream GET /v1/quote/stream over Server-Sent Events, or use lh.quotes.stream() and the useQuoteStream React hook. Updates only when the best route or an amount changes.
A quote is good for a short time, and prices move. Instead of calling GET /v1/quote in a loop, open one stream: LayerHop re-quotes every provider on a fixed interval and pushes a new ranked quote set only when something you would see changed (the best route, the ranking or any amount), or when the quote on screen is about to expire.
Try it with curl#
curl -N "https://api.layerhop.com/v1/quote/stream?srcChain=base&srcToken=USDC&amount=100000000&dstChain=solana&dstToken=SOL&sender=0x1111111111111111111111111111111111111111&recipient=9WzDXwBbmkg8ZTbNMqUxvQRAyrZzDsGYdLVL9zYtAWWM&intervalMs=10000" \
-H "x-api-key: $LAYERHOP_API_KEY"event: ready
data: {"intervalMs":10000,"expiresAt":"2026-11-04T15:05:00.000Z","retryMs":3000}
event: quote
data: {"seq":1,"best":{"quoteId":"q_…","provider":"relay","dstAmount":"612345678",…},"quotes":[…],"errors":[],"livemode":true}
event: quote
data: {"seq":2,"best":{"quoteId":"q_…","provider":"relay","dstAmount":"612001122",…},…}
event: end
data: {"reason":"expired","quotesSent":7}The parameters are those of GET /v1/quote, plus intervalMs. Every quote event is a complete GET /v1/quote response with a seq number, and every quoteId in it can be built with POST /v1/orders as usual.
| Event | When | data |
|---|---|---|
ready |
At once | intervalMs, expiresAt (end of the stream's lifetime) |
quote |
At once, then on every change | A quote response plus seq |
error |
A refresh failed; the stream then closes | The error body plus status |
end |
After 5 minutes | reason: "expired", quotesSent |
Errors before the stream opens (bad parameters, NO_ROUTE on the first quote, ADDRESS_SANCTIONED) answer as plain JSON with the HTTP status, exactly like GET /v1/quote.
TypeScript SDK#
import { LayerHop } from "@layerhop/sdk";
const lh = new LayerHop({ apiKey: process.env.LAYERHOP_API_KEY });
const ctrl = new AbortController();
for await (const update of lh.quotes.stream(
{ srcChain: "base", srcToken: "USDC", amount: "100000000", dstChain: "solana", dstToken: "SOL", sender, recipient },
{ intervalMs: 10_000, signal: ctrl.signal },
)) {
render(update.best); // a fresh, buildable quote
}
// The loop ends after the stream's lifetime; a failed refresh throws LayerHopApiError.React#
import { useQuoteStream } from "@layerhop/react";
function LivePrice({ params }: { params: QuoteParams }) {
const { data, error, connected } = useQuoteStream(params, { intervalMs: 10_000 });
if (error) return <p>{error.message}</p>;
if (!data) return <p>Finding the best route…</p>;
return <p>{data.best.dstAmount} via {data.best.provider}{connected ? "" : " (reconnecting)"}</p>;
}The hook stays idle until the parameters are complete, opens a new stream when one reaches its lifetime, and reconnects with backoff after network errors and RATE_LIMITED. Client errors (NO_ROUTE, INVALID_PARAMS, ADDRESS_SANCTIONED) stop it until the parameters change.
Limits and cost#
- Interval:
intervalMsdefaults to 10,000; the minimum is 5,000 (INVALID_PARAMSbelow), and anything above 60,000 counts as 60,000. - Cost: each refresh is charged like one
GET /v1/quoteto your key's rate limit (or your IP's without a key) and, on paid plans, to the monthly quota. A refresh over the limit ends the stream with anerrorevent (RATE_LIMITEDorQUOTA_EXCEEDED). - Concurrency: at most 3 open quote streams per API key (or IP). Close one before opening another.
- Lifetime: 5 minutes. Open a new stream to continue (the React hook does).
- Not streamed: destination calls. Use
POST /v1/quotefor those.
Close the stream (abort the request, break out of the loop, unmount the component) as soon as the user leaves the screen: an idle stream still spends your rate limit.
Last updated