Multi-tenant architecture for a WhatsApp SaaS
How to model tenants, channels and credentials in a WhatsApp SaaS so one customer never sees another: schema, request routing and webhook fan-in.
A SaaS that adds WhatsApp usually starts as a single integration and grows into dozens of customers, each with its own number, templates, webhooks and bill. The point where it breaks is rarely the code that sends a message. It is the data model underneath: if the tenant boundary is vague, one customer's webhook ends up in another customer's inbox. This article lays out a tenant model, the routing rules that keep it intact and the places where WhatsApp-specific limits cut across tenants.
Where should the tenant boundary sit?
The boundary should sit at the business that owns the WhatsApp number, with one tenant row per customer and one or more channels hanging from it. Everything that carries customer data (credentials, webhook endpoints, message history, templates, usage) must have a tenant identifier on the row and in every query, never inferred from a session or a URL alone.
WhatsApp adds two facts that shape this. First, Meta sets messaging limits per business portfolio, not per phone number, and the limit is shared by every number in that portfolio. Second, a new portfolio can register only two business phone numbers until it is verified or reaches a limit of 2,000, after which the cap rises to 20. A tenant that brings its own portfolio gets its own limits; tenants that share one portfolio share the ceiling. Record which portfolio each channel belongs to, because noisy-neighbour problems follow that relationship, not your internal tenant id.
What does a minimal schema look like?
A minimal schema has four tables: tenants, channels, credentials and webhook endpoints, each joined by tenant_id. The sketch below is deliberately small and uses a composite foreign key so a channel can never point at a credential owned by another tenant.
CREATE TABLE tenants (
id uuid PRIMARY KEY,
name text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE credentials (
id uuid PRIMARY KEY,
tenant_id uuid NOT NULL REFERENCES tenants(id),
kind text NOT NULL, -- 'hub_api_key' | 'channel_token' | 'webhook_secret'
secret_ref text NOT NULL, -- pointer into your vault, never the secret
expires_at timestamptz,
UNIQUE (id, tenant_id)
);
CREATE TABLE channels (
id uuid PRIMARY KEY,
tenant_id uuid NOT NULL REFERENCES tenants(id),
provider_id text NOT NULL, -- the channel id returned by the API provider
external_id text, -- your own id for the customer's line
credential_id uuid NOT NULL,
FOREIGN KEY (credential_id, tenant_id) REFERENCES credentials(id, tenant_id),
UNIQUE (provider_id)
);
CREATE INDEX channels_by_tenant ON channels (tenant_id);Storing a pointer to the secret instead of the secret keeps backups and replicas free of live credentials; the rotation article in this series covers the vault side. The unique constraint on provider_id matters for the next section, because it lets an inbound event resolve to exactly one tenant.
How do requests and webhooks find the right tenant?
Outbound requests resolve the tenant from your own authentication, and inbound webhooks resolve it from the channel identifier in the event, never from the URL path alone. Treating those as two different lookups prevents the most common leak: trusting a tenant id supplied by the caller.
With CRPRO Hub each customer is an organisation, a key is scoped to one organisation and an id from another organisation answers 404, so the provider already enforces the boundary on its side. Your side still needs its own enforcement, because your database holds the message copies and the business rules. A webhook endpoint registered with all_channels set to true receives events for every channel in the organisation, and the normalized event carries a channel_id you can map back to a tenant:
// POST /webhooks/hub (one endpoint serving many tenants)
const evento = JSON.parse(rawBody) // after the signature check
const canal = await db.channels.findByProviderId(evento.channel_id)
if (!canal) return res.status(200).end() // unknown channel: log and acknowledge
await queue.add({ tenantId: canal.tenant_id, eventId: evento.id, evento })
return res.status(200).end()Two details are worth copying. The handler acknowledges unknown channels with a 2xx and logs them, because a non-2xx response only produces retries that cannot succeed. And the tenant id travels with the job into the queue, so workers never look it up again from untrusted input. The webhooks reference describes the signature headers and delivery rules.
How strict should data isolation be?
Isolation should be enforced in the database, not only in application code. A forgotten WHERE clause is the usual cause of cross-tenant exposure, and row-level security or a mandatory tenant-scoped repository layer turns that mistake from a leak into an empty result.
- Put tenant_id in the primary lookup path of every table that holds customer data, including logs and outbox tables.
- Use row-level security or a repository that refuses to run a query without a tenant context.
- Give background jobs a tenant context at creation, as shown above, and fail the job if it is missing.
- Test isolation directly: create two tenants, write data for one, and assert the other reads nothing across every endpoint.
- Never reuse one webhook endpoint or one credential between tenants without an identifier that proves the origin.
Isolation also covers search and analytics. A shared index or a shared metrics dashboard needs a tenant filter baked into the query, otherwise an internal tool becomes the leak. If a support engineer needs cross-tenant access, make it an explicit, logged role rather than the default.
Which limits are shared across tenants?
Three limits cut across tenants and must be planned for: Meta's per-portfolio messaging limit, per-number throughput, and your provider's request rate limits. Each is shared at a different level, so one tenant's burst can affect others only at the matching level.
| Limit | Scope | What it means for a multi-tenant SaaS |
|---|---|---|
| Messaging limit (250, 2,000, 10,000, 100,000, unlimited) | Business portfolio, moving 24 hours | Tenants in the same portfolio share it; give each customer its own portfolio when volume matters |
| Throughput (80 messages per second by default) | Business phone number | A busy tenant cannot starve another tenant on a different number |
| Hub requests (120 per minute per organisation and credential) | Organisation and credential | Per-tenant credentials keep one tenant's polling from consuming another's budget |
| Hub sends (60 per minute per channel) | Channel | A burst on one line is contained to that line |
The practical rule: route each tenant through its own credential and its own channel, so the provider-side limits already partition the load for you. The throughput article in this series covers how to plan sends against those numbers.
How should tenant lifecycle events be handled?
Treat creation, suspension and deletion as explicit state transitions that revoke credentials, pause webhook endpoints and stop queued sends. A tenant that cancels should not keep a live key, and a suspended tenant should not keep receiving events into a database nobody reads.
Onboarding is the other transition. With a connection link, the customer authorises the number directly with Meta and your system never touches their Meta password; see the authentication guide for how keys are scoped, and the quickstart for the first authenticated call. Offboarding should archive the channel on the provider side, revoke the tenant's keys, and schedule deletion of stored messages according to the retention policy you promised.
Keep a status column on the tenant (active, suspended, closing) and check it in the same place you check authentication. A single choke point is far easier to audit than status checks scattered across handlers.
Frequently asked questions
Do I need one WhatsApp business portfolio per tenant?
Not technically, but messaging limits are set per business portfolio and shared by every number in it. Tenants that share a portfolio share the limit, so customers with meaningful volume are usually better off with their own.
Can one webhook endpoint serve every tenant?
Yes, provided each event is mapped to a tenant through its channel identifier and the signature is verified first. Never take the tenant from the URL or from a field the sender controls without checking it against your own channel table.
Where should API credentials be stored?
In a secrets manager, with only a reference stored in the tenant database. That keeps backups free of live secrets and makes rotation a vault operation rather than a data migration.
Fontes
Documentação oficial da Meta, conferida em 31 de outubro de 2026.