Authentication
TenancyEngine's HTTP API accepts exactly two credential types. Every protected route uses one of them, never both, never something else.
| Mechanism | Header | Bound to | Typical caller |
|---|---|---|---|
| Organization API key | X-TenancyEngine-Api-Key: te_... |
A single organization | Servers, CI/CD, background jobs, MCP tools, your SDK |
| OIDC bearer token | Authorization: Bearer <access_token> |
A signed-in user and their org/tenant context | TE Console, browser SPAs, mobile apps |
Missing or invalid credentials return 401 Unauthorized. Valid credentials without sufficient scope return 403 Forbidden.
Organization API key
| Header | X-TenancyEngine-Api-Key (or Authorization: Bearer te_...; both work) |
|---|---|
| Route prefix | /api/v1/runtime/* |
| Created in | TE Console → your account → API keys |
Common scopes (set at key creation, comma-separated):
| Scope | Grants |
|---|---|
applications.read | Application lookup, runtime context |
applications.write | Provision tenants under an application |
tenants.read | Tenant lookup |
tenants.write | Create tenants, adjust usage |
entitlements.read | Add-on / entitlement status |
analytics.write | Send analytics events |
sms.send | Send SMS via the platform gateway (never bundled into any composite scope, a deliberate money-spending guard) |
mcp.full | Composite scope minted by the MCP device-code flow |
A key only ever authorizes calls about your own organization's applications and
tenants: OrganizationApiKeyMiddleware binds the request to your organization before
any route logic runs, and each endpoint separately checks the specific scope it needs.
OIDC bearer token
| Header | Authorization: Bearer <access_token> |
|---|---|
| Issuer | https://auth.saasruntime.com (discovery: /.well-known/openid-configuration) |
| Route prefix | /api/v1/console/*, /api/v1/admin/* |
Use a bearer token when a human is signed in and authorization should reflect that person's roles and organization/tenant membership: console UIs, staff tooling, or your own product's backend acting on behalf of a logged-in user.
Quick decision matrix
| Scenario | Use |
|---|---|
| Nightly job provisions tenants from your CRM | Organization API key (tenants.write) |
| Your backend gates a request on a tenant's plan features | Organization API key (applications.read) |
| Your own console page saves a setting for the signed-in user | OIDC bearer (their session) |
| MCP tool call over HTTP | Organization API key (usually mcp.full, via device flow) |
Example: get your application
curl -sS "https://auth.saasruntime.com/api/v1/runtime/applications/your-app-slug" \
-H "X-TenancyEngine-Api-Key: te_your_key_here"
Error handling
| Status | Meaning | Typical fix |
|---|---|---|
| 401 | Missing, invalid, expired, or revoked credential | Rotate the key; check expiresAtUtc; refresh the OIDC token |
| 403 | Valid credential, insufficient scope | Add the scope at key creation, or use a bearer token with the right role |
| 404 | Resource doesn't exist or belongs to a different organization | Both cases 404, deliberately: TenancyEngine never confirms another org's data exists |
| 429 | Rate limited | Back off and respect Retry-After |
Security checklist
- Store API keys in a secret manager or CI env, never in source control.
- Scope keys narrowly (
tenants.read, not a full-access key) for automation. - Set an expiry on CI/automation keys; revoke on offboarding.
- Browser and mobile apps use OIDC PKCE. Never embed an organization API key in shipped client code.
- Rotate a key immediately if you suspect it leaked.
See also: Getting started, API reference, SDKs.