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, path parameters can be used to pass values directly in the URL to retrieve specific resources. In this tutorial, we will show you how to create a GET endpoint and explore what path 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 to retreive data from a sample dataset. Additionally, we want to use path parameters to enable access to specific resources.

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 Path Parameters?

Path parameters are parts of a URL that capture dynamic values, allowing you to pass data directly through the URL itself. They're often used in web APIs to specify which resource you want to access.

FastAPI allows you to define the type of path parameters, enhancing code readability and adding validation. Supported types include:

  • str: for strings
  • int: for integers
  • float: for floating-point numbers
  • bool: for boolean values
  • Enum: for enumerated choices

Step 1: Import Libraries

First, we import the following python modules:

from fastapi import FastAPI

Step 2: Create FastAPI instance

Let's create a FastAPI instance:

app = FastAPI()

Step 3: Create GET Endpoint with Path Parameter

Imagine a system where you can find information about various online courses. To make the course information accessible, we want to create a GET endpoint and set up dynamic paths using Course IDs. For instance, a course with the ID c1 should be reachable via the path /courses/c1.

To do this, we would like to define a path parameter for the Course ID. The Course ID is a string value and can for example look like follows:

  • c1
  • c2
  • c3
  • c4
  • c5
  • ...

Let's set up an GET endpoint and define a path parameter course_id as str:

@app.get("/courses/{course_id}")
async def read_item(course_id: str):
    return {"course_id": course_id}

Step 4: Code Overview

Let's take a look at the entire code:

from fastapi import FastAPI

app = FastAPI()


@app.get("/courses/{course_id}")
async def read_item(course_id: str):
    return {"course_id": course_id}

Your FastAPI application is now implemented and can be run.

Step 4: Run the Application

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