Skip to content

javienc/fastapi-starter

Repository files navigation

FastAPI Demo Project

A demonstration project showcasing FastAPI features with authentication, CRUD operations, and interactive documentation.

🚀 Features

  • 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

📋 Requirements

  • Python 3.9+2
  • FastAPI
  • Uvicorn
  • Python-multipart (for form data processing)
  • Jinja2 (for HTML templates)

🛠 Installation

  1. Clone the repository:
bash
git clone https://github.com/yourusername/fastapi-demo.git
cd fastapi-demo
  1. Create and activate a virtual environment:
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
  1. Install dependencies (choose one option):

    Option 1: Install from requirements.txt

    pip install -r requirements.txt

    Option 2: Install packages directly

    pip install fastapi uvicorn python-multipart jinja2 python-dotenv fastapi-cli

🏃‍♂️ Running the Application

You can start the server in several ways:

  1. Using FastAPI CLI (recommended):

    fastapi dev main.py

    or start with host localhost and port 8000

    fastapi dev main.py --host localhost --port 8000 --proxy-headers
  2. Using uvicorn directly:

    uvicorn main:app --reload

    or start with host localhost and port 8000

    uvicorn main:app --reload --port 8000 --host localhost
  3. 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

📚 API Documentation

Visit these URLs after starting the application:

🔑 Authentication

  • Login endpoint: /token
  • Tokens expire after 24 hours
  • Token format: user_username_timestamp
  • Manual token revocation available

Test Credentials

Username: testuser
Password: testpass

🛣 Available Routes

Public Endpoints (No Authentication Required)

  • GET /api - API status check
  • GET /public/items - List public items
  • GET /public/items/{item_id} - Get specific public item

Protected Endpoints (Authentication Required)

  • POST /token - Get authentication token
  • POST /token/revoke - Revoke current token
  • GET /items - List all items
  • GET /items/{item_id} - Get specific item
  • POST /items - Create new item
  • PUT /items/{item_id} - Update entire item
  • PATCH /items/{item_id} - Partially update item
  • DELETE /items/{item_id} - Delete item

📝 Example Usage

Get Authentication Token

curl -X POST http://127.0.0.1:8000/token \
    -H "Content-Type: application/x-www-form-urlencoded" \
    -d "username=testuser&password=testpass"

Create New Item

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
    }'

🧪 Testing

The API includes mock databases for testing:

  • public_items_db: Publicly accessible items
  • items_db: Protected items requiring authentication
  • users_db: User credentials for authentication

📦 Project Structure

fastapi-demo/
├── main.py              # Main application file
├── README.md            # Project documentation
└── templates/
    └── fastapi_tutorial.html  # Tutorial page template

🚀 Deployment on Vercel

  1. Fork/clone this repository

  2. Local Setup:

    • Copy .env.example to .env for development:
      cp .env.example .env
    • The default development settings should work out of the box
  3. 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)
  4. 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

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a Pull Request

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

✨ Acknowledgments