Introduction

FastAPI is a modern web framework that empowers developers to build web APIs quickly and efficiently using Python. A GET endpoint allows clients to retrieve data from a server. In a GET request, query parameters allow passing additional information to an endpoint via the URL's query string, typically for filtering or customizing the response. In this tutorial, we will show you how to create a GET endpoint and explore what query parameters are, how to implement them in FastAPI, and provide a practical example to illustrate their use.

Goal

💡
We want to create a GET endpoint for providing data from a sample dataset. Additionally, we want to use query parameters to enable dynamic requests with filters.

Prerequisites

☑️ Set up a virtual environment in the root of your project directory

Set up a Python Virtual Environment on Mac: A Step-by-Step Guide
Introduction In this tutorial, we show you step-by-step how to set up a Python virtual environment on your Mac. Why Use a Virtual Environment? A virtual environment allows you to create isolated environments for different Python projects. This helps to avoid conflicts between project dependencies and ensures that each project

☑️ Set up a FastAPI project

How to set up a FastAPI Project
Introduction FastAPI has quickly gained popularity as a modern, fast and easy-to-use Python web framework for building RESTful APIs. In this tutorial, we show you step-by-step how to set up a FastAPI project. Prerequisites First of all, make sure you have Python installed on your system. Furthermore, it is recommended

What is a GET Endpoint?

GET endpoint is a specific URL in an API where clients can request data from the server.

What are Query Parameters?

In FastAPI, query parameters are used to send extra information in the URL to help customize the response from an endpoint. You define them as parameters in your route function, which allows you to set default values and types. When a user makes a request, FastAPI automatically reads these parameters and passes them to your function, making it easy to handle user input.

Step 1: Import Libraries

First, we import the following python modules:

from fastapi import FastAPI, HTTPException

Step 2: Define Sample Data

Imagine a system where you can find information about various online courses. Let's define a sample database:

sample_courses_db = [
    {"course_id": "c1", "course_name": "Data Science"}, 
    {"course_id": "c2", "course_name": "Data Engineering"}, 
    {"course_id": "c3", "course_name": "Data Analytics"}
    ]

Step 3: Create FastAPI instance

Let's create a FastAPI instance:

app = FastAPI()

Step 4: Create GET Endpoint with Query Parameter

Now, we want to enable querying specific courses using a query parameter. Specifically, it should be possible to retrieve course details based on the course_id. So we want to define a query parameter course_id. Let's create a GET endpoint that meets the following requirements:

  • The query parameter course_id should be optional.
  • If the course_id doesn't exist in the database, a 404 error with an error message should be raised.
  • If no course_id is specified, all courses should be returned.

Let's set up an GET endpoint and define a query parameter course_id:

You can view this post with the tier: Academy Membership

Join academy now to read the post and get access to the full library of premium posts for academy members only.

Join Academy Already have an account? Sign In