Member-only story
Create a fast auto-documented, maintainable and easy-to-use Python API — CRUD routes and routing management
Learn how to catch requests and obtain the information that they carry
In the previous article we’ve set up an API in 5 lines of code. We’ve install our dependencies and created a working API with just one simple route. In this article we’ll build further, fleshing out the API with multiple, different routes that’ll show you all the things you can do with FastAPI. Then we’ll focus on how to organize all of these routes in a concise and clear way
Setting up
To make this article as understandable as possible we’re going to pretend we’re coding an API for our blogging website. The goal is to create some common routes with which we can CRUD (create, read, update, delete):
- add new article
- retrieve articles from the database
- update articles to fix typo’s e.g.
- delete an article
We’ll focus on catching the requests and extracting the information. How our API communicates with our database is another topic that’s described perfectly well in this article. Also check out this article for creating a version controlled database structure.
Since our API will be responsible for catching the requests and obtaining the information that they carry, let’s first quickly check out how request work so that we can accurately create endpoints for our API to catch all the information.
The request
Let’s simplify a bit and say that a request consists of a URL
, a HTTP method
and optionally a body
. We’ll go through each of these quickly before implementing them in our API.
Dissecting a URL
I assume you are quite familiar with URLs since you have to type them into a browser when using the internet. For the sake of our API let’s split the…