Overview
SnackBase uses FastAPI for REST API endpoints. All endpoints are organized in thesrc/snackbase/infrastructure/api/routes/ directory.
Existing API Routers
Architecture Review
Layer Structure
Clean Architecture Principles
- Routers handle HTTP concerns (status codes, headers, parsing)
- Services contain business logic
- Repositories abstract database access
- Models (Pydantic) define request/response schemas
Where to Add Endpoints
Decision Tree
When to Create a New Router
Create a new router when:- Adding a new major feature area
- Existing routers don’t match the domain concept
- The feature has 3+ related endpoints
/api/v1/webhooks/*- Webhook management/api/v1/scheduled-tasks/*- Task scheduling/api/v1/integrations/*- Third-party integrations
Step-by-Step Guide
Let’s add a new feature: Tags for organizing records.Step 1: Define Pydantic Schemas
Create request/response models ininfrastructure/api/schemas/:
Step 2: Create Database Model
Add SQLAlchemy model ininfrastructure/persistence/models/:
Step 3: Create Repository
Add repository ininfrastructure/persistence/repositories/:
Step 4: Create API Router
Create router ininfrastructure/api/routes/:
Step 5: Register Router
Add the router toapp.py:
Step 6: Create Migration
Generate and apply database migration:Step 7: Add Permissions (Optional)
If your feature needs authorization, add permissions:Request/Response Patterns
Request Body Validation
Use Pydantic for automatic validation:Response Formatting
Use consistent response formats:Pagination
For list endpoints, support pagination:Authentication & Authorization
Require Authentication
All endpoints automatically require authentication viaget_context():
Require Permissions
Userequire_permission() for authorization:
Superadmin-Only Endpoints
For superadmin-only endpoints:Testing Endpoints
Unit Tests
Test router logic:Integration Tests
Test full flow with database:Manual Testing with Swagger UI
Visithttp://localhost:8000/docs to test endpoints interactively.
Best Practices
1. Use Appropriate Status Codes
2. Account Isolation
Always filter byaccount_id in repositories:
3. Use Dependency Injection
Inject dependencies via FastAPI’sDepends():