Exploring FastAPI: Handling Query and Path Parameters

Introduction

FastAPI, a modern Python web framework, empowers developers to create efficient and robust APIs with ease. Among its many features, FastAPI provides excellent support for handling query and path parameters. In this article, we will dive into the fundamentals of working with query and path parameters in FastAPI.

Path Parameters

  • Suppose you're building a web page for a library, and you want to create a route to display information about a specific book using its unique identifier. For example, displaying the page for "Book 1" with ID 1. You could create a FastAPI route like this:

      @app.get('/book')
      def book():
          return {f'This is a book page for book ID: 1'}
    
  • The output on the web browser is as follows:

  • This would work for displaying information about a single book. However, what if you have hundreds of books to showcase? This is where path parameters come into play. Path parameters allow you to create dynamic URLs. Instead of hardcoding the book ID, you make it a variable, like id, which is then passed as a function parameter:

@app.get('/book/{id}')
def book(id):
    return {f'This is a book page for book ID:  {id}'}
  • The output is as follows:

  • Now, you can visit various paths such as /book/1, /book/2, or /book/100 to retrieve information about different books.

Path Parameters with Type

  • We saw how we can add path parameters in FastAPI.

    But the problem is that we can also pass names in place of id.

  • For example if we hit the endpoint book/orchid The output is as follows:

  • We want that the ID should only be of integer type and user should see an error message if he enters name instead of numeric id. We can do so by defining type for path paramater. When the id is passed as a parameter to the function we need to specify it's type as int. Here is the code:

      @app.get('/book/{id}')
      def book(id:int):
          return {f'This is a book page for book ID:  {id}'}
    

Now if you give the endpoint /book/orchid the following output is obtained.

Query Parameters

  • Just like path parameters we have something which is called as query parameter.

  • Query parameter is something which we declare in the function but they are not part of the path. For example:

    
      @app.get('/book')
      def book(id:int):
          return {f'This is a book page for book ID:  {id}'}
    
  • In the above example id is only passed as a parameter in function and isn't present in the path. Earlier this "id" was present in the path also hence it was called as path parameter.

  • To pass a query parameter from web page we need to use ?<name_of_parameter>=<value_of_parameter>. For example /book?id=12. The output is as follows

If the user doesn't pass query parameter following output is displayed:

Conclusion

FastAPI is a versatile and powerful web framework for building APIs, and it excels in handling both query and path parameters. By using path parameters, you can create dynamic and user-friendly URLs while ensuring data validation through type hints. Query parameters, on the other hand, allow clients to customize their requests, enhancing the flexibility of your API.

With FastAPI, you can quickly create APIs that are not only efficient and robust but also user-friendly and well-documented. It's an ideal choice for developers of all levels looking to create Python APIs that perform exceptionally well.

By mastering these parameter types, you can make your FastAPI-powered APIs even more powerful and versatile.