ASAgriSense/Docs/Routes & Auth

API Reference

Routes & Auth

Base URL#

EnvironmentURL
Local dev (Wrangler)http://localhost:8788
ProductionSet by NEXT_PUBLIC_API_URL env var

Authentication#

Every request to the API must include a Clerk session token in the Authorization header:

http
Authorization: Bearer <clerk-session-token>

A global Hono middleware verifies the JWT using jose and extracts auth context:

typescript
// Accessible in every route handler
const { userId, orgId, role, permissions } = c.get('auth');

All database queries are automatically scoped to orgId. A user from Org A cannot read or modify data belonging to Org B regardless of the URL they request.

Local dev bypass#

Set AUTH_DEV_BYPASS=true in the API .dev.vars file to bypass Clerk JWT verification. Use DEV_AUTH_ROLE and DEV_AUTH_TENANT_ID to simulate a specific user context.

Error response format#

json
{
  "error": {
    "code": "VALIDATION_ERROR",
    "message": "Request body failed validation",
    "details": {
      "fieldErrors": {
        "name": ["String must contain at least 1 character(s)"]
      }
    }
  }
}
HTTP statusWhen
400 Bad RequestMalformed request (e.g. invalid JSON).
401 UnauthorizedMissing or invalid Authorization header.
403 ForbiddenValid token but insufficient permissions for this operation.
404 Not FoundResource does not exist within the authenticated org.
422 Unprocessable EntityZod schema validation failed. Includes field-level errors.
500 Internal Server ErrorUnexpected server error.

Zod validation#

Every route that accepts a request body uses @hono/zod-validator. A request body that fails schema validation returns a 422 with field-level error details before the route handler is ever called.

Route groups#

All routes are under /api/ prefix. Routes are grouped by module and mounted in apps/api/src/index.ts.

Geographic Setup#

PathMethodsNotes
/api/orgsGET, PATCHOrg config read and update.
/api/regionsGET, POST, PATCH, DELETE
/api/districtsGET, POST, PATCH, DELETE
/api/communitiesGET, POST, PATCH, DELETE
/api/nodesGET, POST, PATCHGeneric hierarchy node operations.
/api/hierarchyGETFull hierarchy tree read.
/api/hierarchy-levelsGET, POST, PATCHConfigurable label names per level.
/api/buying-stationsGET, POST, PATCH, DELETE
/api/importPOSTBulk CSV import.

Farmer Registry#

PathMethodsNotes
/api/farmersGET, POST, PATCHIncludes search, pagination, status transitions.
/api/farmers/:idGET, PATCH
/api/farmers/:id/auditGETFarmer audit trail.
/api/farmers/bulk-importPOSTCSV farmer bulk import.

Farm Mapping#

PathMethodsNotes
/api/farmsGET, POST, PATCH, DELETE
/api/plotsGET, POST, PATCH, DELETEIncludes GeoJSON geometry and export.

Deforestation Risk#

PathMethodsNotes
/api/assessmentsGET, POSTRequest and read assessments.
/api/remediationsGET, POST, PATCH
/api/risk-overridesGET, POSTApply and view overrides.
/api/risk-summaryGETAggregate risk stats by geography.
/api/notificationsGET, PATCHRisk alert notifications.

Sustainability Programs#

PathMethodsNotes
/api/program-typesGET, POST, PATCH
/api/programsGET, POST, PATCH, DELETE
/api/activitiesGET, POST, PATCH, DELETE
/api/attendanceGET, POSTBulk attendance recording.
/api/input-distributionsGET, POST
/api/coverageGETProgram coverage aggregates.
/api/evidencePOSTGenerate presigned R2 upload URL.

Surveys#

PathMethodsNotes
/api/surveysGET, POST, PATCH
/api/survey-versionsGET, POST, PATCHDraft, publish, archive.
/api/survey-questionsGET, POST, PATCH, DELETE
/api/survey-assignmentsGET, POST, PATCH
/api/survey-responsesGET, POSTSubmit and retrieve responses.
/api/survey-analyticsGETResponse aggregate stats.
/api/survey-exportGETCSV export of responses.
/api/survey-photosPOSTPresigned R2 URL for photo upload.

EUDR Compliance#

PathMethodsNotes
/api/complianceGET, POSTRead records and trigger recompute.
/api/audit-logGETRead audit entries by org, farmer, event.

Dashboard#

PathMethodsNotes
/api/dashboardGETSummary response scoped to org + optional geographic filters.
Source of truth
The authoritative route list is in apps/api/src/index.ts. This page reflects the intended route structure — always verify against the actual source if there is any discrepancy.