case studies

How to Develop Against a Metered or Rate Limited API With CodeInstances

Some APIs you can’t call as often as you’d like. A payments sandbox that throttles you after a burst. A maps or weather API billed per request. A partner’s staging server that falls over every afternoon. You’re working on the part of your program that handles the answers (the formatting, the error messages, the retry logic), and each try means another call you’d rather not make.

Record the program once against the real API, and work against the recording for the rest of the day:

codeinstances record -o forecast.instance -- python3 forecast.py --city Lisbon --days 5

That run makes the real calls and keeps them. From then on, each try is a replay:

codeinstances replay forecast.instance

Your program’s first run goes to the metered API with codeinstances record, and the answers are kept in api.instance. Then, while working on the code that handles the answers, every run is a replay: formatting, errors, retry, layout, tests. The limit: a request that changes isn’t in the recording, so record again when what you send changes. Recorded error answers, and failed calls as a 502, replay the same way, so error paths can be worked on too.

Your program gets the same answers it got from the API, without a single call going out, so the rate limit, the bill and the afternoon outage stop mattering. Change how the output is laid out, replay, look at it, change it again. The replay report will say the output differs from the recording, which is expected here, since changing the output is the work. What you’re checking is that the requests still match: the report lists any request that isn’t in the recording, and any recorded one that was never made.

Error Paths Without Waiting for Errors

The answers you need most are often the bad ones, and a healthy API won’t hand you a 429 when you want one. You don’t have to wait for a bad hour. Fork the run at the call you care about, offline, and give it the answer yourself:

codeinstances fork -at 3 -offline -answer 429.json -o slow-down.instance forecast.instance

with 429.json holding something like {"status": 429, "header": {"Retry-After": ["30"]}, "body": "slow down"}. Calls 1 and 2 get the API’s real answers, call 3 gets the 429, and nothing goes out. An answer of {"error": "connection refused"} tries the case where the API doesn’t answer at all. Each fork is kept as a run of its own, slow-down.instance here, so you can replay it offline as often as you like while you work on the retry logic.

Real bad answers count too. If the API refused a request while you were recording, the 429 or 500 it sent back is in the file and replays the same way.

Pay Only for the Calls That Changed

Replay only answers requests it has seen. It matches each request on method, URL and body, so when your change alters what the program sends (a new parameter, an extra field in a POST body), that request isn’t in the recording and gets a 502. Before, that meant recording the whole run again.

If the change only touches the later calls, fork instead:

codeinstances fork -at 4 forecast.instance

Calls 1 to 3 are answered from the recording, at no cost, and from call 4 on the program talks to the real API. You pay for the calls that changed and nothing else. If the program changed what it sends before step 4, those calls go out live too, and fork tells you so.

Where It Stops Working

The same matching rule catches a common snag: programs that put today’s date or a random id in the request. Every run sends something new, so nothing matches. In Go, take the date and the id from the hook package (hook.Now(), hook.Uint64()) and they’re recorded and replayed too. In other languages, pass the date in as an argument while you work. Streaming endpoints are buffered whole, and an API key sent in the URL rather than a header stays in the file, so don’t share the file around.