Free Numbers Pricing Reviews Feedback Terms & Policy API Docs Blog About
Log in Get Started
2 views 3 min read
Back to Blog
Developers

Retries and Idempotency When You Buy Numbers Through an API

Retries and Idempotency When You Buy Numbers Through an API

A timeout is the most dangerous response an API can give. The request may have failed, or it may have succeeded and the answer got lost. If your code simply retries, it may buy a second number and charge you twice.

The problem

A buy call times out after twenty seconds. You do not know if an order exists. Retrying blindly can create a duplicate. Giving up can leave a paid order that you never read.

Safe rules

  1. Treat a buy timeout as unknown, not as failed.
  2. Before you buy again, check your own record and the provider's order list for an order made in the last few minutes.
  3. Keep a local order row with a unique reference before you call the API.
  4. Mark the row as confirmed only after the provider answers with an order ID.
  5. Run a small reconciliation job that finds rows stuck in the unknown state.

Which calls are safe to retry

CallSafe to retry?Why
Read an orderYesIt changes nothing
Buy a numberOnly after checkingA retry may create a second order
Cancel an orderYes, if you handle already cancelledA repeat cancel should not fail your job
ReorderOnly after checkingIt may create a new paid order

Backoff

  • Wait longer after each failure, for example one, two, four seconds.
  • Add a small random delay so many workers do not retry together.
  • Stop after a small number of attempts and alert a person.

Money checks

Compare your wallet balance before and after a batch. A difference you cannot explain is a bug in your retry logic and should raise an alert.

A simple reconciliation job

  1. Every few minutes, find local orders in the unknown state.
  2. Ask the provider for orders in the last hour.
  3. Match them by number or reference.
  4. Mark matches as confirmed and cancel orders you do not need.
  5. Alert a person for anything that does not match.

Test it before you need it

Simulate a timeout in a test and check that exactly one order exists afterward. Repeat with the provider returning an error. This is the test that saves real money.

Questions people ask

Does the API support idempotency keys?

Check the API documentation. If it does not, your local reference and reconciliation job do the same work.

How many retries are reasonable?

Two or three for reads, and none for buys until you have checked for an existing order.

Related guides and pages

Create a free OtpGet account and try it with a small balance.

Tags

#api retries #idempotency #duplicate orders api #timeout handling #safe retry #otp api reliability
Contact us