Authentication
All data routes require authentication. On the Buildpad Engine backend, you send every request to the Engine’s URL, not to the Studio’s URL. The Engine accepts a JWT access token. It accepts a static token on the MCP endpoint, and on the item routes for a data token (see Static Token). The Engine sets no cookie.
All API routes require authentication. (Public access at data model level.) The platform supports three authentication methods.
1. Login (JWT Token)
Exchange credentials for a short-lived access token and a refresh token.
POST /api/auth/logincurl
curl -X POST https://your-domain.com/api/auth/login \
-H "Content-Type: application/json" \
-d '{"email": "user@example.com", "password": "secret"}'Response:
{
"data": {
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"refresh_token": "3f9c…(64 hex characters)",
"expires": 1761400000,
"mfa_required": false,
"mfa_enrollment_required": false
}
}expiresis the time when the access token expires, in Unix seconds.mfa_requiredistruewhen the account has a verified second factor. Ask for a code next. See Multi-Factor Authentication.mfa_enrollment_requiredistruewhen the Engine requires a factor and the account has none.- A sign-in always gives an
aal1session.
{
"data": {
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"refresh_token": "v1.AAAA...",
"expires": 3600000
}
}expires is the token lifetime in milliseconds.
Tenants
On the Engine, each session belongs to one tenant, or to none:
- Send
tenant(the tenant id) in the login body to sign in to that tenant. A tenant where the person has no role gets the same401 INVALID_CREDENTIALSas a wrong password. - Without
tenant, the Engine picks the host if the person has a role there, or else the person’s only tenant. A person in two or more tenants gets a session bound to no tenant. That session reaches only the identity routes until it switches. GET /api/auth/tenantslists the caller’s tenants.POST /api/auth/tenantreturns a new token pair for another tenant.
On the Supabase backend, a session is not bound to a tenant. A request chooses its scope with the X-Resource-Uri header. See Scopes.
Sign in with a Supabase client
The Engine also serves a Supabase-compatible sign-in at /auth/v1. Point a Supabase client at the Engine URL. Name the tenant in the X-Tenant-Id header:
curl -X POST "https://<engine-url>/auth/v1/token?grant_type=password" \
-H "Content-Type: application/json" \
-H "X-Tenant-Id: <tenant id>" \
-d '{"email": "user@example.com", "password": "secret"}'| Supabase client call | On the Engine |
|---|---|
signInWithPassword | Available. POST /auth/v1/token?grant_type=password |
| Session refresh | Available. POST /auth/v1/token?grant_type=refresh_token |
signInWithIdToken | Available when the operator configures an identity provider. See Sign in with XAppHub |
getUser | Available. GET /auth/v1/user |
signOut | Available. POST /auth/v1/logout. The client’s default sign-out ends the user’s sessions on every device |
| Sign-up, magic links, OAuth, password recovery, multi-factor | Not available. Use the Engine’s /api/auth/* routes instead |
A token from /auth/v1 and a token from /api/auth/login are the same kind of token. Each one renews at either refresh route.
On the Supabase backend, a Supabase client signs in to the Supabase Auth of the project, with the same accounts as /api/auth/login.
Sign in with XAppHub
XAppHub (formerly “My Apps”) is an identity provider. When the Engine’s operator configures it, an app’s server can exchange an XAppHub ID token for an Engine session. The app’s server completes the sign-in with XAppHub, then calls:
POST /auth/v1/token?grant_type=id_token
X-Tenant-Id: <tenant id>
{ "provider": "my-apps", "id_token": "<ID token>", "nonce": "<raw nonce>" }With a Supabase client, this is signInWithIdToken. The answer is the same session that a password sign-in gives.
- The operator turns the grant on with the Engine’s
AUTH_EXTERNAL_*settings.AUTH_EXTERNAL_ISSUERSnames the providers, for examplemy-apps. For each provider, the Engine needs its exact issuer and its key address. While no provider is set, the grant answers400 unsupported_grant_type. - A tenant administrator names the client ids whose tokens the tenant accepts, with
POST /api/auth/external-sign-in/audiences. A token for another client is refused. - The first sign-in of a person creates an account with no role. The account can read only what the tenant grants to every caller. Give it a role in the Studio, or name a starting role (below).
- A starting role. A tenant administrator can name one role that the Engine gives to new XAppHub accounts, for the email domains you list. See A starting role for new accounts.
- Administrators are refused. A host account and a tenant administrator cannot sign in with XAppHub. They sign in with an email and a password. If a linked account becomes an administrator, its XAppHub session ends at its next renewal.
- An existing account with the same email and no XAppHub link is refused. The Engine does not link it.
- A deleted account is refused. If an administrator restores it, it signs in again as the same account.
- Every refusal answers
400 invalid_grant, the same answer as a wrong password. A503means that the Engine cannot check the token now. Sign the user in with XAppHub again to get a new token.
In the Studio, the Users page shows an XAppHub badge next to a linked user. See Users.
The id_token grant is a feature of the Buildpad Engine. It is not available on the Supabase backend.
A starting role for new accounts
By default, a new XAppHub account holds no role until an administrator gives it one. A tenant administrator can instead name a starting role for a provider, with the email domains that receive it:
PUT /api/auth/external-sign-in/default-role
Authorization: Bearer <tenant administrator's token>
{ "provider": "my-apps", "role": "<role id>", "domains": ["example.com"] }The answer is the same as GET /api/auth/external-sign-in, whose providers now show default_role (the role’s id and name, or null) and default_role_domains. To stop giving the role, send DELETE /api/auth/external-sign-in/default-role?provider=my-apps.
- Who gets it. A person whose first XAppHub sign-in creates their account, or creates their profile in this tenant, when the token’s email is verified and its domain is in the list. The first session already holds the role, so the person does not wait or sign in again. Anyone else is created with no role, as before.
- Exact domains only.
example.comdoes not includemail.example.com; list each domain you mean, up to 20. - Existing accounts are not changed. The role is given only when the account or the profile is created. If you take the role away later, the next sign-in does not give it back.
- Never an administrator role. A role that gives administrator access, or that cannot be given across the whole tenant, is refused with
400 INVALID_PAYLOAD. The Engine checks again when it gives the role: if the role has gained administrator access since, the person gets no role and the activity log says why. - It follows the role. Deleting the role clears the setting. Adding, removing or replacing a trusted client id leaves it as it is.
- The activity log records each change to the setting, and each role given at sign-in, with the origin
external_sign_in.
The domain list is only as strong as the identity provider’s own check that an address belongs to its domain. Confirm that check with the provider’s owner before you name a starting role.
A starting role for XAppHub accounts is a feature of the Buildpad Engine. It is not available on the Supabase backend.
Using the token
Include the access token in the Authorization header on every subsequent request:
Authorization: Bearer <access_token>Refreshing the token
Access tokens expire after the Engine’s AUTH_ACCESS_TOKEN_EXPIRES (default: 1 hour). Use the refresh token to get a new pair:
POST /api/auth/refresh{ "refresh_token": "3f9c…" }A refresh token works once. Each refresh replaces it with a new one:
- Reuse window. For 10 seconds after a refresh, the Engine accepts the replaced token again and answers with the same session. This lets two tabs, or a server and a browser, renew one session at the same time. The operator can change the window with
AUTH_REFRESH_TOKEN_REUSE_INTERVAL(0 to 60 seconds, 0 turns it off). - After the window, the replaced token is refused with
401 INVALID_TOKEN, and the Engine ends the whole session. The Engine treats a late reuse as a stolen token. Sign the user in again. - Two refreshes of one token that arrive together make one new pair, not two.
Access tokens expire according to your Supabase JWT config (default: 1 hour). Use the refresh token to get a new pair:
POST /api/auth/refresh{ "refresh_token": "v1.AAAA..." }2. Session Cookie (Browser)
The Engine does not use cookies. A browser app keeps the tokens itself, or calls the Engine from its own server routes, and sends Authorization: Bearer <access_token> with each request. The Engine accepts requests from another origin without credentials.
When logging in from a browser, the platform automatically sets secure httpOnly cookies. Subsequent requests from the same browser include the cookie automatically — no manual token management needed.
Session cookies are the default method used by the DaaS Studio (admin UI). For server-to-server or mobile clients, use JWT bearer tokens or static tokens instead.
3. Static Token (API Keys)
Static tokens are long-lived API keys that don’t expire. They are ideal for CI/CD pipelines, server-to-server integrations, and automation scripts.
On the Engine, a static token has a kind:
| Kind | Accepted on | Who can hold one |
|---|---|---|
mcp (the default) | The MCP endpoint, /api/mcp, only | Any account |
data | The MCP endpoint, the item routes (/api/items/:collection and /api/items/:collection/:id) and GET /api/users/me | Only an account that is not an administrator in the token’s tenant |
Every other route refuses a static token with 401 TOKEN_INVALID. The token acts in the tenant where it was written, whatever X-Tenant-Id says. A token in the query string is refused.
A token that you generate in the Studio is an mcp token. To write a data token, an administrator sends token_kind with the token:
curl -X PATCH "https://<engine-url>/api/users/<user_id>" \
-H "Authorization: Bearer <admin_token>" \
-H "Content-Type: application/json" \
-d '{"token": "<generated_string>", "token_kind": "data"}'A cleared or replaced token stops working on the next request.
Generate a token
Via the UI: Users → Edit User → Generate Token → Save
Via the API:
# 1. Generate a random string
curl "https://your-domain.com/api/utils/random/string?length=32" \
-H "Authorization: Bearer <admin_token>"
# 2. Assign it to a user
curl -X PATCH "https://your-domain.com/api/users/<user_id>" \
-H "Authorization: Bearer <admin_token>" \
-H "Content-Type: application/json" \
-d '{"token": "<generated_string>"}'Use the static token
curl https://your-domain.com/api/items/posts \
-H "Authorization: Bearer <static_token>"Static tokens bypass Row-Level Security at the database layer. Permissions are still enforced at the application layer. Use JWT tokens where database-level RLS enforcement is required.
Password Reset
POST /api/auth/password/request → sends reset email
Body: { email }
POST /api/auth/password/reset → completes reset with token from email
Body: { token, password }On the Engine, the reset link opens the page in the Engine’s AUTH_PASSWORD_RESET_URL, with the token as the token query parameter. The token lasts one hour and works once. The mail goes through the Engine’s SMTP relay (MAIL_SMTP_*). Without a relay or without AUTH_PASSWORD_RESET_URL, no mail is sent. The request always answers 200, so it does not tell which addresses have an account.
Password reset requires SMTP or Supabase email to be configured in your project. Without this, reset emails will not be delivered.
Current User
GET /api/users/meReturns the authenticated user’s profile and roles. The Engine does not serve /api/auth/user.
GET /api/auth/userReturns the authenticated user’s profile and role information.
Logout
POST /api/auth/logoutEnds the session of the access token that you send. Other sessions of the user, on other devices, stay signed in. To end every session of the user, send { "everywhere": true } in the body.
Invalidates the current session server-side. Scope depends on how you authenticate:
- Bearer token (
Authorizationheader) — only the session for that specific token is revoked; other active sessions on other devices are unaffected. - Session cookie (browser) — all sessions for the user are revoked across all devices.
Bearer token clients must pass the token in the Authorization header so the server knows which session to revoke. Omitting the header falls back to cookie-based session logout.
curl
curl -X POST https://your-domain.com/api/auth/logout \
-H "Authorization: Bearer <access_token>"The Engine revokes the session immediately. Its access token and its refresh token are refused on the next request, before they expire.
The session is revoked server-side immediately — the token will be rejected by any subsequent request even before its natural expiry time. Exception: if the deployment enables local JWT verification (SUPABASE_JWT_SECRET), access tokens for active users are verified in-process without consulting the auth server, so a logged-out session’s token keeps working until it expires. Leave that variable unset if immediate revocation matters.
Multi-Factor Authentication
A user can add a TOTP factor to their account. After they do, a password
sign-in alone is no longer enough: requests answer 403 with the code
MFA_REQUIRED until the session reaches assurance level 2. Set
MFA_FORCE_ENABLED=true to require a factor from every user. On the Engine, set it on the Engine. The Engine also needs MFA_ENCRYPTION_KEY.
See Multi-Factor Authentication for the enrollment flow, the step-up call, and the endpoints.
Error Responses
All auth errors follow the standard error envelope:
{
"errors": [
{
"message": "Invalid user credentials",
"extensions": { "code": "INVALID_CREDENTIALS" }
}
]
}| Code | Meaning |
|---|---|
INVALID_CREDENTIALS | Wrong email or password, or a tenant where the person has no role |
INVALID_TOKEN | Refresh token is invalid or expired — log in again |
INVALID_PAYLOAD | Missing or malformed request body field |
MFA_REQUIRED | Session is at aal1 and a second factor is needed — see Multi-Factor Authentication |
INTERNAL_SERVER_ERROR | Unexpected server-side failure |
On the Engine, a request with a bad access token answers 401 with TOKEN_EXPIRED or TOKEN_INVALID. The /auth/v1 routes answer in the Supabase format instead, for example 400 with {"error": "invalid_grant"}.