API Development
Maybern uses class-based views with strong typing for all API endpoints. This guide covers the patterns and best practices for building APIs.API View Structure
APIs should be thin and delegate complex logic to services:Key Components
API Decorators
The@api_get, @api_post, @api_put, @api_patch, and @api_delete decorators handle:
- Request validation
- Response serialization
- OpenAPI schema generation
- Error handling
Operation IDs
Every endpoint must have a unique operation ID defined inOperationIds:
- OpenAPI schema generation
- Frontend client generation
- API documentation
- Logging and monitoring
Request Context
TheRequestCtx object contains request-scoped information:
ctx parameter.
URL Parameters
URL path parameters are passed viaURLParams:
API Helper Functions
Extract complex response building logic into helper functions:Request/Response Dataclasses
Use typed dataclasses for all request and response structures:Testing APIs
API tests should mock the service layer:Best Practices
Keep APIs Thin
Keep APIs Thin
APIs should only handle:
- Request parsing
- Response formatting
- Delegating to services
Use Strong Typing
Use Strong Typing
Always define typed dataclasses for:
- Request bodies
- Query parameters
- Response data
- Type checking
- OpenAPI schema generation
- Frontend client generation
Document Everything
Document Everything
Every endpoint needs:
- Operation ID
- Description
- Parameter documentation
- Response documentation
Consistent Naming
Consistent Naming
Follow these conventions:
GET /entities/→listEntitiesGET /entities/{id}/→getEntityPOST /entities/→createEntityPUT /entities/{id}/→updateEntityDELETE /entities/{id}/→deleteEntity