Authentication mechanism
All requests to the Address API (except/ and /healthz) require an API key passed in the x-commenda-key header.
Roles
API keys are assigned specific roles that determine which endpoints they can access:| Role | Description | Endpoints |
|---|---|---|
GEOCODE | Access to geocoding functionality | /api/v1/geoencode |
MANAGE_CONTENT | Access to content ingestion | /api/v1/internal/iso_3166/ingest/json |
How authorization works
- Request received: The API receives a request with the
x-commenda-keyheader - Key lookup: The middleware queries the
api_keytable to find the key - Key validation: Checks if the key exists and is valid
- Role check: Verifies the key has the required role for the endpoint
- Request processing: If authorized, the request proceeds to the handler
- Error response: If unauthorized, returns 401 or 403 error
Database schema
API keys are stored in theapi_key table:
Getting an API key
To obtain an API key with the necessary roles:- Contact your Commenda administrator or developer
- Request the specific role(s) you need (
GEOCODEorMANAGE_CONTENT) - The administrator will generate an API key with the appropriate permissions
- Store the API key securely (use environment variables or secret management)
- Include it in the
x-commenda-keyheader for all requests
Error responses
Missing API key (401)
Invalid API key (401)
Missing role (403)
If your API key doesn’t have the required role for an endpoint:Exempt endpoints
The following endpoints do NOT require authentication:GET /- Root endpoint (returns service status message)GET /healthz- Health check endpoint (returns database connection status)
Security best practices
- Never commit API keys: Store them in environment variables or secret management systems
- Rotate keys regularly: Generate new keys periodically and revoke old ones
- Use least privilege: Only assign the roles that are actually needed
- Monitor usage: Track API key usage to detect anomalies
- Secure transmission: Always use HTTPS to prevent key interception