TenanciTenanci

Help centerYour app's customers

Tenant context

Tell each domain which customer it belongs to in your app and receive that customer's id and data, signed, on every request (X-Tenanci-Tenant) and, optionally, in the browser as window.TENANCI.

An app built for one customer can serve many, each on their own domain, without you writing any per-domain routing code. You tell each domain which customer it is in your app (the id plus a few fields such as name, plan and colors), and Tenanci hands that to your backend on every request, signed, and to the browser if you want.

How it works

  1. Each domain stores the customer id from your system (say, the barbershop's id in your database) and, optionally, some data about them.
  2. On every request through the domain, the Tenanci edge adds X-Tenanci-* headers with that context before it reaches your origin.
  3. If the project has a signing secret, the headers are signed (HMAC-SHA256): your backend checks the signature and knows the customer was set by Tenanci.
  4. With Available in the browser turned on, pages also get window.TENANCI, so your front end can show the customer's branding without calling your API.

Set the customer for a domain

  1. Open the project, go to Domains and click the domain.
  2. On the Settings tab, find the Customer for this domain card.
  3. In Customer id in your app, enter the id your system uses (letters, numbers and . _ : @ -).
  4. Under Customer data, click Add field for each piece of information: the Field name, the Type (Text, Number or Yes/no) and the Value.
  5. If your front end needs this data, turn on Available in the browser (window.TENANCI).
  6. Click Save. The context goes live within a few seconds.

To remove the customer from a domain, clear the id and save: the headers stop being sent.

Headers your backend receives

Header Contents
X-Tenanci-Tenant customer id (e.g. barbershop-123)
X-Tenanci-Tenant-Data customer data: JSON encoded as base64url, with no trailing =
X-Tenanci-Domain the domain the visitor opened (e.g. joesbarbershop.com)
X-Tenanci-Timestamp when the signature was made, in Unix seconds
X-Tenanci-Tenant-Signature hex(HMAC-SHA256(secret, message)); only present if the project has a secret

The headers are only sent when the domain has a customer id set.

Generate the signing secret

The secret belongs to the project and covers all of its domains.

  1. Open the project and go to the Customization tab.
  2. Open the Customers tab and find the Tenant context signature card.
  3. Click Generate secret (or Rotate secret if one already exists, and confirm).
  4. Copy the secret (it starts with tsk_) and store it on your backend, in an environment variable or a secrets manager.

Without a secret, the headers still arrive, just without X-Tenanci-Tenant-Signature.

Verify the signature

The signed message is four lines joined by \n, with no trailing \n, using the values exactly as they arrived in the headers (the data still base64url-encoded):

<X-Tenanci-Timestamp>
<X-Tenanci-Domain>
<X-Tenanci-Tenant>
<X-Tenanci-Tenant-Data>

Compute the HMAC-SHA256 of that message with the secret, as lowercase hex, and compare it with X-Tenanci-Tenant-Signature in constant time. Also reject the request if the timestamp is more than 5 minutes away from your clock.

import crypto from "node:crypto";

export function tenantOf(req, secret) {
  const ts = req.get("X-Tenanci-Timestamp");
  const domain = req.get("X-Tenanci-Domain");
  const id = req.get("X-Tenanci-Tenant");
  const data = req.get("X-Tenanci-Tenant-Data") ?? "";
  const sig = req.get("X-Tenanci-Tenant-Signature") ?? "";
  if (!ts || !domain || !id || Math.abs(Date.now() / 1000 - Number(ts)) > 300) return null;
  const want = crypto.createHmac("sha256", secret).update(`${ts}\n${domain}\n${id}\n${data}`).digest("hex");
  if (sig.length !== want.length || !crypto.timingSafeEqual(Buffer.from(sig), Buffer.from(want))) return null;
  return { id, domain, data: JSON.parse(Buffer.from(data, "base64url").toString("utf8") || "{}") };
}

In the browser (window.TENANCI)

With Available in the browser (window.TENANCI) turned on, every HTML page on the domain gets this in its <head>, before any of your app's scripts:

<script>window.TENANCI={"domain":"joesbarbershop.com","tenant":{"id":"barbershop-123","data":{"name":"Joe’s Barbershop","plan":"pro"}}};</script>

Use it to swap the name, colors or plan on screen without calling your API. It's the way to go for apps that can only read the customer in the browser (FlutterFlow, WeWeb, Lovable, Bolt); see integrations.

Through the API

Set a domain's customer with PUT /domains/:id/tenant:

curl -X PUT https://api.tenanci.com/v2/domains/dom_4h7n1q/tenant \
  -H "Authorization: Bearer tnc_live_xxxxxxxx_xxxxxxxxxxxx" \
  -H "Content-Type: application/json" \
  -d '{"id":"barbershop-123","data":{"name":"Joe’s Barbershop","plan":"pro"},"expose":true}'

Or right when you create the domain, with the tenant field on POST /domains:

curl -X POST https://api.tenanci.com/v2/domains \
  -H "Authorization: Bearer tnc_live_xxxxxxxx_xxxxxxxxxxxx" \
  -H "Content-Type: application/json" \
  -d '{"projectId":"prj_8f3k2m","hostname":"joesbarbershop.com","tenant":{"id":"barbershop-123","data":{"name":"Joe’s Barbershop"}}}'

To read what's set, use GET /domains/:id/tenant: the response also tells you whether the project already has a secret (projectHasSecret). You can generate or rotate the secret with POST /projects/:id/tenant-secret, which returns the new value only once. All of these calls need a key with domains:write (or domains:read to read).

Rules and limits

Item Rule
Customer id letters, numbers and . _ : @ -, up to 100 characters
Number of fields up to 40
Field name starts with a letter; letters, numbers and _ only; up to 40 characters
Value text (up to 500 characters), number, true/false or null

Data outside these rules is rejected with 400 (the message says which field), and the domain is left unchanged.