1Environment Setup
Create a backend folder next to the frontend folder (my-admin).
# 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\activateDon't have Python?
Install Python 3.10 or later from python.org.
Mac users can also use brew install python.
2Install FastAPI
# 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:
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:
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
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.
/api/v1/projects Project list /api/v1/projects Create project /api/v1/projects/{id} Project details /api/v1/projects/{id} Update project /api/v1/projects/{id} Delete project Testing in Swagger
- Click on POST /api/v1/projects
- Click the "Try it out" button
- Enter a name and description
- Click the "Execute" button
- Verify the 201 response and created project!
The completed example code is available on the GitHub repository .