Welcome to Nebutra Sailor Docs 🎉
ChangelogIntroductionLicensing
Help Center (External) ↗

Multi-Tenancy Guide

Step-by-step guide to creating tenants, managing members, switching organizations, and isolating data.

Create an organization

New users are prompted to create an organization after sign-up. There's no separate client SDK — tenant/org management goes through Better Auth's organization plugin, client-side via authClient.organization.* or server-side via auth.api.*:

import { authClient } from "@/lib/auth-client";

const { data: org } = await authClient.organization.create({
  name: "Acme Corp",
  slug: "acme",
});
// org.id → the new organization's id

Invite a member

await authClient.organization.inviteMember({
  organizationId: "org_123",
  email: "alice@acme.com",
  role: "member",  // owner | admin | member | viewer
});

Alice receives a transactional email with an invitation link. Accepting the invite creates her membership and triggers the member.joined webhook event.

Change a member's role

await authClient.organization.updateMemberRole({
  organizationId: "org_123",
  memberId: "member_456",
  role: "admin",
});

You cannot change an owner's role unless you first transfer ownership. There must always be at least one owner.

Remove a member

await authClient.organization.removeMember({
  organizationId: "org_123",
  memberIdOrEmail: "user_456",
});

Removing a member:

  • Revokes all active sessions for that user in this org
  • Revokes any API keys they created
  • Does not delete their data (projects, resources remain)

Switch organizations

Users can switch between orgs they belong to. In the dashboard, the org switcher is in the top-left nav. Programmatically:

// Client-side — Better Auth
import { authClient } from "@/lib/auth-client";

function OrgSwitcher() {
  const { data: organizations } = authClient.useListOrganizations();
  const { data: activeOrganization } = authClient.useActiveOrganization();

  return (
    <select
      value={activeOrganization?.id}
      onChange={(e) => authClient.organization.setActive({ organizationId: e.target.value })}
    >
      {organizations?.map((org) => (
        <option key={org.id} value={org.id}>
          {org.name}
        </option>
      ))}
    </select>
  );
}

Data isolation in practice

All data queries are automatically scoped to the current tenant when using withRls:

import { withRls, getCurrentTenant } from "@nebutra/tenant";
import { prisma } from "@/lib/db";

export async function getProjects() {
  const tenant = getCurrentTenant();
  const db = withRls(prisma, tenant.tenantId);

  // This query ONLY returns projects for the current tenant
  // The WHERE tenant_id = '...' clause is injected by PostgreSQL RLS
  return db.project.findMany({
    orderBy: { createdAt: "desc" },
  });
}

Tenant-scoped API keys

API keys belong to a tenant, not a user. Create them in Settings → API Keys, which calls the gateway route directly (backends/gateway/src/routes/ai/api-keys.ts, mounted at /api/v1/ai/api-keys):

const res = await fetch("/api/v1/ai/api-keys", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ name: "Production CI" }),
});
const { key } = await res.json();
// key is shown ONCE — store it securely

API keys are hashed (SHA-256) before storage; we cannot recover the plaintext. List with GET, revoke with DELETE /:id, rename with PATCH /:id.

Delete an organization

Deleting an organization requires the owner role and re-authentication. Client-side, this goes through the Better Auth organization plugin:

await authClient.organization.delete({
  organizationId: "org_123",
});

Deleting an org soft-deletes org data and triggers the corresponding webhook event on @nebutra/webhooks — see Webhooks for the event payload shape.

Related

Multi-Tenancy Concepts

Architecture and isolation strategies explained.

Permissions

Full role and scope reference.

How is this guide?

Edit on GitHub

Last updated on

On this page

Create an organization
Invite a member
Change a member's role
Remove a member
Switch organizations
Data isolation in practice
Tenant-scoped API keys
Delete an organization
Related