FastAPI - Beginner to Pro / Project 1 - FastAPI Request Method Logic

Get HTTP Request Method Overview

๐Ÿ› ๏ธ Creating a FastAPI Application

In this course, we will use Visual Studio Code as our primary editor. You can also use any IDE youโ€™re comfortable with, such as PyCharm.

In FastAPI, a GET request fetches data from the server. It does not modify any data โ€” it simply retrieves information.

Step 1: Create a FastAPI Project (Quick Setup)

  1. Create a new project folder FastAPI

Through the desktop โ†’

Through the terminal (recommended) โ†’

  1. Open it in your editor (for this course, weโ€™ll be using VS Code).

  2. Create a virtual environment to isolate your project from other projects (recommended).

mac users โ†’

// Create.
python3 -m venv fastapienv

// Activate.
source fastapienv/bin/activate

windows users โ†’

// Create
python -m venv fastapienv

// Activate
fastapienv/scripts/activate.bat ---> command prompt
fastapienv/scripts/activate.ps1 ---> powershell
  1. Install FastAPI and Uvicorn.

mac users โ†’

pip3 install fastapi
pip3 install "uvicorn[standard]"

// pip3 ensures pip is using the same Python version as python3.

windows users โ†’

pip install fastapi
pip install uvicorn[standard]

Step 2: Create the Application File

Create a file named books.py inside your project folder.

๐Ÿ“‚ Project Structure

FastAPI/
 โ””โ”€โ”€ books.py

Step 3: Write Your First FastAPI Code

๐Ÿ—‚๏ธ books.py

from fastapi import FastAPI
# Import the FastAPI class to create an API application.

app = FastAPI()
# Create an instance of the FastAPI app.

@app.get("/api-endpoint")
# Define a GET endpoint at "/api-endpoint."
async def first_api():
    return {"message": "Hello world"}
# Function that returns a JSON response.

๐Ÿง  Understanding Our Python Function

To make our Python function accessible from the web, we connect it to an HTTP GET request using a decorator (@).

@app.get("/api-endpoint")

Here, /api-endpoint is the URL path where this function will be available.

Letโ€™s break down the function we created:

async def first_api():
    return {"message": "Hello world"}

๐Ÿ”น The async keyword makes the function asynchronous, but it is optional.

FastAPI can handle async behavior automatically when needed.

๐Ÿ”น Apart from that, this is just a normal Python function that returns data (a dictionary).


๐Ÿš€ Starting the FastAPI Application

Run this command in the terminal:

uvicorn books:app --reload

๐Ÿ”ง What this command means:

  • uvicorn โ†’ The server that runs FastAPI apps.

  • books โ†’ Name of the Python file (books.py).

  • app โ†’ The FastAPI instance inside that file.

  • - - reload โ†’ Automatically restarts the server when you change code.

๐Ÿ’ป**terminal**



๐ŸŒ Accessing the Endpoint from the Browser

When you start the FastAPI app, a local server runs at:

๐Ÿ”— 127.0.0.1:8000

To call the function, open this URL in your browser:

๐ŸŒ 127.0.0.1:8000/api-endpoint

You will see the response:

{"message":"Hello world"}

๐ŸŒ**browser**


๐Ÿ” Returning a List of Books Instead of a Message

Now letโ€™s return real data instead of a simple text response.

๐Ÿ“š Sample Book Data

๐Ÿ—‚๏ธ books.py

from fastapi import FastAPI

app = FastAPI()

BOOKS = [
    {"title": "Title One", "author": "Author One", "category": "science"},
    {"title": "Title Two", "author": "Author Two", "category": "science"},
    {"title": "Title Three", "author": "Author Three", "category": "history"},
    {"title": "Title Four", "author": "Author Four", "category": "math"},
    {"title": "Title Five", "author": "Author Five", "category": "math"}
] #sample book data.

@app.get("/api-endpoint") #we will update this soon.
async def first_api():
    return {"message": "Hello world"} #returns a message.

๐Ÿ”„ Update the Endpoint

โŒ Old endpoint

@app.get("/api-endpoint") #old endpoint.
async def first_api():
    return {"message": "Hello world"}

โœ… New endpoint

@app.get("/books") #updated endpoint.
async def read_all_books():
    return BOOKS #returns a list of books.

โœ… Final Result

๐Ÿ—‚๏ธ books.py

from fastapi import FastAPI

app = FastAPI()

BOOKS = [
    {"title": "Title One", "author": "Author One", "category": "science"},
    {"title": "Title Two", "author": "Author Two", "category": "science"},
    {"title": "Title Three", "author": "Author Three", "category": "history"},
    {"title": "Title Four", "author": "Author Four", "category": "math"},
    {"title": "Title Five", "author": "Author Five", "category": "math"}
]

@app.get("/books")
async def read_all_books():
    return BOOKS

โœ… View All Books

Restart the server and open:

๐ŸŒ 127.0.0.1:8000/books

NOTE โ†’

If you have an issue restarting the server, kill the terminal,

ctrl + C

then start the terminal again.

uvicorn books:app --reload

๐ŸŒ**browser**

You will now see a JSON response containing all books.


๐Ÿ“Œ Summary

  • GET requests are used to fetch data from the server.

  • FastAPI connects URLs to Python functions using decorators โ†’ @

  • Uvicorn runs the application locally.

  • Endpoints can easily return different data (like a list of books).

This simple pattern is the foundation for building real-world FastAPI applications.