<?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: APIHiver</title>
    <description>The latest articles on DEV Community by APIHiver (@api_hiver).</description>
    <link>https://dev.to/api_hiver</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%2F4177880%2F5ee5bf4f-93ad-482a-bb8b-9794fa6b8994.png</url>
      <title>DEV Community: APIHiver</title>
      <link>https://dev.to/api_hiver</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/api_hiver"/>
    <language>en</language>
    <item>
      <title>Amadeus Closed Its Free API Portal: What to Use Instead in 2026</title>
      <dc:creator>APIHiver</dc:creator>
      <pubDate>Sun, 01 Nov 2026 10:19:43 +0000</pubDate>
      <link>https://dev.to/api_hiver/amadeus-closed-its-free-api-portal-what-to-use-instead-in-2026-3d51</link>
      <guid>https://dev.to/api_hiver/amadeus-closed-its-free-api-portal-what-to-use-instead-in-2026-3d51</guid>
      <description>&lt;p&gt;If you built a travel app on the Amadeus Self-Service API, your keys stopped working on 17 July 2026. If you are starting one now, the free test tier that many older tutorials still point to is gone.&lt;/p&gt;

&lt;p&gt;I spent a day reading the shutdown coverage and the pricing pages of the alternatives, and checking what is still online on Amadeus's side. Here is what I found, and what I could not check.&lt;/p&gt;

&lt;h2&gt;
  
  
  What actually happened
&lt;/h2&gt;

&lt;p&gt;PhocusWire &lt;a href="https://www.phocuswire.com/amadeus-shut-down-self-service-apis-portal-developers" rel="noopener noreferrer"&gt;reported on 9 February 2026&lt;/a&gt; that Amadeus told users, in a letter, that it would pause new registrations and fully close the self-service portal on 17 July. Amadeus said it was decommissioning only the self-service section and that its Enterprise APIs stay available. It did not give a reason, according to that report.&lt;/p&gt;

&lt;p&gt;On 1 November 2026 I looked for the traces myself:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The old test host, &lt;code&gt;test.api.amadeus.com&lt;/code&gt;, has no DNS record.&lt;/li&gt;
&lt;li&gt;All 20 repositories in Amadeus's &lt;code&gt;amadeus4dev&lt;/code&gt; GitHub organisation are archived.&lt;/li&gt;
&lt;li&gt;The banner on &lt;a href="https://developers.amadeus.com/" rel="noopener noreferrer"&gt;developers.amadeus.com&lt;/a&gt; says the self-service portal "has been decommissioned on July 17th" and that the site is now for the Enterprise portal only.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  There is no one-for-one replacement
&lt;/h2&gt;

&lt;p&gt;Self-Service did several jobs: flight price search, booking, airport and airline lookups, and flight status. The alternatives each cover one or two.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Prices only:&lt;/strong&gt; &lt;a href="https://serpapi.com/pricing" rel="noopener noreferrer"&gt;SerpApi&lt;/a&gt; lists a free plan of 250 searches a month, and &lt;a href="https://www.searchapi.io/pricing" rel="noopener noreferrer"&gt;SearchApi&lt;/a&gt; lists 100 free requests. These return search results, not tickets.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Booking tickets:&lt;/strong&gt; &lt;a href="https://duffel.com/guides/getting-started" rel="noopener noreferrer"&gt;Duffel&lt;/a&gt; lets you create a test token on sign-up, but going live needs verification and KYC checks. Its &lt;a href="https://duffel.com/pricing" rel="noopener noreferrer"&gt;pricing page&lt;/a&gt; lists $3.00 per order.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Airport, airline and status data:&lt;/strong&gt; AirLabs and &lt;a href="https://aviationstack.com/product" rel="noopener noreferrer"&gt;Aviationstack&lt;/a&gt;. Aviationstack's free plan is 100 requests a month and is non-commercial.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Seat maps and hotels:&lt;/strong&gt; none of the vendor pages I read claim to replace these. That is Enterprise territory.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The cost trap nobody mentions
&lt;/h2&gt;

