API Structure
Overview
The Rhesis backend API is built with FastAPI and follows RESTful principles. The API endpoints are organized into routers, each handling a specific resource type.
Router Organization
All API routers are defined in the app/routers/
directory. The application includes over 30 routers for different entities, including:
- Authentication
- Users and Organizations
- Tests and Test Sets
- Prompts and Templates
- Models and Endpoints
- Categories and Tags
Route Categories
Routes are categorized based on authentication requirements:
Public Routes
These routes don’t require any authentication:
Token-Enabled Routes
These routes accept both session and token authentication:
Session-Only Routes
All other routes require session-based authentication.
Custom Route Class
The application uses a custom AuthenticatedAPIRoute
class that automatically adds the appropriate authentication dependencies based on the route path:
Standard Endpoints
Most resource routers follow a standard pattern with these endpoints:
Read Operations
GET /{resource}/
: List all resources (with filtering and pagination)GET /{resource}/{id}
: Get a specific resource by IDGET /{resource}/count
: Get the count of resources
Write Operations
POST /{resource}/
: Create a new resourcePUT /{resource}/{id}
: Update a resourceDELETE /{resource}/{id}
: Delete a resource
Example Router
Here’s a simplified example of a router structure:
Query Parameters
The API supports various query parameters for filtering, sorting, and pagination:
Filtering
OData-style filtering is supported:
GET /tests/?$filter=prompt_id eq '89905869-e8e9-4b2f-b362-3598cfe91968'
Sorting
GET /tests/?$orderby=created_at desc
Pagination
GET /tests/?$skip=10&$top=10
Response Headers
The API includes helpful response headers:
X-Total-Count
: Total number of items for paginated responses- Standard CORS headers for cross-origin requests
API Documentation
The API documentation is automatically generated using FastAPI’s built-in support for OpenAPI:
- Swagger UI: Available at
/docs
- ReDoc: Available at
/redoc
- OpenAPI JSON: Available at
/openapi.json
Error Handling
The API uses standard HTTP status codes for error responses:
- 400: Bad Request (invalid input)
- 401: Unauthorized (authentication required)
- 403: Forbidden (insufficient permissions)
- 404: Not Found (resource doesn’t exist)
- 500: Internal Server Error (server-side error)
Error responses include a JSON body with details about the error.