DEV Community

Hyunjung
Hyunjung

Posted on

How to start FastAPI?

I’m starting a new FastAPI project for my capstone, and I want to document the entire process so I can look back on it later.


Prerequisites

  • Python 3.10+ installed
  • Git installed and configured
  • A GitHub account
  • Basic terminal knowledge

1. Create your project folder

mkdir my-project
cd my-project
Enter fullscreen mode Exit fullscreen mode

2. Set up a virtual environment

Always use a virtual environment.

python -m venv venv
Enter fullscreen mode Exit fullscreen mode

Activate it:

# macOS / Linux
source venv/bin/activate

# Windows (CMD)
venv\Scripts\activate.bat
Enter fullscreen mode Exit fullscreen mode

Your terminal prompt should now show (venv) at the start.

3. Install FastAPI and Uvicorn

pip install fastapi uvicorn
Enter fullscreen mode Exit fullscreen mode

uvicorn is the ASGI server that runs your FastAPI app.

4. Freeze your dependencies

pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

This creates a requirements.txt file so anyone can reproduce your environment with:

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

5. Create your first app

Create a file called main.py at the root of your project:

from fastapi import FastAPI

app = FastAPI()

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

6. Run the dev server

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

Now open your browser and visit:

  • http://127.0.0.1:8000 — your root endpoint
  • http://127.0.0.1:8000/docs — auto-generated Swagger UI

FastAPI auto-generates interactive API docs from your code.


7. Structure your project (optional but recommended)

For a capstone or real project, a flat main.py will get messy fast. Here's a minimal structure that scales well:

my-fastapi-project/
├── app/
│   ├── __init__.py
│   ├── main.py
│   └── models/
│       ├── __init__.py
├── requirements.txt
├── .gitignore
└── README.md
Enter fullscreen mode Exit fullscreen mode

Update your entry point to point to app.main:

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

8. Add a .gitignore

Before committing anything, create a .gitignore so you don't accidentally push your virtual environment or secrets:
you can get text from gitignore.io


9. Write a README.md

just as you want~


10. Initialize Git and push to GitHub

  • Create a repo on GitHub
  • Connect and push

This is really easy and you can do it.


Done! Your FastAPI project is now on GitHub.

Top comments (0)