&lt;p&gt;Duffel charges $0.005 per search once you pass 1,500 searches per order. With 10 orders, 15,000 searches are free. For a shop that sells a lot, that is nothing. For a price tracker where people browse a lot and rarely book, the ratio can flip the economics. Do the arithmetic with your own numbers before you choose.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I did not verify
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;I did not sign up to or call any of these APIs, so I have no first-hand numbers on speed or accuracy.&lt;/li&gt;
&lt;li&gt;I did not read the full terms of SerpApi, SearchApi or Sabre.&lt;/li&gt;
&lt;li&gt;Claims from Ignav and AirLabs are vendor claims, because they sell the services they recommend.&lt;/li&gt;
&lt;li&gt;My Amadeus checks are one machine on one day.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Which flight API did you move to after the shutdown, and how long did the switch take? I would like to hear what surprised you.&lt;/p&gt;

&lt;p&gt;I wrote up the full comparison, with a worked cost example and a migration checklist, on the APIHiver blog: &lt;a href="https://apihiver.com/blog/amadeus-api-alternatives" rel="noopener noreferrer"&gt;https://apihiver.com/blog/amadeus-api-alternatives&lt;/a&gt;&lt;/p&gt;

</description>
      <category>api</category>
      <category>webdev</category>
      <category>travel</category>
      <category>programming</category>
    </item>
    <item>
      <title>I Asked Three Free Flight APIs About the Same Sky. They Disagreed</title>
      <dc:creator>APIHiver</dc:creator>
      <pubDate>Sun, 01 Nov 2026 07:04:36 +0000</pubDate>
      <link>https://dev.to/api_hiver/i-asked-three-free-flight-apis-about-the-same-sky-they-disagreed-5fcn</link>
      <guid>https://dev.to/api_hiver/i-asked-three-free-flight-apis-about-the-same-sky-they-disagreed-5fcn</guid>
      <description>&lt;p&gt;I wanted to know a simple thing: if you ask different free flight tracking APIs about the same patch of sky, do you get the same planes? So I called three of them, none needing an API key, over a small box around New York. I did it five times in a row at about 06:48 UTC on 1 November 2026.&lt;/p&gt;

&lt;p&gt;They did not agree.&lt;/p&gt;

&lt;h2&gt;
  
  
  What came back
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;OpenSky returned 7 to 8 aircraft.&lt;/li&gt;
&lt;li&gt;adsb.fi returned 18 to 19.&lt;/li&gt;
&lt;li&gt;adsb.lol returned 29.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Across the three, 31 different aircraft were seen. All three answered in under half a second (median 0.32 to 0.45 seconds), so speed is not what separates them. Coverage is.&lt;/p&gt;

&lt;p&gt;I do not know why OpenSky returned so few. Each service uses its own receivers and rules, and OpenSky's docs describe anonymous access as limited, but I did not test the cause. It was also the middle of the night in New York, so the sky was quiet. Treat the numbers as one snapshot from one internet connection, not a ranking.&lt;/p&gt;

&lt;h2&gt;
  
  
  What none of them tell you
&lt;/h2&gt;

&lt;p&gt;None of the responses I got contained an origin or destination. These APIs give you what an aircraft broadcasts about itself: position, altitude, speed and call sign. If you want departures and arrivals, you need a flight status API with a key. Aviationstack, for example, has a free plan of 100 requests a month.&lt;/p&gt;

&lt;h2&gt;
  
  
  The part most people skip: the licence
&lt;/h2&gt;

&lt;p&gt;"No key" does not mean "no rules". I read the terms of each one.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;OpenSky's terms of use say the data is licensed for non-profit research and education. Any use by a for-profit or commercial entity needs a written licence from OpenSky.&lt;/li&gt;
&lt;li&gt;adsb.fi's page says its open data is for personal, non-commercial use only, with a credit link back to it.&lt;/li&gt;
&lt;li&gt;adsb.lol's docs say its API is licensed under ODbL 1.0, an open database licence with attribution and share-alike conditions. I read a summary of ODbL, not the full text, so check it yourself.&lt;/li&gt;
&lt;li&gt;airplanes.live refused my first call with a 403 and a message asking me to email them with a description of my project. I did not contact them. Secondhand sources describe it as non-commercial, but its guide page returned a 404 when I looked, so that is unconfirmed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So a free API that answers instantly can still be one you are not allowed to build a business on.&lt;/p&gt;

