Open In App

Introduction to FastAPI And Installation

Last Updated : 29 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

FastAPI is a modern and high-performance web framework for building APIs with Python. It is designed to be simple, fast and developer-friendly. Built on top of Starlette (for web routing) and Pydantic (for data validation), FastAPI provides everything you need to create robust APIs quickly.

Features of FastAPI

Here are the five most important features that make FastAPI popular:

  1. High Performance: Built on modern Python async features, FastAPI handles thousands of requests per second with ease.
  2. Type Safety: Uses Python type hints to validate inputs automatically, reducing developer mistakes.
  3. Automatic Documentation: Generates interactive API docs (/docs and /redoc) without extra work.
  4. Asynchronous Support: Fully supports async and await, making it great for real-time applications.
  5. Built-in Security: Supports authentication methods like OAuth2, JWT and API keys out of the box.

Installation for FastAPI

Setting up FastAPI is simple and quick. Let’s go step by step:

Step 1: Install FastAPI

Use pip to install the FastAPI framework:

pip install FastAPI

Step 2: Install an ASGI Server (Uvicorn)

FastAPI needs an ASGI server to run. The most common and recommended one is Uvicorn. So, run the following command in terminal:

pip install "uvicorn[standard]"

Step 3: Write Your First FastAPI App

Create a file named main.py and add the following code:

Python
from fastapi import FastAPI

# Create FastAPI app instance
app = FastAPI()

# Define a simple GET endpoint
@app.get("/")
def read_root():
    return {"message": "Hello World"}

Explanation:

  • from fastapi import FastAPI: Imports the FastAPI class.
  • app = FastAPI(): Creates an application instance (used by Uvicorn to run the server).
  • @app.get("/"): Defines the root (/) route for GET requests.
  • def read_root(): A function that runs when the route is accessed.
  • return {"message": "Hello World"}: Returns a JSON response.

Step 4: Run the FastAPI App

Run the server using Uvicorn:

uvicorn main:app --reload

Here’s what each part means:

  • main: file name (main.py).
  • app: FastAPI instance inside the file.
  • --reload: Automatically restarts the server when you make code changes.

Step 5: Open in Browser

Once the server starts, you will see a message in the terminal showing that Uvicorn is running (like below). This means your FastAPI app is up and ready:

Screenshot-(237)

Now, let’s test it in the browser:

http://127.0.0.1:8000/ -> Displays your API response.
http://127.0.0.1:8000/docs -> Opens Swagger UI (interactive docs).
http://127.0.0.1:8000/redoc -> Opens ReDoc (alternative docs).

When you visit http://127.0.0.1:8000/, you will see the JSON response from your FastAPI app, like this:

Screenshot-(238)
localhost:8000

That’s it! You have successfully set up FastAPI, created your first API endpoint and explored the automatic documentation.


Article Tags :

Explore