Building a multi-tenant CRM: scope and integrations
A travel agency CRM that grew from a spreadsheet replacement into Facebook lead capture, email automation, quote acceptance and integrations.
6 min read
A travel agency was running its customer relationships out of a shared spreadsheet and three WhatsApp groups. The build that replaced it was a multi-tenant CRM. The multi-tenancy mattered, and I have written about the isolation model elsewhere. It is not what made the project interesting.
The interesting part was that the scope kept growing, in a direction that was hard to plan for and easy to underestimate. Once the CRM was the place the business looked at every morning, it became the place every adjacent task wanted to live: leads from Facebook, follow-up emails, quotes sent and accepted, and a steady stream of connections to tools the agency already worked in.
The initial scope
The agreed scope was small on purpose:
- Contacts, a pipeline, activities and notes.
- Two tenants from day one, because the agency’s second brand was already in the spreadsheet.
- Roles, so a manager could see more than an agent.
- Nothing more. The measure of done was simply that the agency stopped using the spreadsheet.
That is the version I would still start with. A CRM that replaces a spreadsheet is a bounded, testable thing. The trouble starts the week it succeeds.
Where the scope changed
A tool becomes load-bearing the moment people open it first thing in the morning. After that, every adjacent task wants to live inside it, and each one arrives with the same sentence: “while you are in there.”
In roughly the order they arrived:
- Facebook lead automation. Leads from the agency’s Facebook presence flowing into the CRM, deduplicated and routed to the right tenant.
- Email automation. Follow-up sequences triggered by pipeline stage.
- Quote acceptance. Build a quote, send it, and record the acceptance.
- Ongoing integrations. The long tail of connections to the tools the agency already used.
Complexity: every integration asks the same questions
Each new piece looked small in isolation and added the same four questions:
- Whose data is this, and which tenant does it belong to?
- Who is allowed to see it, in the permission model?
- What happens when it runs with no logged-in user, as a webhook or a scheduled job does?
- What happens when it fails silently, which is how integrations usually fail?
The architecture did not change much as the scope grew. Tenancy stayed enforced at more than one layer. Application checks are for helpful error messages; the database is for the truth.
Row-level security was the piece that kept the growth cheap. Every tenant-owned table carried the same shape and the same policy:
-- Every tenant-owned table gets the same shape and the same policy.
alter table contacts enable row level security;
create policy contacts_tenant_isolation on contacts
using (tenant_id = (auth.jwt() ->> 'tenant_id')::uuid);
Two things followed. Application queries got simpler, because the database was already filtering. And a bug in application code stopped being a data breach and became a missing row.
Facebook lead automation
This is where the scope stopped being a CRM and started being a system.
A lead arrives from Facebook, and it has to become a contact in the right tenant, attributed to the right source, without creating a duplicate of a customer who already exists. The hard parts are not the connection. They are:
- Deduplication. A lead who enquired last month and again this week is one person, not two.
- Attribution. Which campaign, which page, which brand, so the follow-up is not generic.
- Tenant routing. When one page serves more than one brand, the lead’s destination is a policy decision, not a technical one.
- Order of arrival. The lead can arrive before the contact record that it should attach to, so the handler has to be safe to run twice.
That last point is the whole discipline of integrations in one line: make the handler idempotent, or you will spend your weekends removing duplicates.
Email automation
Email tied to pipeline stage is straightforward to describe and full of edge cases in practice.
Sending on behalf of a tenant raises questions the CRM did not have before: which sending domain, whose name is on the message, and what happens to an unsubscribe. An unsubscribe from one brand is not consent for another, but they might be the same person, and getting that wrong is both a deliverability problem and a trust problem.
The rule I landed on was simple to state and fiddly to implement: the tenant boundary applies to consent, not just to data.
Quote acceptance
Quotes started as a document the agency produced and ended as a record the system owns.
A quote has versions, because prices change. Acceptance has to record who accepted, when, and which version they accepted, because that record is what settles an argument later. Once acceptance lives in the system, it connects to the pipeline (the opportunity moved) and to whatever handles invoicing (an integration with its own tenancy questions).
Of everything the scope grew into, this is the one I would build first next time. It is the point where the CRM stops recording the business and starts running it.
Ongoing integrations are a standing cost
The long tail is easy to add and easy to forget. Each integration can break when the other side changes, and none of them fail loudly by default.
What I would hold every integration to now:
- Idempotent handlers, safe to run twice, because retries and duplicates are normal.
- Retries with backoff, and a dead-letter path, so a failure is stored rather than lost.
- A tenant on every job. Background jobs, scheduled reports and webhooks all run with no logged-in user, so make them run as a tenant, not as an administrator, and refuse them if the tenant is missing.
- An owner and a monitor. An integration nobody watches is one that will be discovered broken by a customer.
The exported nightly aggregate is the classic example. A summarisation job running as the database owner will cheerfully roll every tenant into one number. Making the job take a tenant id as its first argument is one line of setup and turns a silent bug into a loud failure.
What I would do differently
- 01
Model the permission, not the role
Roles are a naming convenience over a set of permissions. Storing the permission set and deriving roles from it survived every change the client asked for afterwards.
- 02
Put tenancy in the token, not the query
Carrying tenant identity in the session means no query can forget it. It also makes the same isolation work for background jobs and webhooks, which have no logged-in user.
- 03
Treat each integration as a product
An integration with no owner, no monitoring and no retry path is a future incident. The connection is the easy part; the operations around it are the product.
- 04
Write the tenancy tests before the features
The tenancy boundary deserves a written test suite from the start. The parts you cannot afford to get wrong are exactly the parts worth testing first, and the aggregate leak was the proof.
The permission model is covered in full in designing role-based access control that lasts, and the question of when a CRM stops being an internal tool and becomes a product is in turning an internal tool into a product.
If you are building something similar and the scope is already growing, that is the right time to talk, not after the integrations have owners nobody remembers naming.
On this page
Related reading
Turning an Internal Tool into a SaaS: the Checklist
The gap between a tool your team uses and a product strangers pay for: multi-tenancy, onboarding, billing, support and export, in the order they bite.
3 min read
Building a Fully Automated Daily Content Pipeline
Scheduled GitHub Actions, object storage, Playwright for images, ffmpeg for video and an API to post, the shape of a daily pipeline that runs unattended.
3 min read
Why Next.js and Firebase Still Work for Fast Business Apps
Auth, realtime data and hosting for near-zero effort gets a business app live in weeks. The two real costs are lock-in and queries.
3 min read