&lt;h2&gt;
  
  
  If I were starting today
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;For a hobby map, I would use adsb.lol with a small radius and cache the results.&lt;/li&gt;
&lt;li&gt;For a school or university project, OpenSky is designed for that. Credit the network.&lt;/li&gt;
&lt;li&gt;For a product, I would not build on a non-commercial API. I would read the ODbL conditions properly and plan to move to a paid or licensed source before launch.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I did not verify
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;I did not run any API that needs a key. Aviationstack's numbers come from its pricing page.&lt;/li&gt;
&lt;li&gt;I did not contact airplanes.live.&lt;/li&gt;
&lt;li&gt;I tested one area at one quiet hour, so counts will differ at busy times and elsewhere.&lt;/li&gt;
&lt;li&gt;This is not legal advice.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Which flight data API are you using, and did you check its licence? I would like to know if anyone has had OpenSky grant them a commercial licence.&lt;/p&gt;

&lt;p&gt;I wrote up the full comparison, with the code I ran and the paid options, on the APIHiver blog: &lt;a href="https://apihiver.com/blog/free-flight-data-api" rel="noopener noreferrer"&gt;https://apihiver.com/blog/free-flight-data-api&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://openskynetwork.github.io/opensky-api/rest.html" rel="noopener noreferrer"&gt;OpenSky Network, REST API documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://opensky-network.org/about/terms-of-use" rel="noopener noreferrer"&gt;OpenSky Network, terms of use&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/adsbfi/opendata" rel="noopener noreferrer"&gt;adsb.fi, open data API repository&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.adsb.lol/docs/open-data/api/" rel="noopener noreferrer"&gt;adsb.lol, API documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://opendatacommons.org/licenses/odbl/summary/" rel="noopener noreferrer"&gt;Open Data Commons, ODbL summary&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://aviationstack.com/product" rel="noopener noreferrer"&gt;Aviationstack, pricing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Automatic_Dependent_Surveillance%E2%80%93Broadcast" rel="noopener noreferrer"&gt;Wikipedia, ADS-B&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>api</category>
      <category>webdev</category>
      <category>opensource</category>
      <category>data</category>
    </item>
    <item>
      <title>A Right AI Answer Can Still Cost You Time. Here's the Arithmetic and What the Studies Say</title>
      <dc:creator>APIHiver</dc:creator>
      <pubDate>Thu, 29 Oct 2026 13:03:52 +0000</pubDate>
      <link>https://dev.to/api_hiver/a-right-ai-answer-can-still-cost-you-time-heres-the-arithmetic-and-what-the-studies-say-4igp</link>
      <guid>https://dev.to/api_hiver/a-right-ai-answer-can-still-cost-you-time-heres-the-arithmetic-and-what-the-studies-say-4igp</guid>
      <description>&lt;p&gt;&lt;em&gt;A simple break-even model, three kinds of evidence, and one study that told us its own data was too compromised to trust.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A recent Coder Legion post by BizzAppDev, &lt;a href="https://coderlegion.com/32840/ai-gives-the-right-answer-but-does-it-actually-save-time" rel="noopener noreferrer"&gt;"AI Gives the Right Answer. But Does It Actually Save Time?"&lt;/a&gt;, makes a point I think is right: an AI feature should be judged by how much work is left after the answer appears, not by how good the answer is. The post has no numbers, so I went looking for some, and I added a small calculation of my own.&lt;/p&gt;

&lt;p&gt;Here is what I found.&lt;/p&gt;

&lt;h2&gt;
  
  
  The arithmetic of a "right" answer
&lt;/h2&gt;

&lt;p&gt;Take a task that takes 30 minutes by hand. With AI you pay for four things: generating the answer, getting it into the right place, reviewing it, and fixing it when it is wrong. I'll assume 2 minutes to generate, 4 to hand it off, 5 to review, and 20 to fix a wrong answer. These are my assumed numbers, not measurements.&lt;/p&gt;

&lt;p&gt;Plug them in and AI saves 17 minutes when it is right 90% of the time, 13 minutes at 70%, 9 at 50% and 5 at 30%. So far, so good.&lt;/p&gt;

&lt;p&gt;Now change one thing. If careful review of code you did not write takes 12 minutes instead of 5, the break-even point moves from "right 5% of the time" to "right 40% of the time". If the hand-off takes 10 minutes instead of 4, because someone has to open another screen, find the record, copy the answer and update it by hand, break-even moves to 35%. That hand-off is exactly the scenario the Coder Legion post describes.&lt;/p&gt;

