<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Odds API</title>
    <description>The latest articles on DEV Community by Odds API (@oddsapinet).</description>
    <link>https://dev.to/oddsapinet</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4146307%2F3cbe2110-acfa-47cf-bf3f-aa9fb5771220.png</url>
      <title>DEV Community: Odds API</title>
      <link>https://dev.to/oddsapinet</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/oddsapinet"/>
    <language>en</language>
    <item>
      <title>How to recover a sports odds SSE feed without missing updates</title>
      <dc:creator>Odds API</dc:creator>
      <pubDate>Mon, 28 Sep 2026 04:47:55 +0000</pubDate>
      <link>https://dev.to/oddsapinet/how-to-recover-a-sports-odds-sse-feed-without-missing-updates-40p1</link>
      <guid>https://dev.to/oddsapinet/how-to-recover-a-sports-odds-sse-feed-without-missing-updates-40p1</guid>
      <description>&lt;p&gt;A live odds screen has two jobs: show a coherent starting state and keep that state current. Opening a stream alone solves only the second job. If the connection drops, the client also needs a precise place to resume—and a way to rebuild when that place is no longer available.&lt;/p&gt;

&lt;p&gt;Here is a practical pattern using the &lt;a href="https://odds-api.net/docs" rel="noopener noreferrer"&gt;Odds API event odds endpoints&lt;/a&gt;. The API exposes a REST snapshot and a Server-Sent Events (SSE) stream for each event. The same approach applies to other snapshot-plus-stream APIs.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Start with an event, then fetch its snapshot
&lt;/h2&gt;

&lt;p&gt;Discover a current &lt;code&gt;event_id&lt;/code&gt; through &lt;code&gt;GET /v1/events&lt;/code&gt;. Do not hard-code an example ID. Keep your API key on the server and send it in &lt;code&gt;X-API-Key&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s2"&gt;"X-API-Key: &lt;/span&gt;&lt;span class="nv"&gt;$ODDS_API_KEY&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;   &lt;span class="s2"&gt;"https://api.odds-api.net/v1/events/&lt;/span&gt;&lt;span class="nv"&gt;$EVENT_ID&lt;/span&gt;&lt;span class="s2"&gt;/odds/snapshot?market_keys=moneyline"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The response contains &lt;code&gt;items&lt;/code&gt;, &lt;code&gt;as_of_ts_ms&lt;/code&gt;, &lt;code&gt;ttl_seconds&lt;/code&gt;, &lt;code&gt;complete&lt;/code&gt;, &lt;code&gt;next_cursor&lt;/code&gt;, and &lt;code&gt;resume&lt;/code&gt;. If &lt;code&gt;complete&lt;/code&gt; is false, follow the opaque &lt;code&gt;next_cursor&lt;/code&gt; with the same filters until the snapshot is complete. Store the final set of lines by each line's &lt;code&gt;id&lt;/code&gt;, and keep the snapshot's &lt;code&gt;resume&lt;/code&gt; token alongside that state. Do not display a partial page as the whole market.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;as_of_ts_ms&lt;/code&gt; is the accepted snapshot time. &lt;code&gt;bookmaker_as_of_ts_ms&lt;/code&gt; is more useful when you need to decide whether a particular bookmaker's price is fresh. A connected stream does not make an old bookmaker observation fresh by itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Open the stream from that exact point
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-N&lt;/span&gt; &lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s2"&gt;"X-API-Key: &lt;/span&gt;&lt;span class="nv"&gt;$ODDS_API_KEY&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;   &lt;span class="s2"&gt;"https://api.odds-api.net/v1/events/&lt;/span&gt;&lt;span class="nv"&gt;$EVENT_ID&lt;/span&gt;&lt;span class="s2"&gt;/odds/stream?market_keys=moneyline&amp;amp;since=&lt;/span&gt;&lt;span class="nv"&gt;$RESUME&lt;/span&gt;&lt;span class="s2"&gt;&amp;amp;catchup=true"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use the &lt;strong&gt;same filters&lt;/strong&gt; on the snapshot and stream. Otherwise, your initial state and later changes describe different markets. Encode the opaque resume value as a query parameter; do not parse it or increment it yourself.&lt;/p&gt;

&lt;p&gt;The stream sends named SSE messages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;event: delta
data: {"event_id":"...","resume":"...","changes":[...]}

event: heartbeat
data: {}

event: resync
data: {"event_id":"...","resume":null,"reason":"trimmed"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each &lt;code&gt;changes&lt;/code&gt; entry has an operation and an &lt;code&gt;odd&lt;/code&gt; line. Apply the batch idempotently using the line's stable &lt;code&gt;id&lt;/code&gt;. For an upsert, replace that ID; for a deletion, remove it. &lt;strong&gt;Only after the whole batch is applied&lt;/strong&gt; should you save its &lt;code&gt;resume&lt;/code&gt;. If saving state and cursor happens in separate steps, a crash can leave you with a cursor ahead of your actual state. In a durable consumer, commit both together.&lt;/p&gt;

&lt;p&gt;The core state transition is small:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;handle_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;heartbeat&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;continue&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;resync&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;reload_snapshot&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;delta&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;continue&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

    &lt;span class="n"&gt;updated&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;change&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;changes&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
        &lt;span class="n"&gt;odd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;change&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;odd&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;line_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;odd&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;id&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;change&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;op&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;upsert&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;updated&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;line_id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;odd&lt;/span&gt;
        &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;change&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;op&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;delete&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;updated&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;reload_snapshot&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;updated&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;resume&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;continue&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That function deliberately returns a new mapping. The caller can persist it together with the cursor before showing the update. If your consumer cannot recognize an operation, reloading is safer than advancing past it.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Treat a broken connection as a normal state
&lt;/h2&gt;

&lt;p&gt;On disconnect, reconnect with &lt;code&gt;since=&amp;lt;last_committed_resume&amp;gt;&amp;amp;catchup=true&lt;/code&gt;. Use jittered exponential backoff, and honour &lt;code&gt;Retry-After&lt;/code&gt; on HTTP 429 rather than retrying in a tight loop. Set a liveness timeout: if neither a delta nor a heartbeat arrives within your chosen window, close the connection and reconnect.&lt;/p&gt;

&lt;p&gt;If the server sends &lt;code&gt;resync&lt;/code&gt;, the old cursor cannot be used. Fetch a new complete snapshot, replace the cached state and cursor together, then open a new stream from that cursor. Keep the same request filters throughout.&lt;/p&gt;

&lt;p&gt;The practical test is to disconnect the client while prices are moving. After reconnecting, it should either catch up from the saved cursor or visibly rebuild from a new snapshot. It should never silently declare itself current merely because the TCP connection reopened.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://odds-api.net/docs" rel="noopener noreferrer"&gt;live API reference&lt;/a&gt; documents the snapshot fields, SSE message shapes, filters, and rate-limit responses used here.&lt;/p&gt;

</description>
      <category>python</category>
      <category>api</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
