Python Friday #217: Path- and Query Parameters in FastAPI

FastAPI offers us two main options to specify what data we want: as part of the path or as query parameters. In this post we expand on our minimalistic example and dig a bit deeper into the basic concepts of FastAPI.

This post is part of my journey to learn Python. You find the code for this post in my PythonFriday repository on GitHub.

 

Query parameters

We can append our query parameters to the URL with this pattern:

The query starts with a ? and then follow the variables with their values, separated by a &.

We can add this little method to our main.py file to experiment with the query parameters:

If we access the URL http://localhost:8000/echo?a=1&b=Hello, we should see something like this:

FastAPI turns our parameters into a key/value pair and encodes it as JSON.

You can play with it and add more variables and you should get immediate feedback.

 

A more explicit usage of query parameters

If we want to create a small calculation function that creates the sum of the variables a, b, and c, we can write the function like this:

If we now go to http://localhost:8000/calculator?a=1&b=2&c=3, we get a result back that may not be what we expected:

We get the string 123 back instead of the result of the addition of 1 + 2 + 3.

The result is 123, then by default everything we get into FastAPI is a string. We could go and convert the values like this:

However, while that works, it is much easier to use type hints:

Whatever you choose, if you go back to the API, you now should get the correct result:

We now get 6 back, what is the value for 1 + 2 + 3.

 

Path parameters

The other option we can use to influence the results we get is a path parameter. For that we define our path and add {} around the parameters, like we would with f-strings:

We can now call our weather endpoint with the name of the city or a postal code, then FastAPI again interprets the parameter as a string:

We get back a JSON response that has our input as a string, even when we enter a number.

We get back a JSON response that has our input as a string when we enter a string

If we want our parameter to be something else than a string, we need to specify it:

If we run it now with a string while we expect an integer, we get an error:
Since abc is not a number, we get an error that tells us about the exception at the conversion to an int.

While type hints for python are hints, with FastAPI those hints are turned into checks and the parameters are validated against them. That gives us an input check for free.

 

Next

Type hints allow us to get the data that we want from the path and query parameters. If we are explicit on what we expect, we do not need to make the conversation by ourselves. Instead, FastAPI delegates this task to Pydantic and throws errors if the types do not match.

Next week we try something new and send data to FastAPI.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.