Authentication
How the engine's HTTP API is protected, which routes stay public and why, how a caller authenticates, and how to replace the bearer token with your own check.
This page is for whoever deploys the engine and whoever writes the service that calls it. At the end you will know which routes need credentials, how to set and rotate the token on each host, when to write your own authorize, and what the studio needs to read a deployed engine. Everything here is what the code does; there is no hidden second mechanism.
The model
The engine serves one HTTP API, and every route falls into one of two groups:
| Group | Routes | Who calls them |
|---|---|---|
| Public | GET /health, everything under /t/ | Load balancers, recipients' mail clients and devices |
| Protected | Every other route: /ingest, /devices*, /messages/opened, /subscriptions*, /users/forget, /deploy, /bundle, /bundle/deploy, /segments/sweep, /preview/* | Your backend, your deploy tooling |
A protected route is let through by the host's authorize function if it has one, and otherwise by the API_TOKEN bearer. There is one credential for the whole protected group: a caller that can ingest can also deploy and erase users. Treat the token as a server secret and never ship it to a browser or an app.
The bearer token
By default the host reads API_TOKEN from its environment and expects it on every protected route:
- The scheme is matched case-insensitively (
bearerworks), as RFC 9110 says schemes are. - The token is compared in constant time with
node:crypto'stimingSafeEqual. A length mismatch answers at once, which reveals the length and nothing else. This is why the Cloudflare Worker needs thenodejs_compatflag. - A missing or wrong token answers
401 { "error": "unauthorized" }.
JourneyClient sends it for you when you pass token:
Generate a long random value (the reference monorepo's environment schema requires at least 32 characters) and give the producer the same value under its own name.
Setting it on each host
| Host | Where API_TOKEN comes from |
|---|---|
| Cloudflare Worker | wrangler secret put API_TOKEN; locally a .dev.vars line |
| AWS Lambda | The function's environment, passed in the env you give journeyLambda |
| Node | vars: { API_TOKEN: process.env.API_TOKEN, ... } given to journeyNode |
The production gate
A host that has neither API_TOKEN nor authorize would let anyone ingest, change subscriptions, erase users and rewrite the routing table. So it refuses to serve that way unless it was told by name that it is a local or test instance:
ENVIRONMENTisdevelopmentortest, or a mode the config gives aruntimeentry of its own (rehearsal, say): the instance runs open.- Anything else, including
production, an unset variable, or a typo such asprodorProduction: every request exceptGET /healthand/t/...answers 503 until a token is set.
Comparing against the single string production would have served all of those typos open. The rule is the other way round: open has to be asked for.
Public routes and why they stay public
Some callers cannot carry a bearer:
| Caller | Routes |
|---|---|
| Recipients' mail clients and browsers | GET /t/o, GET /t/l, /t/u |
| Gmail and Yahoo one-click unsubscribe | POST /t/u/<message> (RFC 8058) |
| iOS notification extension, service worker | POST /t/d, POST /t/p |
| Health checks | GET /health |
These routes never ask authorize, and they are built so that being public is safe:
- They only record, keyed by a message id that is an unguessable UUID which only ever reached that mailbox or device. An id that is not a UUID names no copy.
- A tracked link redirects only to the URL recorded for that message and position when the email was sent, never to one the request carries, so the route cannot be used as an open redirect.
- An unsubscribe link shows a confirmation page, and only the confirmation, a POST that link scanners do not send, opts the recipient out. A transactional email never carried the link, so every
/t/uvariant answers 404 for it. GET /healthanswers{ "ok": true }and nothing else.
The email preference page reached from a footer works the same way, by message id, but its routes (/subscriptions/by-message/:messageId) are protected. Your own api proxies them and rate-limits them; the browser never talks to the engine. See Sending events.
There are no inbound webhook routes. trigger.webhook() compiles and deploys but is not routed yet (a deploy lists it under unrouted), and Slack and Discord messages are outgoing posts to URLs your templates read from the environment.
Your own authorize
Pass authorize to journeyWorker, journeyLambda or journeyNode to replace the bearer check with any scheme you choose: an HMAC over the body, a JWT, a header your proxy adds.
- It receives a copy of the request, so it can read the body and the route still gets it.
truelets the request through,falseanswers 401, and a throw answers 500 and is logged. An authorizer that fails never lets a request in.envis the environment the host validated.- The public routes never reach it.
- With
authorizeset, the production gate no longer demandsAPI_TOKEN: your function is the only check, so it must refuse when its own secret is missing.
An HMAC signature over the body, on AWS:
hmac is yours to write with WebCrypto. timingSafeEqual(a, b) hashes both strings with SHA-256 first, so it compares equal-length digests whatever the inputs' lengths. It and bearerMatches(request, token) are exported from sendwend/cloudflare and sendwend/aws. sendwend/node does not re-export them; on Node, compare with node:crypto's timingSafeEqual over hashes of your own.
Rotating the token
The default check knows one value, so replacing API_TOKEN means a moment where the engine and its callers disagree. To rotate without refused requests, accept two tokens for the length of the rollout with an authorizer built on bearerMatches:
- Set
API_TOKEN_NEXTon the engine and deploy this authorizer. - Switch every caller to the new value.
- Move the new value into
API_TOKEN, removeAPI_TOKEN_NEXT, and remove the authorizer if you no longer need it.
The function returns false when neither variable is set, so it cannot run open by accident.
Per host
Cloudflare
The Worker is served on a public URL, either workers.dev or a custom domain. The reference monorepo turns workers_dev and preview URLs off and serves the engine only on its custom domain, so there is one address to protect and to put rules on. JourneyClient takes a fetch option, so a producer Worker can also call the engine through a service binding's fetch; the engine does not know how a request arrived and still checks the bearer either way.
AWS Lambda
The function sits behind a Function URL with AuthType: NONE, and the API is protected in the app. An alias has one URL and one auth type, and the public routes above must stay public:
AWS_IAMwould break those routes. SigV4 also takes theAuthorizationheader, so the bearer could not travel with it.- CloudFront origin access control does not help either: with it, every POST must carry the body's SHA-256 in
x-amz-content-sha256, and a mail provider's one-click POST does not.
Grant lambda:InvokeFunctionUrl to everyone and lambda:InvokeFunction only with lambda:InvokedViaFunctionUrl = true, so the URL cannot be used to invoke the function directly. Internally, a journey run is an asynchronous invoke of the function with a run payload; the host runs it only if it matches, byte for byte, a start it recorded in Postgres, so a forged run payload cannot start a workflow for an arbitrary user. POST /deploy is always refused on Lambda: a version deploys its own bundle.
Every request that reaches the function is a durable execution and is billed, including one the app answers 401. Put CloudFront with a rate-based WAF rule in front and cap the function's reserved concurrency. Details on AWS Lambda.
Node
journeyNode serves plain HTTP on the port you give it. Terminate TLS in front of it (a load balancer or a reverse proxy) and keep the port off the public internet except through that proxy. The server reads at most 10 MB of body (maxBodyBytes) before it looks at anything, the token included, and answers 413 beyond that.
POST /deploy in production
A host that carries its own bundle (journeyWorker, journeyLambda, journeyNode) refuses POST /deploy with a 409 in production. It deploys by publishing a new version, which applies its own bundle; POST /bundle/deploy re-applies it after a rollback. A bundle posted by hand there could only be a leaked bearer at work, so the route that would accept arbitrary IR is closed. The bare handleRequest API carries no bundle and still accepts one.
The studio's access
The studio never calls the engine's HTTP API and never needs the token. It reads two things:
- The project, from disk on its own dev server. It does not serve
workflow.config.ts, the stats file or credential files kept beside the code (.env*,.dev.vars*,*.p8,*.pem,*.keyand similar) to the page, and it sends no CORS headers. - The database, with a connection string.
workflow.stats.ts(postgresStats) reads the reporting tables, andsendwend plan --database-urlreads which workflow versions journeys are pinned to. Both only read.
Give both a database role that can only SELECT on the engine's schema, and keep the connection string in the environment rather than in the stats file. See studio configuration.
Other secrets
API_TOKEN is the only credential callers present. The rest are credentials the engine presents to providers, read from its environment and never from the config: APNS_PRIVATE_KEY_BASE64, GOOGLE_APPLICATION_CREDENTIALS_BASE64, VAPID_PUBLIC_KEY and VAPID_PRIVATE_KEY, SES credentials on a Worker, and webhook URLs for Slack and Discord templates. Declare them in the host's env schema so a missing one fails at startup.