&lt;p&gt;Accuracy is one term in the sum. Review and hand-off are the others, and they are the ones that rarely get measured.&lt;/p&gt;

&lt;h2&gt;
  
  
  What controlled studies found
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;a href="https://arxiv.org/abs/2302.06590" rel="noopener noreferrer"&gt;2023 preprint studying GitHub Copilot&lt;/a&gt; found that developers with the AI pair programmer finished one task, writing an HTTP server in JavaScript, 55.8% faster than a control group. It was a clean, self-contained task, which is where AI tends to look best.&lt;/li&gt;
&lt;li&gt;In mid-2025, &lt;a href="https://metr.org/blog/2025-07-10-early-2025-ai-experienced-os-dev-study/" rel="noopener noreferrer"&gt;METR ran a randomized trial&lt;/a&gt; with 16 experienced open-source developers working on 246 real issues in repositories they knew well. Each issue was randomly assigned to allow or forbid AI. With AI allowed, the developers took 19% longer. They had expected a 24% speedup, and afterwards they still believed AI had made them 20% faster. The authors list limits themselves: a small group, one kind of work, and tools that may not have been used in the best way.&lt;/li&gt;
&lt;li&gt;In February 2026, &lt;a href="https://metr.org/blog/2026-02-24-uplift-update/" rel="noopener noreferrer"&gt;METR published a follow-up&lt;/a&gt; with 57 developers across 143 repositories and more than 800 tasks. The raw numbers pointed the other way: tasks took 18% less time for the 10 returning developers and 4% less for the 47 new ones. Both ranges include "no change" (from 38% less to 9% more, and from 15% less to 9% more).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;METR then said its own data was too compromised to rely on. Developers increasingly refuse to work without AI, so the most optimistic people drop out of the no-AI condition, and 30% to 50% said they held back tasks they did not want to do without it. METR still believes developers are probably more sped up now than in early 2025, but calls its data only very weak evidence of that. My reading: the honest answer today is "it depends on the task, the tool and the year", and measuring it well is hard. One thing was solid in 2025, though: people felt faster while the clock said slower.&lt;/p&gt;

&lt;h2&gt;
  
  
  What developers say
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://survey.stackoverflow.co/2025/ai" rel="noopener noreferrer"&gt;2025 Stack Overflow Developer Survey&lt;/a&gt; asked what frustrates people about AI tools. The top answer, chosen by 66% of respondents, was "AI solutions that are almost right, but not quite". Another 45.2% said debugging AI-generated code is more time-consuming. On accuracy, about a third said they trust it and about 46% said they distrust it, while 47.1% use AI tools every day.&lt;/p&gt;

&lt;p&gt;My reading of "almost right": it is the most expensive case in the arithmetic above. A clearly wrong answer is rejected in seconds. An almost-right one passes a quick look, and you pay the review cost and then the fix cost.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I would measure instead
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Measure time to done, not time to answer. Start the clock when you begin the task and stop it when the result is where it needs to be.&lt;/li&gt;
&lt;li&gt;Count how often you had to fix or redo the AI's output, and how long that took.&lt;/li&gt;
&lt;li&gt;Count the manual steps left after the answer appears. BizzAppDev's three questions are a good checklist: is there less work afterwards, did the information reach the right place, and did the task finish without unnecessary manual steps?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;a href="https://dora.dev/research/2025/dora-report/" rel="noopener noreferrer"&gt;2025 DORA report&lt;/a&gt; on AI-assisted software development describes AI as an amplifier of an organization's existing strengths and weaknesses, which fits: the same tool saves time in a team with a clean workflow and costs time in one without.&lt;/p&gt;

&lt;p&gt;The METR result is the reason to write numbers down. For a week, note your own estimate for each AI-assisted task and then the real time including review and fixes. In the 2025 METR study, the developers' feeling and the clock disagreed.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I did not test
&lt;/h2&gt;

&lt;p&gt;I ran no experiment on real people or real tasks. The 30-minute model uses numbers I assumed, so change them to match your own work and see where your break-even lands. The studies above are about writing code, while the Coder Legion post is about AI features inside business tools, which is a related but different case. I read METR's two posts, the Stack Overflow survey results page and the arXiv abstract page for the Copilot experiment, but only the overview page of the DORA report, not the full report.&lt;/p&gt;

