Multi-tenancy with Teams_
Learn how to implement multi-tenancy in your applications using Appwrite Teams.
2 min read
Appwrite Teams provides an effective way to implement multi-tenancy in your applications. Create a team for each tenant to handle multi-tenant apps with built-in data isolation.
What is multi-tenancy?
Multi-tenancy is a design pattern where a single instance of software serves multiple user groups (tenants). With Appwrite Teams, you can:
- Create a team for each tenant in your application
- Control access to resources using team-based permissions
- Define different roles within each tenant
- Scale to unlimited tenants without code changes
Common use cases
- SaaS applications: Organizations that need isolated data and users
- Collaborative tools: Projects with different access levels
- Educational platforms: Schools with teachers and students
- Business software: Companies with department-based access control
Create teams for tenants
When a new tenant signs up, create a dedicated team that serves as their isolated environment.
import { Client, Teams, ID } from "appwrite";
const client = new Client() .setEndpoint('https://<REGION>.cloud.appwrite.io/v1') .setProject('<PROJECT_ID>');
const teams = new Teams(client);
// Create team for a new tenantconst tenantTeam = await teams.create({ teamId: 'example_corp', name: 'Example Corp', roles: ['owner', 'admin', 'member']});Add members to tenants
Invite users to join a tenant using team memberships. Each member can be assigned different roles for access control.
import { Client, Teams } from "appwrite";
const client = new Client() .setEndpoint('https://<REGION>.cloud.appwrite.io/v1') .setProject('<PROJECT_ID>');
const teams = new Teams(client);
// Invite a member to the tenantconst membership = await teams.createMembership({ teamId: 'example_corp', roles: ['admin'], email: 'user@example.com', url: 'https://example.com/accept-invite'});Secure resources with team permissions
Control access to rows and resources using team-based permissions. This ensures data isolation between tenants.
import { Client, TablesDB, ID, Permission, Role } from "appwrite";
const client = new Client() .setEndpoint('https://<REGION>.cloud.appwrite.io/v1') .setProject('<PROJECT_ID>');
const tablesDB = new TablesDB(client);
// Create a row that only members of "Example Corp" tenant can accessconst row = await tablesDB.createRow({ databaseId: 'invoices_db', tableId: 'invoices', rowId: ID.unique(), data: { title: 'Q2 Invoice', amount: 2500.00, customer: 'Example Customer', status: 'pending', tenant_id: 'example_corp' }, permissions: [ // All Example Corp team members can read Permission.read(Role.team('example_corp')),
// Only admins can update Permission.write(Role.team('example_corp', ['admin'])) ]});Query tenant data
When querying data, users will automatically only see rows they have permission to access based on their team memberships.
import { Client, TablesDB, Query } from "appwrite";
const client = new Client() .setEndpoint('https://<REGION>.cloud.appwrite.io/v1') .setProject('<PROJECT_ID>');
const tablesDB = new TablesDB(client);
// Current user will only see invoices they have access toconst rows = await tablesDB.listRows({ databaseId: 'invoices_db', tableId: 'invoices'});
// For specific tenant data, you can add a query filterconst tenantRows = await tablesDB.listRows({ databaseId: 'invoices_db', tableId: 'invoices', queries: [ Query.equal('tenant_id', 'example_corp') ]});Was this page helpful?
Share what worked or what we should fix. Once approved, our agents automatically apply suggested updates to the docs.