Overview
Multi-tenancy provides:- Tenant isolation - Each tenant has isolated storage and keys
- Dynamic tenant management - Create and delete tenants at runtime
- Resource efficiency - Share infrastructure across tenants
- Module inheritance - Tenants inherit modules from root agent
- Tenant routing - Route incoming messages to correct tenant
Installation
Dependencies
The Tenants module requires:@credo-ts/core- Core functionality@credo-ts/didcomm- For DIDComm-based routing (automatically installed)- A storage module that supports multi-tenancy (e.g.,
@credo-ts/askar)
Registration
Configuration Options
Creating Tenants
Basic Tenant Creation
Tenant with Custom Configuration
Working with Tenants
Get Tenant Agent Context
To perform operations as a specific tenant:Scoped Operations
For single operations, use the convenience method:Tenant Operations
Create Connection for Tenant
Issue Credential as Tenant
Managing Tenants
List All Tenants
Get Tenant by ID
Update Tenant
Delete Tenant
Message Routing
The tenants module automatically routes incoming DIDComm messages to the correct tenant based on the recipient keys.How Routing Works
- Message arrives at root agent endpoint
- Routing module extracts recipient key from message
- Tenant is identified from routing records
- Message is processed in tenant context
Manual Routing Registration
Routing records are created automatically, but you can manage them manually:Storage Considerations
Askar Multi-Tenancy Modes
When using@credo-ts/askar, choose a database scheme:
Profile Per Wallet (Recommended)
- Single database with profiles for each tenant
- Better resource efficiency
- Easier backups
Database Per Wallet
- Separate database per tenant
- Better isolation
- Independent scaling
Events
Tenant Events
Best Practices
1. Always Close Tenant Sessions
2. Use Tenant-Specific Configuration
3. Handle Tenant Errors
4. Implement Tenant Authentication
Example: Multi-Tenant API
API Reference
agent.modules.tenants.createTenant()- Create new tenantagent.modules.tenants.getTenantAgent()- Get tenant agent contextagent.modules.tenants.withTenantAgent()- Execute with tenant contextagent.modules.tenants.getAllTenants()- List all tenantsagent.modules.tenants.getTenantById()- Get specific tenantagent.modules.tenants.updateTenant()- Update tenant configagent.modules.tenants.deleteTenantById()- Delete tenant