A demonstration project showcasing FastAPI features with authentication, CRUD operations, and interactive documentation.
- RESTful API endpoints with FastAPI
- Token-based authentication
- Public and protected routes
- CRUD operations for items
- Interactive API documentation
- Mock database implementation
- Comprehensive tutorial page
- Python 3.9+2
- FastAPI
- Uvicorn
- Python-multipart (for form data processing)
- Jinja2 (for HTML templates)
- Clone the repository:
bash
git clone https://github.com/yourusername/fastapi-demo.git
cd fastapi-demo
- Create and activate a virtual environment:
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
-
Install dependencies (choose one option):
pip install -r requirements.txt
pip install fastapi uvicorn python-multipart jinja2 python-dotenv fastapi-cli
You can start the server in several ways:
-
Using FastAPI CLI (recommended):
fastapi dev main.py
fastapi dev main.py --host localhost --port 8000 --proxy-headers
-
Using uvicorn directly:
uvicorn main:app --reload
uvicorn main:app --reload --port 8000 --host localhost
-
Using python -m:
python -m uvicorn main:app --reload
The application will be available at:
- Development:
http://127.0.0.1:8000
- Production: Check your environment's API_URL setting
Visit these URLs after starting the application:
- Tutorial Page: http://127.0.0.1:8000/
- Swagger UI: http://127.0.0.1:8000/docs
- ReDoc: http://127.0.0.1:8000/redoc
- OpenAPI Schema: http://127.0.0.1:8000/openapi.json
- Login endpoint:
/token
- Tokens expire after 24 hours
- Token format:
user_username_timestamp
- Manual token revocation available
Username: testuser
Password: testpass
GET /api
- API status checkGET /public/items
- List public itemsGET /public/items/{item_id}
- Get specific public item
POST /token
- Get authentication tokenPOST /token/revoke
- Revoke current tokenGET /items
- List all itemsGET /items/{item_id}
- Get specific itemPOST /items
- Create new itemPUT /items/{item_id}
- Update entire itemPATCH /items/{item_id}
- Partially update itemDELETE /items/{item_id}
- Delete item
curl -X POST http://127.0.0.1:8000/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=testuser&password=testpass"
curl -X POST http://127.0.0.1:8000/items \
-H "Authorization: Bearer your_token_here" \
-H "Content-Type: application/json" \
-d '{
"name": "New Item",
"description": "Description",
"price": 29.99,
"tags": ["new", "item"],
"is_offer": false
}'
The API includes mock databases for testing:
public_items_db
: Publicly accessible itemsitems_db
: Protected items requiring authenticationusers_db
: User credentials for authentication
fastapi-demo/
├── main.py # Main application file
├── README.md # Project documentation
└── templates/
└── fastapi_tutorial.html # Tutorial page template
-
Fork/clone this repository
-
Local Setup:
- Copy
.env.example
to.env
for development:cp .env.example .env
- The default development settings should work out of the box
- Copy
-
Vercel Setup:
- Create a new project on Vercel
- Connect your repository
- Copy environment variables from
.env.production.example
- Configure these variables in Vercel dashboard (Project Settings > Environment Variables):
APP_ENV
: Set to "production"API_URL
: Set to your Vercel deployment URL (you'll get this after first deploy)
-
Deploy the project
- After first deployment, copy your Vercel URL
- Update the
API_URL
in Vercel environment variables with your deployment URL - Trigger a new deployment to apply the URL change
Note: The example environment files are provided for reference:
.env.example
- Template for local development.env.production.example
- Template for Vercel deployment
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.