Cloud
Multi-Tenancy
Tenant isolation middleware with header, subdomain, JWT, and custom resolution strategies.
Overview
The multi-tenancy middleware resolves, validates, and attaches the current tenant to every incoming request. It supports four resolution strategies and automatically rejects suspended or archived tenants.
Basic Usage
typescript
Resolution Strategies
| Strategy | Source | Example |
|---|---|---|
header | Configurable request header | x-tenant-id: acme-corp |
subdomain | First segment of the Host header | acme.app.io → acme |
jwt | req.locals.user.tenantId from auth | Populated by @axiljs/auth |
custom | User-supplied async resolver function | Any logic you define |
Header Strategy (Default)
typescript
Subdomain Strategy
typescript
JWT Strategy
typescript
Custom Strategy
typescript
Tenant Validation
The middleware validates tenant ID format and lifecycle status automatically.
typescript
- Malformed IDs →
401 Unauthorized - Suspended tenants →
401 Unauthorized - Archived tenants →
401 Unauthorized - Unknown tenants →
401 Unauthorized(whenrequired: true)
Helper Functions
typescript
Options Reference
| Option | Type | Default | Description |
|---|---|---|---|
strategy | 'header' | 'subdomain' | 'jwt' | 'custom' | 'header' | Resolution method |
headerName | string | 'x-tenant-id' | Header name for header strategy |
resolveTenant | (id: string) => Promise<Tenant | null> | Identity lookup | Database/cache lookup function |
validateTenantId | (id: string) => boolean | RFC 1123 regex | Custom format validation |
required | boolean | true | Reject requests without a tenant |
customResolver | (req) => string | null | — | Required when strategy is custom |