If you’re doing any type of web development in Python, there’s a good chance you’ve heard about FastAPI by now.
If not, go check it out now. It’s performant, intuitive and time saving (thanks to Pydantic / type hinting) and it supports API standards, e.g. OpenAPI (previously Swagger).
You cannot leave this powerful framework out of your toolset.
And although it’s true you can get an API running with FastAPI really fast and with little code, to build something more serious takes a bit of practice.
Hence we created a FastAPI learning path on our platform.
In 10 exercises you will write a simple API to track food / calories. This takes you from complete beginner to building your first CRUD (Create-Read-Update-Delete) API including authentication with JWT (JSON Web Tokens).
The following is a summary of the 10 FastAPI Bite exercises. Note that they build upon each other, becoming accessible as you solve more of them.
- 1) FastAPI Hello World
To get up and running you’ll write your first endpoint returning a dict. - 2) A little detour: Pydantic
You’ll learn to use Pydantic, which is an awesome library to do data validation and settings management using Python type annotations. New to type hints? Check out our article series here. - 3) Create food objects
With the basics of Pydantic learned, you will start creating food objects through a FastAPI endpoint. - 4) Retrieve food objects
Now that our API can add food objects, in this exercise you’re going to retrieve them.
To keep it simple this is all in memory. In a future learning path we’ll use a database for persistence (using SQLModel). - 5) Update and delete food objects
The last two actions for the the full food CRUD. We also have you add a bit of exception handling when a user tries to update a food object that does not exist. - 6) Pydantic part II
In order to measure daily food intake as a user, our current Food model is not enough. We also needUser
andFoodEntry
models. You will create those in this exercise. - 7) Food logging CRUD
In this exercise you will use the PydanticFoodEntry
model (which contains theFood
andUser
models as well). You will create the CRUD (create-read-update-delete) for this model which gives you a nice opportunity to practice again what you’ve learned in the first 5 FastAPI Bites. - 8) FastAPI Exception handling
In this Bite exercise we’ll take the API we built in the last exercise and have you add some more exception handling covering some specific scenarios. - 9) Return an HTML response
You use FastAPI primarily to build APIs, but it can also be used as a web framework (although we think Django is a better option for this). In this Bite you will return anHTMLResponse
with embedded HTML. - 10) FastAPI Authentication with JWT (JSON Web Tokens)
In this Bite you’ll lock down the API using FastAPI’s OAuth2 with Password (and hashing), Bearer with JWT tokens guide. Throughout this exercise you will add all the pieces to effectively make the API secure using JWT tokens and secure password hashing.
This last Bite is the most challenging one, but persist through and you’ll earn 10 points (as opposed to the regular 2-4 points per Bite) towards your next Pybites Ninja Belt.
Start our FastAPI learning path today, the exercises are free. Head over to https://codechalleng.es/bites/paths/fastapi and start coding!
We hope you enjoy this learning path and that it gives you enough practice to build your own API upon completing it.
When you do please share it on our Slack …