&lt;p&gt;How do you measure whether an AI tool really saves you time? A commenter on the original post asked the same thing, and I still don't have a method I would call reliable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://coderlegion.com/32840/ai-gives-the-right-answer-but-does-it-actually-save-time" rel="noopener noreferrer"&gt;BizzAppDev on Coder Legion, "AI Gives the Right Answer. But Does It Actually Save Time?"&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://metr.org/blog/2025-07-10-early-2025-ai-experienced-os-dev-study/" rel="noopener noreferrer"&gt;METR, early-2025 randomized trial (July 2025)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://metr.org/blog/2026-02-24-uplift-update/" rel="noopener noreferrer"&gt;METR, late-2025 follow-up and why its data is unreliable (February 2026)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://survey.stackoverflow.co/2025/ai" rel="noopener noreferrer"&gt;Stack Overflow Developer Survey 2025, AI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://arxiv.org/abs/2302.06590" rel="noopener noreferrer"&gt;GitHub Copilot controlled experiment, arXiv preprint&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dora.dev/research/2025/dora-report/" rel="noopener noreferrer"&gt;DORA 2025 report overview&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ai</category>
      <category>productivity</category>
      <category>softwaredevelopment</category>
      <category>programming</category>
    </item>
    <item>
      <title>Retry-Safe API Calls in Node.js: Idempotency Keys, Backoff and 429s</title>
      <dc:creator>APIHiver</dc:creator>
      <pubDate>Thu, 15 Oct 2026 20:17:51 +0000</pubDate>
      <link>https://dev.to/api_hiver/retry-safe-api-calls-in-nodejs-idempotency-keys-backoff-and-429s-50pc</link>
      <guid>https://dev.to/api_hiver/retry-safe-api-calls-in-nodejs-idempotency-keys-backoff-and-429s-50pc</guid>
      <description>&lt;p&gt;A user taps "Pay" twice. Or your request times out &lt;em&gt;after&lt;/em&gt; the server already processed it. Retry blindly and you may charge someone twice. Never retry and you lose real requests.&lt;/p&gt;

&lt;p&gt;This post shows a small Node.js client that retries the right things, waits the right amount, and keeps &lt;code&gt;POST&lt;/code&gt; requests safe.&lt;/p&gt;

