APIs with FastAPI · Chapter 15 of 26
Async and external calls
By the end of this chapter you can call another company's API from your own without letting it slow you down or take you with it when it fails: say what async def and def mean to FastAPI, spot the call that freezes every request, use httpx.AsyncClient with a timeout, retry what is worth retrying, make two calls at once, and test all of it without the partner. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Security and real-world behaviour
- 4 tasks
- FastAPI
- In the all labs plan
The first 2 chapters of APIs with FastAPI are free. This one comes with the all labs plan.
Why you need this
DakiyaGo knows its own 35 pincodes. India has about 19,000. Imran signed up Pinmitra, a partner whose API knows every one: its post office, district, state, and whether couriers deliver there. Merchants' checkouts will ask DakiyaGo, and DakiyaGo will ask Pinmitra.
Until now every route answered from memory or from DakiyaGo's own database, in a few milliseconds. A call to a partner goes over the internet to someone else's server, and takes as long as they take. On a bad day that is thirty seconds, or forever. Aman has seen one slow partner freeze an entire API, health check and all, during a festival sale. This chapter is how that happens, and how to make sure it cannot.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
lookup.py: ask the partner
Pinmitra, a partner, knows every pincode in India. Make an app that reads PARTNER_URL from the environment when the file is imported (default http://127.0.0.1:9000), with an async def route GET /pincodes/{pincode}/check that asks the partner GET {PARTNER_URL}/pincodes/{pincode} with httpx.AsyncClient and answers {"pincode", "district", "state", "deliverable"} from the partner's answer, without its office. The partner's 404 answers 404.
Try it with partner.py, the lab's stand-in for Pinmitra: uvicorn partner:app --port 9000 &, then uvicorn lookup:app &, then curl -i localhost:8000/pincodes/400076/check. The check stands in for the partner itself, with pincodes that are not in DakiyaGo's files.
- Task 2
timeout.py: when the partner is slow, or down
Make the same app and route as task 1, GET /pincodes/{pincode}/check, with three rules for the partner call. A timeout of 2 seconds: no answer by then answers 504 {"detail": "The pincode partner did not answer in time"}, and a timeout is not retried. A connection error or a 5xx answer is tried once more, and failing again answers 502 {"detail": "The pincode partner failed"}. A 404 is still a 404, and a good answer is as in task 1.
partner.py takes 5 seconds for 744101 (Port Blair), to try the timeout by hand. The check's partner times out, refuses connections and answers 503, and counts how many times you asked.
- Task 3
together.py: two partner calls at once
A checkout asks whether DakiyaGo can carry a parcel from one pincode to another: two questions to the partner, each about half a second. Make an app (PARTNER_URL as in task 1, a 2-second timeout as in task 2) with GET /routes/check?from_pincode=...&to_pincode=... that asks the partner about both pincodes at the same time and answers {"from": ..., "to": ..., "deliverable": ...}, where from and to are each pincode's answer as in task 1, and deliverable is true only if both are. A pincode the partner does not know answers 404.
The check's partner takes 0.6 seconds per answer and counts how many of your calls are running at the same moment.
- Task 4 · interview question
Interview: frozen.py, the label that froze the API
print_label in the starter is the label vendor's SDK: it blocks for a second while it renders, and you cannot change it. Make an app with GET /health answering {"status": "ok"} and GET /labels/{awb} answering {"awb": ..., "label": <what print_label(awb) returns>}.
The check runs your app for real with uvicorn (one process), asks for two labels at once, and asks GET /health while they print: health must answer at once, and the two labels must take about one second together, not two. The interviewer will ask why one time.sleep froze every request, and two ways to fix it.
What you will learn
- async def and def in FastAPI: the event loop and the thread pool.
- What blocks the event loop, and why one blocking call stops everyone.
- httpx.AsyncClient: calling a partner and awaiting the answer.
- Timeouts and retries: 504 and 502, and which failures to try again.
- Two calls at once with asyncio.gather, and testing with respx.
More in Security and real-world behaviour
Build "Async and external calls" yourself
DakiyaGo, a Jaipur courier startup, takes bookings on WhatsApp. Merchants want an API, and you build it. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of APIs with FastAPI are free. This one comes with the all labs plan.