API
This section describes the AClimate v3 WebAPI (aclimate_v3_webapi), the backend service layer that exposes data and business logic through RESTful endpoints.
Overview
The WebAPI is a Python FastAPI application that:
- Exposes RESTful endpoints organized by functional domain
- Implements business logic and data orchestration
- Uses the
aclimate_v3_ormpackage for data access - Provides JWT-based authentication and authorization
- Returns structured JSON responses aligned with ORM schemas
Authentication
The API uses JWT (JSON Web Token) for authentication:
POST /api/v1/auth/login
Content-Type: application/json
{
"username": "user",
"password": "***"
}
Response:
{ "access_token": "...", "token_type": "bearer" }
All subsequent requests include the token in the Authorization header:
Authorization: Bearer <token>
API Domains
Endpoints are organized by domain, mirroring the ORM entity structure:
| Domain | Base Path | Description |
|---|---|---|
| Geographic | /api/v1/geographic | Countries, admin divisions, locations |
| Climate | /api/v1/climate | Historical data, climatology, indicators |
| Forecast | /api/v1/forecast | Forecast runs and results |
| Agronomic | /api/v1/agronomic | Crops, cultivars, soils, setups |
| Historical | /api/v1/historical | Historical analysis data |
| Recommendation | /api/v1/recommendation | Agronomic recommendations |
| Admin | /api/v1/admin | Users, roles, configuration |
Geographic Endpoints
| Endpoint | Method | Description |
|---|---|---|
/api/v1/geographic/countries | GET | List all countries |
/api/v1/geographic/countries/{id} | GET | Get country by ID |
/api/v1/geographic/countries/{id}/admin1 | GET | Get admin level 1 divisions |
/api/v1/geographic/admin1/{id}/admin2 | GET | Get admin level 2 divisions |
Climate Endpoints
| Endpoint | Method | Description |
|---|---|---|
/api/v1/climate/historical/daily | GET | Daily climate data (paginated) |
/api/v1/climate/historical/monthly | GET | Monthly aggregated data |
/api/v1/climate/historical/climatology | GET | Climatological normals |
/api/v1/climate/historical/indicators | GET | Derived climate indicators |
Endpoint Convention
All API endpoints follow RESTful conventions:
- GET: List resources (with pagination, filtering, sorting)
- GET /{id}: Retrieve single resource
- POST: Create resource
- PUT /{id}: Update resource
- DELETE /{id}: Soft-delete (disable) resource
Common Parameters
| Parameter | Type | Description |
|---|---|---|
page | integer | Page number (default: 1) |
page_size | integer | Items per page (default: 20, max: 100) |
sort_by | string | Field to sort by |
sort_order | string | asc or desc |
search | string | Text search filter |
Response Format
{
"data": [],
"pagination": {
"page": 1,
"page_size": 20,
"total_items": 150,
"total_pages": 8
}
}
Error Handling
The API returns consistent error responses:
{
"detail": {
"code": "VALIDATION_ERROR",
"message": "Human-readable error description",
"errors": [
{
"field": "field_name",
"message": "Specific validation error"
}
]
}
}
Standard HTTP status codes:
200: Success201: Created400: Bad Request (validation error)401: Unauthorized (missing/invalid token)403: Forbidden (insufficient permissions)404: Not Found422: Unprocessable Entity500: Internal Server Error
Versioning
The API is versioned via the URL path (/api/v1/). Backward-compatible changes are additive; breaking changes trigger a new version.
ORM to API Mapping
Each API endpoint maps to:
- ORM Model → Database entity
- ORM Schema → Request/Response contract
- ORM Service → Business logic execution
- Validation → Input data integrity check
This ensures consistency between the data layer and the API contract.