Choorai

60분 완주 챌린지

Step 1/6

0%
Estimated time: 15 min
FastAPI

Backend: FastAPI

Build an API server with Python FastAPI. We'll start with a simple in-memory storage approach without a database.

TL;DR (Summary)

  • Create a Python virtual environment and install FastAPI
  • Create project CRUD API endpoints
  • Test the API at /docs

Terms Before Step 3

  • Endpoint: A URL where your API receives requests.
  • Request: Data/action sent from frontend to backend.
  • Response: Result data returned by backend.
  • CRUD: Create, Read, Update, Delete basic operations.

1Environment Setup

Create a backend folder next to the frontend folder (my-admin).

Terminal
# Navigate to the parent folder (where my-admin is located)
cd ..

# Create backend folder
mkdir my-admin-api
cd my-admin-api

# Create Python virtual environment (recommended)
python3 -m venv venv

# Activate virtual environment
# Mac/Linux:
source venv/bin/activate
# Windows:
# venv\Scripts\activate

Don't have Python?

Install Python 3.10 or later from python.org. Mac users can also use brew install python.

2Install FastAPI

Terminal
# Install FastAPI and server
pip install fastapi uvicorn

# Generate dependencies file
pip freeze > requirements.txt

A requirements.txt file will be created. You'll use this file later for deployment.

Term Tip: Virtual Env / Dependencies

  • Virtual Environment: A project-isolated Python package environment that prevents conflicts.
  • requirements.txt: The package list your Python app needs.
  • Runtime: The environment where your code actually runs.
  • Deep dive: Tools Setup, Environment and Runtime

3Create Your First API

Create a main.py file and paste the following code:

main.py
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI(title="My Admin API")

# CORS setup - required for communicating with the frontend!
app.add_middleware(
    CORSMiddleware,
    allow_origins=["http://localhost:5173"],  # Vite dev server
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.get("/health")
def health_check():
    return {"status": "ok", "version": "1.0.0"}

@app.get("/")
def root():
    return {"message": "Welcome to My Admin API"}

Let's run the server:

Terminal
uvicorn main:app --reload --port 8000

Open http://localhost:8000/docs in your browser and you'll see the Swagger UI!

Term Tip: CORS / Swagger

  • CORS: Browser security rules for cross-origin requests (different domain/port).
  • Swagger UI: An interactive API docs/testing interface in your browser.
  • Deep dive: CORS troubleshooting
Success!

If you see the Swagger documentation, the API server is running correctly.

4Create CRUD API with AI

Ask AI to create the project CRUD API for you.

AI Prompt

Copy and use

"Create a project CRUD API with FastAPI. Requirements: 1. Store data in memory (dictionary) without a database 2. Use Pydantic models for type validation API Endpoints: - GET /api/v1/projects - Project list (pagination) - POST /api/v1/projects - Create project - GET /api/v1/projects/{id} - Project details - PUT /api/v1/projects/{id} - Update project - DELETE /api/v1/projects/{id} - Delete project Project Fields: - id: UUID (auto-generated) - name: string (required) - description: string (optional) - created_at: datetime (auto) - updated_at: datetime (auto) File structure: - main.py (add router to existing file) - schemas.py (Pydantic models) - storage.py (in-memory storage)"

Send the prompt above to your AI assistant.

5Verify Results

After applying the AI-generated code and restarting the server, you can test all APIs at /docs.

localhost:8000/docs
GET /api/v1/projects Project list
POST /api/v1/projects Create project
GET /api/v1/projects/{id} Project details
PUT /api/v1/projects/{id} Update project
DELETE /api/v1/projects/{id} Delete project

Testing in Swagger

  1. Click on POST /api/v1/projects
  2. Click the "Try it out" button
  3. Enter a name and description
  4. Click the "Execute" button
  5. Verify the 201 response and created project!

The completed example code is available on the GitHub repository .

FastAPI Backend Completion Checklist

0/4 done

Last updated: February 22, 2026 · Version: v0.0.1

Send Feedback

Opens a new issue page with your message.

Open GitHub Issue