The Ultimate FastAPI Tutorial Part 1 - Basic API Endpoints
In part 1 of the FastAPI tutorial, we'll look at a basic API endpoint
Welcome to the Ultimate FastAPI tutorial series. This post is part 1. The series is a project-based tutorial where we will build a cooking recipe API. Each post gradually adds more complex functionality, showcasing the capabilities of FastAPI, ending with a realistic, production-ready REST API. The series is designed to be followed in order, but if you already know FastAPI you can jump to the relevant part.
Tutorial Series Contents
Beginner Level Difficulty
Intermediate Level Difficulty
Part 14: Using Docker and Uvicorn to Deploy Our App to IaaS (Coming soon)
Part 15: Exploring the Open Source Starlette Toolbox - GraphQL (Coming soon)
Part 16: Alternative Backend/Python Framework Comparisons (i.e. Django) (Coming soon)
Practical Section - Hello World
If you haven’t already, go ahead and clone the example project repo.
README file for local setup.
app/main.py file, you will find the following code:
Let’s break this down:
- We instantiate a FastAPI
appobject, which is a Python class that provides all the functionality for your API.
- We instantiate an
APIRouterwhich is how we can group our API endpoints (and specify versions and other config which we will look at later)
- By adding the
@api_router.get("/", status_code=200)decorator to the
rootfunction, we define a basic GET endpoint for our API.
- We use the
include_routermethod of the
appobject to register the router we created in step 2 on the FastAPI object.
__name__ == "__main__"conditional applies when a module is called directly, i.e. if we run
python app/main.py. In this scenario, we need to import
uvicornsince FastAPI depends on this web server (which we’ll talk more about later)
Having done all that (and followed the
README setup instructions), you can run
the code in the example repo with this command:
poetry run ./run.sh
If you navigate to localhost:8001 you should see:
If you can see the “Hello, World!” response, then your API is working. Next up, visit localhost:8001/docs and you should see a screen like this:
This is the interactive documentation FastAPI offers out of the box, because the framework is built around OpenAPI standards. These docs pages are interactive, and will increase in detail as we add more endpoints and describe the expected input/output values in our code.
Give your endpoint a try:
- Expand the GET endpoint by clicking on it
- Click on the “Try It Out” button
- Press the large “Execute” button
- Press the smaller “Execute” button that appears
You can see the API response body (our “Hello, World!” message), as well as the
that FastAPI has run under the hood for you. We will use this functionality throughout
the tutorial series to easily inspect our endpoints.
Further Reading: Automatic Documentation
FastAPI is carefully built around the OpenAPI Specification (formerly known as swagger) standards. In FastAPI, by coding your endpoints, you are automatically writing your API documentation. Under the hood, FastAPI maps your endpoint details to a JSON Schema document. The generated documentation can (if given enough detail) display:
- Path operations
- body requests
- security details such as required headers
Out of the box, can choose what your preferred documentation UI display as either:
Both of these options offer interactive documentation pages where you can input request data and trigger responses which is handy for bits of manual QA.
There are added benefits such as automatic client generation.
Continue Learning FastAPI
In the next part of the tutorial, we’ll start adding endpoints with URL path parameters.