Using FastAPI
00:00 Fast API. Fast API is a Python web framework that’s optimized for building APIs. It uses Python type hints and has built-in support for asynchronous operations.
00:13 FastAPI is built on top of Starlette and Pydantic and is extremely performant. You’ll need to install FastAPI with pip. Make sure you do this into the virtual environment you created earlier in the course.
00:38
With all these requirements in place, it’s time to start coding an example REST API with FastAPI. Create a fastapi/
folder and in it create a fastapi/app.py
file and start creating your FastAPI API.
00:56 This application uses the features of FastAPI to build a REST API for the same country data you’ve seen in the previous examples.
01:04 You’ll notice some similarities between FastAPI and the Flask application. Like Flask, Fast API has a focused feature set. It doesn’t try to handle all aspects of web application development.
01:15 It’s designed to build APIs with modern Python features.
01:21
Country
extends BaseModel
. It describes the data in the REST API.
01:37 This is an example of a Pydantic model. Pydantic is a data validation library, which is an integral part of Fast API. Pydantic models provide some helpful features in FastAPI.
01:50 They use Python type annotations to enforce the data type for each field in the class. This allows FastAPI to automatically generate JSON with the correct data types for API endpoints.
02:02 It also allows FastAPI to validate incoming JSON.
02:07
There’s a lot going on in the first line of this code. Here you see country_id
, which stores an integer for the ID of the country. It uses the Field
function from Pydantic to modify the behavior of country_id
.
02:20
In this example, you are passing Field
the keyword arguments default_factory
and alias
. The first argument, default_factory
, is set to find_next_id
.
02:31
This argument specifies a function to run whenever a new country is created. The return value will be assigned to country_id
. The second argument, alias
, is set to id
.
02:43
This tells FastAPI to output the key id
instead of country_id
in the JSON. This alias also means you can use id
when you create a new country.
02:55
You can see this in the countries
list. Like Flask, Fast API doesn’t have built-in database tools or an ORM so this example application hardcodes the country data into a list of objects to simplify the code and remain focused on FastAPI.
03:14
This list contains three instances of Country
for the initial countries in the API. Pydantic models provide some great features and allow FastAPI to easily process JSON data.
03:25
Next, there are two API functions in this application. The first, get_countries()
, returns a list of countries for GET requests to /countries
. Fast API will automatically create JSON based on the fields in the Pydantic model and set the right JSON data type from the Python type hints.
03:45
The Pydantic model also provides a benefit when you make a POST request to /countries
. You can see in the second API function here that the parameter country
has a Country
annotation.
03:55
This type annotation tells FastAPI to validate the incoming JSON against Country
.
04:02 You now have everything in place to use your FastAPI app. So in the next section of the course, you’ll see how to do this.
Become a Member to join the conversation.