Coding
TEXT
Python FastAPI Backend Generator
April 1, 2026
Optimized for:
general
Rapid scaffolding of production-quality FastAPI backend modules
You are a senior backend engineer specializing in Python FastAPI applications. Generate a complete, production-ready API module based on the requirements below.
**Module Requirements:**
- Resource name: [RESOURCE_NAME, e.g., 'users', 'products', 'orders']
- Database: [PostgreSQL/MongoDB/SQLite]
- ORM: [SQLAlchemy/Tortoise-ORM/Motor]
- Authentication: [JWT/OAuth2/API Key/None]
- Description: [WHAT_THIS_MODULE_DOES]
**Generate the following files:**
1. **models.py** - SQLAlchemy/Pydantic models:
- Database model with proper column types, indexes, and constraints
- Created_at, updated_at timestamps with timezone awareness
- Soft delete support (is_deleted flag)
- Relationship definitions
2. **schemas.py** - Pydantic v2 schemas:
- CreateSchema (input validation for POST)
- UpdateSchema (partial updates for PATCH, all fields Optional)
- ResponseSchema (API output, excludes sensitive fields)
- ListResponseSchema (paginated list with metadata)
- Use Field() validators with examples in json_schema_extra
3. **router.py** - FastAPI router:
- GET / - List with pagination, sorting, filtering
- GET /{id} - Single resource with 404 handling
- POST / - Create with 201 response and Location header
- PATCH /{id} - Partial update
- DELETE /{id} - Soft delete with 204 response
- Include proper response_model, status_code, summary, description
- Dependency injection for auth, database session
4. **service.py** - Business logic layer:
- CRUD operations separated from route handlers
- Transaction management
- Custom exception classes
- Event hooks (pre_create, post_create, etc.)
5. **tests/test_[resource].py** - Pytest tests:
- Test each endpoint with valid and invalid data
- Test authentication and authorization
- Test edge cases (duplicate, not found, validation errors)
- Use pytest fixtures and httpx AsyncClient
All code must include type hints, docstrings, and follow PEP 8.
Generates a complete FastAPI module with models, schemas, routes, service layer, and tests following production best practices.
Submit your own AI prompts to the community. The best ones get featured on TokenCalculator - and credited to you.