← Back to Home / Coding Prompts

Python FastAPI Backend Builder

Generate production-ready FastAPI applications.

Act as a senior backend engineer specializing in Python FastAPI for high-performance microservices handling thousands of requests per second with sub-millisecond response times. Generate a complete FastAPI application scaffold for a specific use case including authentication, database integration, request validation, error handling, background tasks, and API documentation. Create project structure following best practices including separation of concerns with routers, models, schemas, services, and repositories layers, configuration management using Pydantic settings with environment variable loading, dependency injection for reusable components, and modular package organization. Implement authentication and authorization including JWT token generation and validation, OAuth2 password flow integration, role-based access control decorators, API key authentication for service accounts, and refresh token rotation for security. Develop database integration using SQLAlchemy 2.0 with async support, Alembic migration management with autogenerate and custom migrations, repository pattern for data access abstraction, connection pooling optimization, and query optimization using selectin loading and joined loading. Create request validation using Pydantic models with custom validators, field constraints, and nested model support, query parameter validation for filtering and pagination, path parameter validation with regex patterns, and body validation with custom error messages. Implement error handling including global exception handlers with appropriate HTTP status codes, custom exception classes for domain-specific errors, validation error formatting for user-friendly responses, and logging integration for debugging. Add background tasks using Celery with Redis or RabbitMQ broker, task status endpoints, retry logic with exponential backoff, task chaining and grouping, and result backend configuration. Generate OpenAPI documentation customization including operation descriptions, response examples, deprecated endpoint marking, and tag organization. Include production considerations like CORS configuration, middleware for request logging and timing, gzip compression, rate limiting implementation, health check endpoints, and Prometheus metrics integration. The final code should be production-ready with type hints throughout, comprehensive docstrings, and pytest examples for critical endpoints.