All skills
wshobson avatar

/fastapi-templates

@be57c0b
by Seth Hobsonwshobson/agents40k stars
4,281

Create production-ready FastAPI projects with async patterns, dependency injection, and comprehensive error handling. Use when building new FastAPI applications or setting up backend API projects.

Use this Skill: https://skilld.dev/gh/wshobson/agents/fastapi-templates

This session only. Nothing lands on disk.

SKILL.md

β‰ˆ54 tokens always: the name and description. β‰ˆ890 when used: this file. β‰ˆ2.9k more on demand in 1 file.

FastAPI Project Templates

Production-ready FastAPI project structures with async patterns, dependency injection, middleware, and best practices for building high-performance APIs.

When to Use This Skill

  • Starting new FastAPI projects from scratch
  • Implementing async REST APIs with Python
  • Building high-performance web services and microservices
  • Creating async applications with PostgreSQL, MongoDB
  • Setting up API projects with proper structure and testing

Core Concepts

1. Project Structure

Recommended Layout:

app/
β”œβ”€β”€ api/                    # API routes
β”‚   β”œβ”€β”€ v1/
β”‚   β”‚   β”œβ”€β”€ endpoints/
β”‚   β”‚   β”‚   β”œβ”€β”€ users.py
β”‚   β”‚   β”‚   β”œβ”€β”€ auth.py
β”‚   β”‚   β”‚   └── items.py
β”‚   β”‚   └── router.py
β”‚   └── dependencies.py     # Shared dependencies
β”œβ”€β”€ core/                   # Core configuration
β”‚   β”œβ”€β”€ config.py
β”‚   β”œβ”€β”€ security.py
β”‚   └── database.py
β”œβ”€β”€ models/                 # Database models
β”‚   β”œβ”€β”€ user.py
β”‚   └── item.py
β”œβ”€β”€ schemas/                # Pydantic schemas
β”‚   β”œβ”€β”€ user.py
β”‚   └── item.py
β”œβ”€β”€ services/               # Business logic
β”‚   β”œβ”€β”€ user_service.py
β”‚   └── auth_service.py
β”œβ”€β”€ repositories/           # Data access
β”‚   β”œβ”€β”€ user_repository.py
β”‚   └── item_repository.py
└── main.py                 # Application entry

2. Dependency Injection

FastAPI's built-in DI system using Depends:

  • Database session management
  • Authentication/authorization
  • Shared business logic
  • Configuration injection

3. Async Patterns

Proper async/await usage:

  • Async route handlers
  • Async database operations
  • Async background tasks
  • Async middleware

Detailed worked examples and patterns

Detailed sections (starting with ## Implementation Patterns) live in references/details.md. Read that file when the navigation summary above is insufficient.

Testing

# tests/conftest.py
import pytest
import asyncio
from httpx import AsyncClient
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker

from app.main import app
from app.core.database import get_db, Base

TEST_DATABASE_URL = "sqlite+aiosqlite:///:memory:"

@pytest.fixture(scope="session")
def event_loop():
    loop = asyncio.get_event_loop_policy().new_event_loop()
    yield loop
    loop.close()

@pytest.fixture
async def db_session():
    engine = create_async_engine(TEST_DATABASE_URL, echo=True)
    async with engine.begin() as conn:
        await conn.run_sync(Base.metadata.create_all)

    AsyncSessionLocal = sessionmaker(
        engine, class_=AsyncSession, expire_on_commit=False
    )

    async with AsyncSessionLocal() as session:
        yield session

@pytest.fixture
async def client(db_session):
    async def override_get_db():
        yield db_session

    app.dependency_overrides[get_db] = override_get_db

    async with AsyncClient(app=app, base_url="http://test") as client:
        yield client

# tests/test_users.py
import pytest

@pytest.mark.asyncio
async def test_create_user(client):
    response = await client.post(
        "/api/v1/users/",
        json={
            "email": "test@example.com",
            "password": "testpass123",
            "name": "Test User"
        }
    )
    assert response.status_code == 201
    data = response.json()
    assert data["email"] == "test@example.com"
    assert "id" in data

Source: SKILL.md on GitHub

No alerts16d5 checks Β· Risk SAFE
  • Gen Agent Trust Hub16d

    This skill provides standard, high-quality boilerplate and architectural patterns for developing FastAPI applications. It follows security best practices such as password hashing and environment-based secret management.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW Β· No issues

  • Runlayer6mo

    1 file scanned Β· No issues

  • ZeroLeaks5mo

    1 finding Β· Score: 82/100

Signed by skilld at be57c0b. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 days ago.

Activeupdated 4 months ago
  • Python
  • API
  • Testing
  • fastapi
  • async
  • dependency-injection
  • project-templates
  • rest-api
  • postgres
  • mongodb

README badge

README badge for wshobson/agents/fastapi-templates

Provides production-ready FastAPI project structures with async patterns, dependency injection, middleware setup, and database integration templates. Use this when bootstrapping new FastAPI backends or microservices that require layered architecture (routes, services, repositories) and async database operations.

Generated from the current SKILL.md.

Does this skill support async database operations?
Yes. The templates include async patterns for database operations, with examples for PostgreSQL and MongoDB using async SQLAlchemy sessions and proper async/await in route handlers.
What testing approach is included?
The skill provides pytest-based testing with AsyncClient, in-memory SQLite for test databases, and dependency override patterns to inject test database sessions.
Does this include authentication and authorization?
The skill structures security logic in core/security.py and auth service patterns, though you will need to implement your specific auth strategy (JWT, OAuth, etc.).
Can I use this for microservices?
Yes. The modular project structure with separated services, repositories, and dependency injection is designed to support building microservices and high-performance web services.

Generated from the current SKILL.md. These answers refresh after source changes.