The Ultimate FastAPI Tutorial Part 3 - Query Parameters
In part 3 of the FastAPI tutorial, we'll look at an API endpoint with query parameters
Introduction
Welcome to the Ultimate FastAPI tutorial series. This post is part 3. 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 API. The series is designed to be followed in order, but if you already know FastAPI you can jump to the relevant part.
Code
Project github repo directory for this part of the tutorial
Tutorial Series Contents
Optional Preamble: FastAPI vs. Flask
Beginner Level Difficulty
Part 1: Hello World
Part 2: URL Path Parameters & Type Hints
Part 3: Query Parameters
Part 4: Pydantic Schemas & Data Validation
Part 5: Basic Error Handling
Part 6: Jinja Templates
Part 6b: Basic FastAPI App Deployment on Linode
Intermediate Level Difficulty
Part 7: Setting up a Database with SQLAlchemy and its ORM
Part 8: Production app structure and API versioning
Part 9: Creating High Performance Asynchronous Logic via async def
and await
Part 10: Authentication via JWT
Part 11: Dependency Injection and FastAPI Depends
Part 12: Setting Up A React Frontend
Part 13: Using Docker, Uvicorn and Gunicorn to Deploy Our App to Heroku
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)
Post Contents
Practical Section - An Endpoint With Query Params
Further Reading - The Typing Module
Practical Section - An Endpoint With Query Params
If you haven’t already, go ahead and clone the example project repo.
See the README
file for local setup.
In the app/main.py
file, you will find the following new code:
Let’s break this down:
- We have our toy dataset (later this will go into a database and be expanded)
- We’ve created a new
GET
endpoint/search/
. Notice it has no path parameters, which we looked at in part 2 - The
search_recipes
function defines the logic for the new endpoint. Its arguments represent the query parameters to the endpoint. There are two arguments:keyword
andmax_results
. This means that a (local) query with both of these query parameters might look like:http://localhost:8001/search/?keyword=chicken&max_results=2
- Notice that for each argument, we specify its type and default. Both are
Optional
which comes from the Python standard librarytyping
module. FastAPI is able to use these native Python type declarations to understand that the parameter does not need to be set (if we wanted the parameters to be mandatory, we would omit theOptional
) - Both parameters also have a default, specified via the
=
sign, for example, themax_result
query parameter default is10
. If these parameters are not specified in the request, the default value will be used. - We implement some basic search functionality using Python list slicing to limit results
- We use the Python filter capability for a very basic keyword search on our toy dataset. After our search is complete the data is serialized to JSON by the framework.
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
Navigate to the swagger UI at localhost:8001/docs
Give the endpoint a try:
- Expand the GET endpoint by clicking on it
- Click on the “Try It Out” button
- Enter the value “chicken” for the keyword
- Press the large “Execute” button
- Press the smaller “Execute” button that appears
After pressing the execute
button (which just makes a GET
request via curl under the hood), you
can scroll down to see the response from FastAPI:
Notice that only the two chicken recipes were returned. Try again with the keyword cauliflower
to see
the “Cauliflower and Tofu Curry” recipe returned. Then try adjusting the max_results
parameter.
Further Reading - The Typing Module
- Since Python 3.5, the standard library has included the
typing
module - Type declarations can be checked with a tool called mypy. This is a really useful cheatsheet
Continue Learning FastAPI
In the next part of the tutorial, we’ll cover more advanced endpoint input and output validation with Pydantic models, as well as dealing with POST endpoints and request body data.