&lt;h2&gt;
  
  
  The three rules
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Retry only what is safe to retry.&lt;/strong&gt; Network errors, timeouts, &lt;code&gt;408&lt;/code&gt;, &lt;code&gt;429&lt;/code&gt;, &lt;code&gt;502&lt;/code&gt;, &lt;code&gt;503&lt;/code&gt; and &lt;code&gt;504&lt;/code&gt;. Never retry &lt;code&gt;400&lt;/code&gt;, &lt;code&gt;401&lt;/code&gt;, &lt;code&gt;403&lt;/code&gt;, &lt;code&gt;404&lt;/code&gt; or &lt;code&gt;422&lt;/code&gt;: the same request will fail the same way.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Make &lt;code&gt;POST&lt;/code&gt; requests safe with an idempotency key.&lt;/strong&gt; Generate one key per &lt;em&gt;intended operation&lt;/em&gt; and reuse it on every retry of that operation. The server remembers the result for that key and returns it again instead of doing the work twice.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Wait intelligently.&lt;/strong&gt; Use exponential backoff with jitter, and obey the &lt;code&gt;Retry-After&lt;/code&gt; header when the server sends one.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frcgr9oxyb447nfgob90j.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frcgr9oxyb447nfgob90j.jpg" alt="Three attempts at the same request, each carrying the same idempotency key, ending in one single receipt" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;randomUUID&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;node:crypto&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;RETRYABLE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;408&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;429&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;502&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;503&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;504&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sleep&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ms&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ms&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;waitMs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;retryAfter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// 1. The server told us exactly how long to wait.&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;retryAfter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;seconds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;retryAfter&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isNaN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;seconds&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;date&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;retryAfter&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isNaN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;date&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// 2. Otherwise: exponential backoff (0.5s, 1s, 2s...) with full jitter.&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ceiling&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="nx"&gt;_000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="nx"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;ceiling&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;apiCall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;method&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;GET&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;headers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt; &lt;span class="nx"&gt;retries&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// One key per intended operation, reused on every retry below.&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;method&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nf"&gt;randomUUID&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;attempt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;attempt&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;method&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Content-Type&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;application/json&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="p"&gt;...(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Idempotency-Key&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;
          &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;AbortSignal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="nx"&gt;_000&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Network error or timeout: the server may or may not have seen it.&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;attempt&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;retries&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;waitMs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
      &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;204&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;RETRYABLE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;attempt&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;retries&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`HTTP &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;text&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;waitMs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;retry-after&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;payment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;apiCall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://api.example.com/v1/payments&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4900&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;currency&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;usd&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why each part is there
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The key is created outside the loop.&lt;/strong&gt; That is the whole trick. If you generate a new key per attempt, the server sees three different payments.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Timeouts count as retryable.&lt;/strong&gt; A timeout does not mean the request failed, only that you did not hear back. The idempotency key is what makes retrying safe.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Retry-After&lt;/code&gt; wins over our own guess.&lt;/strong&gt; When a server says "wait 20 seconds", waiting 1 second just earns another &lt;code&gt;429&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Jitter matters.&lt;/strong&gt; If 1,000 clients all retry after exactly 2 seconds, they hit the server together again. Random waiting spreads them out.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Client errors are not retried.&lt;/strong&gt; A &lt;code&gt;422&lt;/code&gt; means your data is wrong. Retrying will not fix it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fagegzbq6kpxdg351qduw.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fagegzbq6kpxdg351qduw.jpg" alt="Many phones retrying at the same instant overload a server, while retries spread out with random jitter keep it calm" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What the server must do
&lt;/h2&gt;

&lt;p&gt;A key only helps if the API honours it. On the server side:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Store the result of the first request under the key, scoped to the authenticated user.&lt;/li&gt;
&lt;li&gt;If the same key arrives again with the same body, return the stored response instead of repeating the work.&lt;/li&gt;
&lt;li&gt;If the same key arrives with a &lt;em&gt;different&lt;/em&gt; body, reject it with an error. That is a bug in the client.&lt;/li&gt;
&lt;li&gt;Expire old keys after a documented period. Many providers keep them for around a day, so check the docs of the API you use.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; only send &lt;code&gt;Idempotency-Key&lt;/code&gt; to an API that documents support for it. If it does not, retrying a &lt;code&gt;POST&lt;/code&gt; after a timeout can still create duplicates. In that case, retry only after checking whether the first request went through.&lt;/p&gt;

&lt;p&gt;Note that the &lt;code&gt;Idempotency-Key&lt;/code&gt; header is still an IETF draft rather than a finished standard, but it is widely used in practice.&lt;/p&gt;

&lt;h2&gt;
  
  
  Things I would add next
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A total time budget&lt;/strong&gt;, so retries stop after, say, 30 seconds no matter how many attempts remain.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Logging&lt;/strong&gt; the attempt number, status and key, so a support ticket can be traced.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A circuit breaker&lt;/strong&gt; for when a provider is down and every call is going to fail anyway.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Over to you
&lt;/h2&gt;

&lt;p&gt;How do you handle retries in your own API clients: a library, a hand-rolled helper like this one, or "we just hope for the best"? And has a duplicate request ever bitten you in production? I would like to hear the stories, and I will answer every comment.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>api</category>
      <category>node</category>
      <category>webdev</category>
    </item>
    <item>
      <title>REST APIs explained with a restaurant (and one real request you can run)</title>
      <dc:creator>APIHiver</dc:creator>
      <pubDate>Sun, 11 Oct 2026 22:13:41 +0000</pubDate>
      <link>https://dev.to/api_hiver/rest-apis-explained-with-a-restaurant-and-one-real-request-you-can-run-cme</link>
      <guid>https://dev.to/api_hiver/rest-apis-explained-with-a-restaurant-and-one-real-request-you-can-run-cme</guid>
      <description>&lt;p&gt;If you have ever been told "just call the API" and nodded without knowing what that meant, this post is for you. No jargon, one analogy, and one real request at the end.&lt;/p&gt;

&lt;h2&gt;
  
  
  The restaurant analogy
&lt;/h2&gt;

&lt;p&gt;Think of a restaurant.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;You&lt;/strong&gt; are the app that wants something.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The kitchen&lt;/strong&gt; is the server that has the data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The waiter&lt;/strong&gt; is the API.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You do not walk into the kitchen. You tell the waiter what you want, in a format the waiter understands, and the waiter brings back the dish. An API is that waiter: a fixed way for one program to ask another program for something.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;REST&lt;/strong&gt; is a popular set of rules for how that waiter behaves. REST APIs talk over the web (HTTP), so the "order" is just a web request and the "dish" is usually JSON.&lt;/p&gt;

&lt;h2&gt;
  
  
  What a request is made of
&lt;/h2&gt;

&lt;p&gt;Every request has four parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;A URL&lt;/strong&gt; says what you are asking for, like &lt;code&gt;https://api.example.com/users/42&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A method&lt;/strong&gt; says what you want to do. The four you will meet most: &lt;code&gt;GET&lt;/code&gt; (read), &lt;code&gt;POST&lt;/code&gt; (create), &lt;code&gt;PUT&lt;/code&gt; or &lt;code&gt;PATCH&lt;/code&gt; (update), &lt;code&gt;DELETE&lt;/code&gt; (remove).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Headers&lt;/strong&gt; carry extra info, like your API key, which proves who you are.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A body&lt;/strong&gt; (only for POST, PUT and PATCH) carries the data you are sending.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The reply has a &lt;strong&gt;status code&lt;/strong&gt; (&lt;code&gt;200&lt;/code&gt; means fine, &lt;code&gt;404&lt;/code&gt; means not found, &lt;code&gt;401&lt;/code&gt; means your key is missing or wrong, &lt;code&gt;429&lt;/code&gt; means you asked too often) and a &lt;strong&gt;body&lt;/strong&gt;, usually JSON.&lt;/p&gt;

&lt;h2&gt;
  
  
  What JSON looks like
&lt;/h2&gt;

&lt;p&gt;JSON is just labelled values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"amount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"from"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"USD"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"to"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"INR"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Curly braces hold a thing, each label sits in quotes, and a colon connects it to its value. If you can read a form, you can read JSON.&lt;/p&gt;

&lt;h2&gt;
  
  
  Send a real request
&lt;/h2&gt;

&lt;p&gt;Here is a currency conversion call. You need a free API key from the &lt;a href="https://apihiver.com/apis/currency-exchange-rates/overview" rel="noopener noreferrer"&gt;Currency Exchange Rates API on APIHiver&lt;/a&gt;, which is a marketplace where you can try APIs in the browser first.&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="s2"&gt;"https://currency-exchange-rates.p.apihiver.com/v1/convert?amount=100&amp;amp;from=USD&amp;amp;to=INR"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s2"&gt;"X-API-Key: YOUR_API_KEY"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reading it piece by piece:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;curl&lt;/code&gt; sends the request from your terminal.&lt;/li&gt;
&lt;li&gt;The URL says &lt;em&gt;convert&lt;/em&gt; and passes three options after the &lt;code&gt;?&lt;/code&gt;: how much, from which currency, to which currency.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-H "X-API-Key: ..."&lt;/code&gt; is the header with your key.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The reply is JSON with the converted amount. That is a complete REST API call: URL, method (GET is the default), header, JSON back.&lt;/p&gt;

&lt;h2&gt;
  
  
  The same call in JavaScript
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://currency-exchange-rates.p.apihiver.com/v1/convert?amount=100&amp;amp;from=USD&amp;amp;to=INR&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;X-API-Key&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;API_KEY&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note the key comes from an environment variable. &lt;strong&gt;Never paste a key into front-end code or a public repo.&lt;/strong&gt; Anyone can read it there and run up your bill.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three mistakes beginners make
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Hard-coding keys.&lt;/strong&gt; Use environment variables and keep them server-side.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ignoring the status code.&lt;/strong&gt; Check &lt;code&gt;res.ok&lt;/code&gt; before you trust the body.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Calling the API on every page view.&lt;/strong&gt; Cache results that rarely change. It is faster and saves your quota.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Where to go next
&lt;/h2&gt;

&lt;p&gt;I wrote a longer version of this guide with REST vs SOAP vs GraphQL, a status code table and a short quiz: &lt;a href="https://apihiver.com/blog/what-is-a-rest-api" rel="noopener noreferrer"&gt;What Is a REST API? A Beginner's Guide&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you have a question about a part that still feels fuzzy, ask in the comments and I will answer it.&lt;/p&gt;

</description>
      <category>api</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
