Multi-Tenant Headers
Detailed implementation of multi-tenant headers for organization and project context
Multi-Tenant Headers
Overview
This guide provides a detailed implementation for multi-tenant headers using the architecture defined in the Header Customization Architecture document. Multi-tenant headers enable your API to understand the organizational context of each request, allowing proper data segmentation and access control in multi-tenant applications.
Quick Start
If you need to quickly implement tenant headers in your application, follow these three steps:
-
Create your tenant store:
-
Implement tenant header provider:
-
Register with your API client:
For complete implementation details, continue reading the sections below.
Purpose & Scope
This implementation guide covers:
- Zustand store for tenant context management
- Header provider implementation for tenant headers
- UI components for tenant selection
- Testing strategies specific to tenant headers
- Backend integration considerations
Prerequisites
Before implementing multi-tenant headers, ensure you're familiar with:
- Header Customization Architecture - Core header architecture
- Client State Management - Zustand store patterns
Tenant Header Architecture
The multi-tenant header system consists of three main components:
Tenant Store Implementation
The tenant store manages the current organization and project selection:
Tenant Header Provider
The header provider reads from the tenant store and adds the appropriate headers:
UI Implementation
Tenant Selector Component
A reusable component for selecting the current organization and project:
Tenant Header Display
A component to show the current tenant context:
Integration with API Clients
Register the tenant header provider with your API clients:
Backend Integration
The backend API will need to extract and validate the tenant headers:
Testing Tenant Headers
Best Practices
Tenant Header Security
Organization and project IDs should be treated as sensitive identifiers. While not as sensitive as authentication tokens, they should be protected from tampering and manipulation.
Key security considerations:
- Use non-sequential, non-predictable IDs for organizations and projects
- Validate tenancy on the server for every authenticated request
- Implement proper access control to prevent tenant ID spoofing
- Consider using signed tenant tokens for high-security applications
Performance Considerations
- Store recently used projects in memory to reduce selection operations
- Consider batching organization/project data for offline access
- Cache tenant validation on the server to reduce database lookups
User Experience
- Save and restore the last used organization and project
- Provide clear visual indication of the current tenant context
- Consider auto-selecting projects if there's only one available
- Allow quick switching between frequently used contexts
Tenant Header Use Cases
-
Data Segmentation: Ensure API responses only include data from the selected organization and project.
-
Multi-tenant Dashboards: Display metrics and data specific to the current organization context.
-
Role-Based Access Control: Combine tenant headers with user roles for fine-grained permissions.
-
Audit Logging: Include tenant context in all audit logs to track actions across organizations.
Troubleshooting
| Issue | Possible Cause | Solution |
|---|---|---|
| Missing tenant headers | Tenant not selected in UI | Ensure organization is selected and stored |
| Missing project context | Project not selected | Verify project ID is set in tenant store |
| 401 Unauthorized errors | Missing required tenant headers | Check if server requires tenant headers for this endpoint |
| Incorrect data returned | Wrong tenant context active | Verify the correct organization/project is selected |
Migration Considerations
If you're migrating from a different tenant identification approach, follow these steps to transition smoothly:
From Context/Service-based Approach
If you previously used a context service pattern:
- Create the Zustand store first, maintaining the same interface where possible
- Initialize the store with any existing tenant data from your current implementation
- Keep both implementations running in parallel during transition
- Update components to use the Zustand store instead of the context service
- Remove the old implementation once all components have been updated
From URL-based Tenant Identification
If you were passing tenant information via URL segments:
- Implement route parameter extraction to set the tenant store state
- Add navigation middleware that updates the tenant store when routes change
- Gradually transition endpoints to use headers instead of URL parameters
From Per-request Header Addition
If you were manually adding tenant headers to each request:
- Create the tenant store to centralize tenant state
- Implement the header provider as shown in this guide
- Remove manual header addition from individual API calls
- Update tests to mock the tenant store instead of individual headers
Related Documents
- Header Customization Architecture - Core architecture for header customization
- Client State Management - Zustand store patterns used in this implementation
- Authentication Architecture - How tenant context relates to user authentication
- security - Best practices for secure multi-tenant implementations
- Project Structure - Where tenant-related code should be placed
- api-integration - Practical guide to integrating APIs with tenant context
Summary
Implementing tenant headers is critical for multi-tenant applications. By following the patterns in this guide, you can create a clean, maintainable solution that:
- Stores tenant context in Zustand for global access
- Automatically adds tenant headers to API requests
- Provides a consistent UI for tenant selection
- Ensures proper testing across all layers
This implementation aligns with our broader API header customization architecture while providing specific guidance for the multi-tenant use case.