DEV Community

Vikas S
Vikas S

Posted on

FastAPI Tutorial Series Part 1: Installation and Your First Endpoint

FastAPI Tutorial Series Part 1: Installation and Your First Endpoint

Welcome to this FastAPI tutorial series! Whether you're new to web development or coming from another framework, this series will guide you through building modern APIs with FastAPI.

What is FastAPI?

FastAPI is a modern, high-performance web framework for building APIs with Python. It's designed to be easy to use while being incredibly fast and production-ready.

What You'll Need

  • Python 3.7 or higher installed on your computer
  • A text editor (VS Code, PyCharm, or any editor you prefer)
  • Basic Python knowledge (variables, functions, classes)

Installing FastAPI

First, let's create a new project directory and set up a virtual environment:

# Create project directory
mkdir fastapi-tutorial
cd fastapi-tutorial

# Create virtual environment
python -m venv venv

# Activate virtual environment
# On Windows:
venv\Scripts\activate
# On Mac/Linux:
source venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

Now install FastAPI and uvicorn (the server we'll use to run our app):

pip install fastapi uvicorn[standard]
Enter fullscreen mode Exit fullscreen mode

Your First FastAPI Application

Create a new file called main.py and add this code:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"message": "Hello, FastAPI!"}
Enter fullscreen mode Exit fullscreen mode

Let's break down what's happening:

  • from fastapi import FastAPI: Import the FastAPI class
  • app = FastAPI(): Create an instance of the FastAPI application
  • @app.get("/"): This is a decorator that tells FastAPI to handle GET requests to the root path
  • def read_root(): A regular Python function that returns what you want to send to the user
  • return {"message": "Hello, FastAPI!"}: Returns a dictionary that FastAPI automatically converts to JSON

Running Your Application

In your terminal, run:

uvicorn main:app --reload
Enter fullscreen mode Exit fullscreen mode

This command means:

  • main: The file name (main.py)
  • app: The FastAPI instance we created
  • --reload: Automatically restart the server when you make changes (great for development)

You should see output like:

INFO:     Uvicorn running on http://127.0.0.1:8000
INFO:     Application startup complete.
Enter fullscreen mode Exit fullscreen mode

Testing Your API

Open your browser and go to http://127.0.0.1:8000

You should see:

{"message": "Hello, FastAPI!"}
Enter fullscreen mode Exit fullscreen mode

The Magic of Automatic Documentation

Now visit http://127.0.0.1:8000/docs

You'll see an interactive API documentation page called Swagger UI. This was generated automatically by FastAPI! You can test your API directly from this page.

Also try http://127.0.0.1:8000/redoc for an alternative documentation style.

Adding More Endpoints

Let's add a few more endpoints to our main.py:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"message": "Hello, FastAPI!"}

@app.get("/about")
def about():
    return {"app": "FastAPI Tutorial", "version": "1.0"}

@app.get("/greet/{name}")
def greet(name: str):
    return {"message": f"Hello, {name}!"}
Enter fullscreen mode Exit fullscreen mode

Save the file, and thanks to --reload, the server will restart automatically.

Now you can:

  • Visit http://127.0.0.1:8000/about
  • Visit http://127.0.0.1:8000/greet/John (or replace John with any name)

Notice how {name} in the path becomes a parameter in your function. FastAPI automatically extracts it and passes it to your function.

Understanding the Response

FastAPI automatically:

  • Converts your Python dictionary to JSON
  • Sets the correct Content-Type header
  • Handles the HTTP response

You don't need to manually create JSON or set headers.

Common Beginner Mistakes

Mistake 1: Wrong indentation

# Wrong
@app.get("/")
    def read_root():
        return {"message": "Hello"}
Enter fullscreen mode Exit fullscreen mode

Mistake 2: Forgetting to return something

# Wrong
@app.get("/")
def read_root():
    message = {"message": "Hello"}
    # Forgot to return!
Enter fullscreen mode Exit fullscreen mode

Mistake 3: Running without uvicorn

# Don't do this:
python main.py  # This won't work!

# Do this instead:
uvicorn main:app --reload
Enter fullscreen mode Exit fullscreen mode

What's Next?

In the next part of this series, we'll cover:

  • Query parameters
  • Request bodies
  • Data validation with Pydantic
  • Different HTTP methods (POST, PUT, DELETE)

Summary

You've learned:

  • How to install FastAPI and uvicorn
  • How to create a basic FastAPI application
  • How to define endpoints with path parameters
  • How to run your application
  • How to access automatic documentation

Keep your server running and experiment with adding more endpoints. Try different paths and see what happens!


Got questions? Drop them in the comments below. In Part 2, we'll dive into handling different types of data from users.

Top comments (0)