Multi-Tenant / Organizations
How to set up multi-tenant CodeSpar deployments with organizations, project isolation, x-org-id header scoping, file storage structure, Clerk Organizations integration, and API examples.
Multi-Tenant / Organizations
CodeSpar supports multi-tenant deployments where multiple organizations share a single instance. Each organization has isolated projects, agents, audit trails, and storage.
Overview
Each organization is fully isolated — agents, projects, audit logs, and user identities are scoped per organization.
x-org-id Header Scoping
All API requests can be scoped to a specific organization using the x-org-id header:
Without the header, requests operate in the default (single-tenant) context.
How x-org-id Works
When the x-org-id header is present on a request:
- The API validates that the organization exists
- All data reads are filtered to that organization's scope
- All data writes are stored in that organization's directory or database partition
- RBAC is evaluated against the user's role in that specific organization
When the header is absent, the API operates in single-tenant mode — all data is accessed without org filtering.
Scoped Endpoints
All major endpoints respect x-org-id:
| Endpoint | With x-org-id | Without x-org-id |
|---|---|---|
GET /api/agents | Returns agents for the org | Returns all agents |
GET /api/projects | Returns projects for the org | Returns all projects |
GET /api/audit | Returns audit entries for the org | Returns all audit entries |
POST /api/projects | Creates project in the org | Creates project in default scope |
GET /api/memory | Returns memory stats for the org | Returns global memory stats |
DELETE /api/projects/:id | Deletes only if project belongs to org | Deletes without org check |
Creating Organizations
Via API
Response:
The organization ID is automatically generated as org-{slug}.
After Creating an Organization
Once created, you can immediately start scoping operations to it:
List Organizations
Get Organization Details
Returns the organization with all its projects and members. See Organization API for full details.
File Storage — Organization Directory Structure
When using file-based storage (DATABASE_URL not set), each organization gets an isolated directory under .codespar/orgs/:
Key Files
| File | Location | Contents |
|---|---|---|
memory.json | .codespar/orgs/<orgId>/memory.json | Organization-level context and memory, including shared knowledge across projects |
audit.json | .codespar/orgs/<orgId>/audit.json | Organization-level audit trail with hash chain integrity |
config.json | .codespar/orgs/<orgId>/config.json | Organization settings: name, slug, autonomy defaults, notification preferences |
members.json | .codespar/orgs/<orgId>/members.json | Member list with identity IDs, display names, roles, and join dates |
Isolation Guarantees
| Data | Isolation Level |
|---|---|
| Project configuration | Per-org directory |
| Agent state | Per-project within org |
| Audit trail | Per-org, separate log files |
| Vector store | Per-project within org |
| User identities | Per-org (same user can be in multiple orgs) |
| RBAC roles | Per-org (different roles per org) |
| Memory/context | Per-org (organization-level), per-project (project-level) |
A user can be an owner in one organization and a reviewer in another.
Docker Volume Mount
In containerized deployments, mount the entire .codespar/ directory as a volume to persist all organization data:
Or use CODESPAR_WORK_DIR to set a custom location:
How Project Isolation Works Per Organization
Projects within an organization are fully isolated from each other:
| Resource | Isolation |
|---|---|
| Git repository | Each project links to a different repo |
| Agent instance | Each project has its own Project Agent |
| Autonomy level | Set independently per project |
| Audit trail | Logged per project (queryable per org) |
| Vector store | Separate embeddings per project |
| Webhooks | Independent webhook per repo |
Projects in different organizations cannot see or interact with each other. The Coordinator Agent can orchestrate across projects within the same organization (e.g., cascading deploys), but never across organizations.
Cross-Org Isolation Example
Adding Projects to an Organization
Create a project scoped to an organization:
The project, its agent, and all associated data will be stored under the organization's directory.
Via Chat Command
When a user sends commands from a channel, the organization is determined by the channel configuration. A Slack workspace or Discord guild can be mapped to a specific organization.
The project is created in the organization associated with the current channel.
Managing Members
Members are managed per organization. Each member has a role that determines their permissions within that organization.
Default Roles
When a user first interacts with an agent in an organization, they are assigned the read-only role by default. An owner or maintainer can upgrade their role.
Role Assignment
Roles are currently managed via the organization configuration file or the dashboard (future). The API for role management is:
The response includes a members array with role information:
Clerk Organizations Integration (Dashboard)
The CodeSpar dashboard (future) integrates with Clerk for organization management.
How Clerk Organizations Map to CodeSpar
Clerk Organizations provide a managed authentication and authorization layer. Each Clerk Organization maps 1:1 to a CodeSpar organization:
| Clerk Concept | CodeSpar Concept |
|---|---|
| Organization | Organization (x-org-id) |
| Organization member | Identity with RBAC role |
| Organization role | RBAC role |
| Organization invitation | Member onboarding |
Clerk-to-CodeSpar Role Mapping
| Clerk Role | CodeSpar Role |
|---|---|
org:admin | owner |
org:member | maintainer |
org:viewer | read-only |
Custom Clerk roles can be mapped to any CodeSpar role via configuration.
Dashboard: OrganizationSwitcher Component
The dashboard sidebar includes a Clerk OrganizationSwitcher component that allows users to switch between organizations. When the user switches:
- The active Clerk organization changes
- The dashboard updates the
x-org-idheader on all subsequent API requests - All displayed data (projects, agents, audit logs) updates to reflect the selected organization
- RBAC permissions are re-evaluated against the user's role in the new organization
API: x-org-id Header with Clerk
When the dashboard makes API requests, it includes the active Clerk organization ID as the x-org-id header:
All API endpoints respect this header and scope their data accordingly.
Setup
When using the dashboard with Clerk:
Clerk webhooks notify CodeSpar when:
- A new organization is created → CodeSpar creates a matching org directory
- A member is added or removed → CodeSpar updates
members.json - A member's role changes → CodeSpar updates the member's RBAC role
Example: Full Multi-Tenant Workflow
Here is a complete example of setting up a multi-tenant deployment from scratch.
1. Create an Organization
2. Create Projects in the Organization
3. Verify the Organization Structure
4. Operations Are Scoped
From this point, all operations using -H "x-org-id: org-acme-corp" are scoped:
Single-Tenant to Multi-Tenant Migration
If you started with a single-tenant setup and want to migrate to multi-tenant:
- Create an organization via the API
- Move existing projects into the organization
- Update channel configurations to map to the organization
- Assign roles to existing users
The existing .codespar/ data can be migrated by moving project directories into the organization structure:
Best Practices
| Practice | Recommendation |
|---|---|
| Organization naming | Use clear, unique slugs (e.g., acme-corp, not org1) |
| Role assignment | Start with read-only, promote as needed |
| Project naming | Match repository names for clarity |
| Autonomy levels | Set per-project based on project criticality |
| Audit review | Regularly review org-level audit logs |
| Member offboarding | Remove members promptly when they leave the team |
| Storage | Always use Docker volumes to persist .codespar/orgs/ data |
Next Steps
- Organization API — Full API reference
- Security — RBAC roles and permissions
- Configuration — Environment variables
- Multi-Channel Setup — Cross-channel identity within orgs