APIs with FastAPI · Chapter 6 of 26
CRUD in memory
By the end of this chapter you can build the four operations every API has (create, read, update, delete) over data kept in memory, choose between PUT and PATCH, answer 204 for a delete, say which methods are safe and which are idempotent, and refuse a change the thing's state does not allow. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: First API
- 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
Devika wants the spreadsheet gone: every parcel should live in the API, and merchants should manage their own. That means more than booking and reading. A merchant weighs a parcel again and fixes the weight. A customer cancels, and the parcel must be cancelled, unless a rider already has it. A test parcel is deleted. And merchants' apps live on shop Wi-Fi: a request times out, the app sends it again, and the API must give an answer that makes sense the second time too. This chapter keeps the data in a dictionary, so you can see every rule clearly; chapter 9 moves it to Postgres.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
crud.py: create, read, replace
Keep the parcels of parcels.json in memory, in a dictionary by AWB, and make an app with four routes: GET /parcels (every parcel, in the file's order), POST /parcels (creates one, answers 201), GET /parcels/{awb} (one parcel, 404 if it is not there) and PUT /parcels/{awb} (replaces what a merchant may change and answers the parcel, 404 if it is not there). Every parcel is answered with exactly awb, merchant_id, to_pincode, weight_grams, cod_amount and status.
POST takes merchant_id (M and two digits), to_pincode (6 digits), weight_grams (1 to 30000) and cod_amount (0 to 50000, 0 when not sent); the new parcel gets the next AWB after the file's highest and status booked. PUT takes to_pincode, weight_grams and cod_amount, all three required, and keeps awb, merchant_id and status.
- Task 2
patch.py: change only what was sent
Make an app over parcels.json (in memory, by AWB) with two routes: GET /parcels/{awb} answers the parcel, and PATCH /parcels/{awb} changes only the fields the body sends and answers the parcel (404 for either when the AWB is not there).
PATCH may send any of to_pincode (6 digits), weight_grams (1 to 30000) and cod_amount (0 to 50000), in any mix, or none at all; a value that breaks its rule answers 422. Sending cod_amount 0 sets it to 0.
- Task 3
cancel.py: cancel, but only before pickup
Make an app over parcels.json (in memory, by AWB) with two routes: GET /parcels/{awb} answers the parcel, and POST /parcels/{awb}/cancel cancels it. A booked parcel becomes cancelled and the route answers the parcel. A parcel already cancelled is answered as it is, with 200, so a retry is safe.
Any other status answers 409 with a detail that says the status, and changes nothing. An AWB that is not there answers 404.
- Task 4 · interview question
Interview: delete.py, delete twice
Make an app over parcels.json (in memory, by AWB) with two routes: GET /parcels/{awb} answers the parcel (404 if it is not there), and DELETE /parcels/{awb} removes it and answers 204 with no body (404 if it is not there). After a delete, GET answers 404, and a second DELETE of the same AWB answers 404 too. Other parcels stay as they are.
The interviewer will ask: is DELETE idempotent, and what should the second call answer?
What you will learn
- One resource, five methods: POST, GET, PUT, PATCH and DELETE on /riders.
- PUT replaces, PATCH changes part, and exclude_unset.
- 204 No Content for a delete, and what a second delete answers.
- Safe and idempotent methods, and why retries need them.
- State rules: a change the current state does not allow is a 409.
More in First API
Build "CRUD in memory" 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.