TenanciTenanci

Help centerAPI Gateway & AuthMesh

API Gateway

Publish your app's API on every customer's domain with per-consumer keys, scopes, per-minute limits and a signed identity for your backend.

Available on: ScaleOpen in the dashboard

The API Gateway puts your app's API behind Tenanci, on every customer's domain. Tenanci checks the caller's key (or JWT), applies scopes and limits, and only then forwards the call to your backend, with the consumer's identity signed. You get per-customer keys, instant revocation and per-minute limits without writing any of it.

How it works

An API has a prefix (e.g. /api) and starts answering on that path on every domain of the project. For each call:

  1. Tenanci picks the route by method and path (the most specific one wins). No route: 404.
  2. If the route requires a key, it checks the API key in the configured header (or a JWT in Authorization: Bearer). Invalid, expired or revoked key: 401.
  3. It checks the route's scopes. Any missing: 403.
  4. It applies the consumer's per-minute limit. Over it: 429 with Retry-After.
  5. It forwards to your backend without the consumer's key and with signed identity headers.

TenantGuard inspects calls before all of this.

Create the API

  1. Open the project and go to the Gateway & AuthMesh tab.
  2. In the API Gateway card, click New API.
  3. Fill in:
    • Name;
    • Path prefix: where the API answers on the domains (e.g. /api);
    • Key header: the header consumers send their key in (default x-api-key);
    • Limit per minute (per consumer): default 600.
  4. Keep Accept API keys checked to use Tenanci-issued keys. If your consumers already sign in with a provider that issues JWTs, fill in JWKS URL (optional), JWT issuer (iss) and JWT audience (aud).
  5. Click Save. The Signing secret shows up: copy it and store it in your backend, because it won't be shown again. Then click I've copied it, close.
  6. The API is created switched off. Set up the routes and flip the toggle next to its name.

Routes

Click the API's name to open Routes and Consumers. For each route:

  1. Pick the Method (* matches all).
  2. Under Path, enter the path after the prefix: exact (/orders) or with * at the end (/orders/*).
  3. Under Destination (backend URL), enter your backend's https:// address.
  4. Optional: Scopes (comma-separated) the consumer must have (e.g. orders:read).
  5. Keep Requires key checked. Unchecked, the route is public (with a per-IP limit) and shows as public.
  6. Strip prefix: checked, /api/orders/1 reaches the backend as /orders/1.
  7. Click Add route.

Consumers and keys

Each customer of your API is a consumer, with its own keys and scopes.

  1. Under Consumers, fill in Consumer name, ID in your system (optional) and the Scopes.
  2. Click Add consumer.
  3. Click Generate key next to the consumer. The Consumer key (starting with tgw_) is shown only once: hand it to the customer.
  4. To cut off access, click Revoke next to the key. It takes effect immediately.

A consumer can have several keys at once, so you can switch keys without breaking the integration.

Verifying the signature in your backend

Every forwarded call arrives with four headers. Check the signature on every call: it proves the call went through the gateway and that nobody forged the identity.

Header Content
X-Tenanci-Consumer consumer id (the id returned when you created it) or the JWT sub; empty on public routes
X-Tenanci-Scopes the consumer's scopes, space-separated
X-Tenanci-Timestamp signing time, in Unix seconds
X-Tenanci-Signature hex(HMAC-SHA256(signing secret, message))

The message is four lines separated by \n: the timestamp, the uppercase method, the path with query exactly as your server received it, and the consumer. Reject the call if a header is missing, if the timestamp is more than 5 minutes away from your clock, or if the signature doesn't match (compare in constant time).

Node (Express)
import crypto from "node:crypto";

export function verifyTenanci(req, res, next) {
  const ts = req.get("X-Tenanci-Timestamp");
  const sig = req.get("X-Tenanci-Signature");
  const consumer = req.get("X-Tenanci-Consumer") ?? "";
  if (!ts || !sig || !/^\d+$/.test(ts) || Math.abs(Date.now() / 1000 - Number(ts)) > 300) return res.status(401).end();
  const msg = `${ts}\n${req.method.toUpperCase()}\n${req.originalUrl}\n${consumer}`;
  const expected = crypto.createHmac("sha256", process.env.TENANCI_GATEWAY_SECRET).update(msg).digest("hex");
  if (expected.length !== sig.length || !crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(sig))) {
    return res.status(401).end();
  }
  req.tenanci = { consumer, scopes: (req.get("X-Tenanci-Scopes") ?? "").split(" ").filter(Boolean) };
  next();
}

To check your implementation: secret segredo, timestamp 1700000000, method GET, path /orders/1?x=1, consumer c1 produce the signature 997e5fa8b3cfb080e09fac7ee9bece3cb7041c1cb046560a81114d2275254e5d.

Through the API

Everything belongs to the project (projects:write scope):

Operation What for
POST /projects/:p/gateway create the API (the response includes signingSecret, once)
PATCH /projects/:p/gateway/:api change it or turn it on ("enabled": true)
POST /projects/:p/gateway/:api/routes add a route
POST /projects/:p/gateway/:api/consumers create a consumer (accepts its own rateLimitPerMin)
POST /projects/:p/gateway/:api/consumers/:consumer/keys generate a key (accepts expiresAt)
DELETE /projects/:p/gateway/:api/keys/:key revoke a key
POST /projects/:p/gateway/:api/rotate-secret new signing secret
# new customer in your system → consumer + key
curl -X POST https://api.tenanci.com/v2/projects/prj_8f3k2m/gateway/<api>/consumers \
  -H "Authorization: Bearer tnc_live_xxxxxxxx_xxxxxxxxxxxx" -H "Content-Type: application/json" \
  -d '{"name":"Bright Smile Clinic","externalId":"cli_482","scopes":["schedule:read"]}'
# → { "id": "...", "name": "Bright Smile Clinic", ... }

curl -X POST https://api.tenanci.com/v2/projects/prj_8f3k2m/gateway/<api>/consumers/<consumer>/keys \
  -H "Authorization: Bearer tnc_live_xxxxxxxx_xxxxxxxxxxxx" -H "Content-Type: application/json" \
  -d '{"expiresAt":"2027-12-31T23:59:59Z"}'
# → { "id": "...", "prefix": "a1b2c3d4e5", "secret": "tgw_…", "expiresAt": "..." }