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

Project: Create Fast API Endpoint

๐Ÿš€ Create and Run a Basic FastAPI App

Open your FastAPI project folder, the one that already has your virtual environment, in Visual Studio Code or any editor you prefer.

Create a Python file named books.py ( if already created, skip )

๐Ÿ“‚ Project Structure

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

๐ŸงฉCreating the FastAPI Application

๐Ÿ—‚๏ธ books.py

from fastapi import FastAPI

# Import the FastAPI class

app = FastAPI()

# Create an instance of the FastAPI application

@app.get("/")
# Define a GET endpoint for the root URL
async def first-api():
    return {"message": "Hello programmer"}

# Return a JSON response

โœ… What this does

  • FastAPI() creates the application

  • @app.get("/") connects the function to the root URL /

  • Opening the URL will return a simple message in JSON format


โ–ถ๏ธ Run the Application

Make sure your virtual environment is activated, then run:

uvicorn main:app --reload

๐ŸŒbrowser

After starting, open your browser and go to:

๐Ÿ”— 127.0.0.1:8000

โน๏ธ Stopping the Server

To stop the server, press:

CTRL + C

โš™๏ธ Alternative Ways to Run FastAPI (Newer Versions)

If you are using a newer version of FastAPI, you can also start the application using:

fastapi run [python_file_name]
// Ex: fastapi run books.py

or

fastapi dev [python_file_name]
// Ex: fastapi dev books.py

๐ŸŒ Accessing the Application in the Browser

Once the server is running, follow the URL displayed in the terminal or manually open it in your browser.

Application URL:

127.0.0.1:8000/

Your default browser will display the response:

๐ŸŒbrowser

If we remove the async keyword from the function, the application will still behave the same way.

๐Ÿ—‚๏ธ books.py

from fastapi import FastAPI

app = FastAPI()

@app.get("/api-endpoint")
def first-api():
    return {"message": "Hello programmer"}

The browser still returns the same response, confirming that async is optional in FastAPI for simple operations.

๐ŸŒ browser

This demonstrates that FastAPI supports both synchronous and asynchronous route handlers.


๐Ÿ”€ Changing the API Endpoint

Letโ€™s now change the endpoint path.

"/" โ†’ "api_endpoint"

from fastapi import FastAPI

app = FastAPI()

@app.get("/api-endpoint")
async def first-api():
    return {"message": "Hello programmer"}

If we attempt to access the root URL:

โŒ 127.0.0.1:8000/

The browser displays an error, and the terminal shows a 404 Not Found response.

This occurs because the root endpoint (/) no longer exists.

๐ŸŒbrowser

From the terminal, we see a 404 Not Found error

๐Ÿ’ปterminal

When we navigate to the correct endpoint:

โœ… 127.0.0.1:8000/api-endpoint โ†’

The browser successfully returns the expected message:

๐ŸŒbrowser


๐Ÿ“ Summary:

  • Created a books.py file inside the FastAPI project folder.

  • Initialized the application using FastAPI().

  • Defined a GET endpoint using @app.get("/") to return a JSON response.

  • Ran the application with uvicorn main:app --reload (after activating the virtual environment).

  • Accessed the app in the browser via 127.0.0.1:8000.

Key Concepts Covered

  • โœ… How FastAPI creates an application instance

  • โœ… How endpoints are defined and mapped to URLs

  • โœ… How to start and stop the development server

  • โœ… async is optional for simple route handlers

  • โœ… Changing the endpoint path affects URL access

  • โœ… Accessing a non-existent route returns 404 Not Found

This section establishes the foundational workflow for building and running a FastAPI application.


โœ… Current Complete Code

๐Ÿ—‚๏ธ books.py

from fastapi import FastAPI

app = FastAPI()

@app.get("/api-endpoint")
async def first-api():
    return {"message": "Hello programmer"}