Skip to main content

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_orm package 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:

DomainBase PathDescription
Geographic/api/v1/geographicCountries, admin divisions, locations
Climate/api/v1/climateHistorical data, climatology, indicators
Forecast/api/v1/forecastForecast runs and results
Agronomic/api/v1/agronomicCrops, cultivars, soils, setups
Historical/api/v1/historicalHistorical analysis data
Recommendation/api/v1/recommendationAgronomic recommendations
Admin/api/v1/adminUsers, roles, configuration

Geographic Endpoints​

EndpointMethodDescription
/api/v1/geographic/countriesGETList all countries
/api/v1/geographic/countries/&#123;id&#125;GETGet country by ID
/api/v1/geographic/countries/&#123;id&#125;/admin1GETGet admin level 1 divisions
/api/v1/geographic/admin1/&#123;id&#125;/admin2GETGet admin level 2 divisions

Climate Endpoints​

EndpointMethodDescription
/api/v1/climate/historical/dailyGETDaily climate data (paginated)
/api/v1/climate/historical/monthlyGETMonthly aggregated data
/api/v1/climate/historical/climatologyGETClimatological normals
/api/v1/climate/historical/indicatorsGETDerived 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​

ParameterTypeDescription
pageintegerPage number (default: 1)
page_sizeintegerItems per page (default: 20, max: 100)
sort_bystringField to sort by
sort_orderstringasc or desc
searchstringText 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: Success
  • 201: Created
  • 400: Bad Request (validation error)
  • 401: Unauthorized (missing/invalid token)
  • 403: Forbidden (insufficient permissions)
  • 404: Not Found
  • 422: Unprocessable Entity
  • 500: 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:

  1. ORM Model → Database entity
  2. ORM Schema → Request/Response contract
  3. ORM Service → Business logic execution
  4. Validation → Input data integrity check

This ensures consistency between the data layer and the API contract.