Authentication
Every Ema API call must carry a credential that identifies the caller and fixes their workspace. The platform supports two: a short-lived Bearer JWT for user-driven access (what the builder UI uses), and a long-lived workspace API key for programmatic, machine-to-machine access. This page explains how to obtain each and how to send it.
Pick one per request. A request must carry exactly one credential. If the Authorization header is present, the platform validates it as a JWT and will not fall back to an API key. If there is no Authorization header, it validates the X-API-Key header. With neither, the call returns 401 Unauthorized.
JWT access tokens
A JWT (JSON Web Token) is a short-lived access token tied to a specific user, workspace, and role. It is the right credential for anything that acts on behalf of a signed-in person.
Sign in with email and password
POST /api/v1/auth/login exchanges credentials for tokens. This route is public; it needs no prior credential.
POST https://your-tenant.ema.ai/api/v1/auth/login
Content-Type: application/json
{
"email": "[email protected]",
"password": "••••••••",
"tenant": "acme"
}
The tenant field is optional. Omit it for email-first sign-in (the server routes you to your home workspace); include the workspace slug when signing in through a workspace-pinned URL.
A successful response returns the tokens and the resolved user:
{
"access_token": "eyJhbGciOiJSUzI1Ni...",
"refresh_token": "eyJhbGciOiJSUzI1Ni...",
"user": {
"id": "8c1f...",
"tenant_id": "a4d2...",
"email": "[email protected]",
"name": "Avery Builder",
"role": "builder",
"status": "active"
}
}
The builder UI uses cookies, not headers. When you sign in through the browser, the gateway stores access_token and refresh_token as HttpOnly cookies and promotes the access token to an Authorization: Bearer header on every backend call for you. The JSON body above is what programmatic clients use; the cookie flow is for the SPA. Either way the underlying token is the same.
Send the token
Put the access token in the Authorization header on every subsequent call:
GET https://your-tenant.ema.ai/api/v1/workflow/workflows
Authorization: Bearer eyJhbGciOiJSUzI1Ni...
Refresh an expired token
Access tokens are short-lived. When one expires, exchange the refresh token for a new access token at POST /api/v1/auth/refresh:
POST https://your-tenant.ema.ai/api/v1/auth/refresh
Content-Type: application/json
{ "refresh_token": "eyJhbGciOiJSUzI1Ni..." }
{ "access_token": "eyJhbGciOiJSUzI1Ni..." }
Refresh tokens are long-lived and are not rotated on refresh. Keep reusing the one you received at sign-in until it expires. The endpoint returns only a new access token; there is no rolling-refresh scheme.
Other sign-in methods
Beyond email and password, the auth service supports:
- SSO (SAML and OIDC). Sign-in begins at
POST /api/v1/auth/auth/begin, which returns the method (sso,password,otp, orunknown) for an email. The SSO branch returns an opaque continuation token you redeem atGET /api/v1/auth/auth/sso/continue, which redirects into the configured identity provider. The callback returns the same token shape as login. - One-time passcode (OTP).
POST /api/v1/auth/auth/otp/requestemails a code;POST /api/v1/auth/auth/otp/verifyexchanges it for tokens (or a partial-auth token when TOTP is also required). - TOTP (two-factor). When a workspace requires it, OTP/SSO sign-in returns a
partial_auth_token; complete the second factor atPOST /api/v1/auth/auth/totp/verify.
Why the doubled /auth/auth/. login, refresh, and switch-tenant have dedicated gateway routes at /api/v1/auth/…. The SSO, OTP, and TOTP endpoints are served under the auth service's own /auth/* mount and reached through the generic proxy, so their public path carries the prefix twice — /api/v1/auth/auth/…. Use the paths exactly as written.
These are browser-driven flows; the email-and-password and refresh endpoints above are what most programmatic clients need.
Switch workspaces
If your user belongs to more than one workspace, list them with GET /api/v1/auth/users/me/tenants and mint tokens for a different one with POST /api/v1/auth/switch-tenant:
POST https://your-tenant.ema.ai/api/v1/auth/switch-tenant
Authorization: Bearer eyJhbGciOiJSUzI1Ni...
Content-Type: application/json
{ "target_tenant_id": "b7e9..." }
The response is a fresh LoginResponse (new tokens) scoped to the target workspace.
Workspace API keys
A workspace API key is a long-lived credential for programmatic access: scripts, backend services, and partner integrations that aren't tied to an interactive user session. It is the right credential for server-to-server automation.
Create a key
An administrator creates a key at POST /api/v1/auth/api-keys. This requires a JWT (you must be signed in to mint a key).
POST https://your-tenant.ema.ai/api/v1/auth/api-keys
Authorization: Bearer eyJhbGciOiJSUzI1Ni...
Content-Type: application/json
{
"name": "Nightly export job",
"key_type": "user"
}
The response includes the full key once. Store it securely, because it is never shown again:
{
"id": "f0a1...",
"name": "Nightly export job",
"key": "ema_sk_live_2f9c...full-secret",
"key_prefix": "ema_sk_live_2f9c",
"key_type": "user",
"created_at": "2026-06-01T12:00:00Z"
}
key_type is user (the default, a key owned by and acting as the creating user) or system (a workspace-level service key; creating one requires admin role). You may also pass operation_scope and resource_scope to narrow what the key can do, for example, restricting a key to a single AI Employee. You may also pass an optional expires_at timestamp (ISO 8601) to set an expiry date on the key; omit it for a key that never expires.
Send the key
Put the key in the X-API-Key header, with no Authorization header on the request:
GET https://your-tenant.ema.ai/api/v1/workflow/workflows
X-API-Key: ema_sk_live_2f9c...full-secret
The key carries its own workspace and owner, so calls made with it behave as that owner within that workspace.
Manage keys
- List the workspace's keys:
GET /api/v1/auth/api-keys. The response shows each key'skey_prefix(never the full secret), type, owner, revocation state, and expiry date (if set). - Update scope on a key:
PATCH /api/v1/auth/api-keys/{id}with a newoperation_scope/resource_scope. - Revoke a key:
DELETE /api/v1/auth/api-keys/{id}. Revocation is immediate. - Rotate a key:
POST /api/v1/auth/api-keys/{id}/rotate. See Rotate a key.
Treat keys like passwords. A workspace API key grants its owner's access to your workspace for as long as it lives. Never commit one to source control or embed it in client-side code. Rotate keys regularly; see Rotate a key.
System user API keys
A system user is an identity for an integration or script. It acts only through API keys and can't sign in. Use a system user when you want an integration to carry a stable, named identity that is separate from any person's account.
System admins, or a custom role with the auth.manage_system_users permission, manage system users in Settings → User Management → System users. Each workspace has a limit on active system users; the page shows how many are in use.
Availability. A system user's key works only through the token exchange described below, and Ema turns the exchange on per environment. Ask your Ema representative before you build an integration on a system user.
Create a system user
In the System users page, select New system user, give it a Name and a Role, and then issue it a key on the next page. You can also create one with POST /api/v1/auth/system-users and a body with name and role. Only roles you can assign are offered.
Issue a key
Issue a key for a system user at POST /api/v1/auth/api-keys. Pass key_type: "user" and set owner_id to the system user's ID. A system user's key carries no scope; the server rejects a scope field on such a key with 422.
POST https://your-tenant.ema.ai/api/v1/auth/api-keys
Authorization: Bearer eyJhbGciOiJSUzI1Ni...
Content-Type: application/json
{
"name": "Production sync",
"key_type": "user",
"owner_id": "00000000-0000-0000-0000-000000000001",
"expires_at": "2027-01-01T00:00:00Z"
}
The full key secret is returned once in the response and is never shown again. Copy it immediately. The expires_at field is optional; omit it for a key that never expires. In the app, Issue key offers 30, 90, 180, or 365 days, or Never; the default is 90 days.
Exchange the key for a token
A system user's key can't be sent directly in the X-API-Key header; a direct call is refused like an unknown key. Instead, exchange the key for a short-lived access token, then send that token as a Bearer JWT:
POST https://your-tenant.ema.ai/api/v1/auth/api-keys/exchange
X-API-Key: ema_sk_live_2f9c...full-secret
{
"access_token": "eyJhbGciOiJSUzI1Ni...",
"token_type": "Bearer",
"expires_in": 900,
"tenant_id": "a07d...",
"key_id": "6c1e..."
}
The token lasts up to 15 minutes (never past the key's own expiry) and comes with no refresh token, so exchange the key again when it runs out. The body is optional: pass { "tenant_id": "..." } to get a token for a child workspace where the system user is an active member. Without it, the token is for the key's own workspace.
Rotate a key
Rotating a key issues a new key with the same name, type, and owner, and keeps the old key working for an overlap period, so your integration can move over without downtime.
POST https://your-tenant.ema.ai/api/v1/auth/api-keys/{id}/rotate
Authorization: Bearer eyJhbGciOiJSUzI1Ni...
Content-Type: application/json
{ "overlap_seconds": 86400 }
The response contains the new key's secret (shown once), its ID, and old_expires_at, when the old key stops working. overlap_seconds is a whole number from 0 (end the old key now) to 604800 (7 days); the default is 86400 (24 hours). In the app, Rotate offers no overlap, 1 hour, 24 hours, or 7 days.
Revoke a key
DELETE https://your-tenant.ema.ai/api/v1/auth/api-keys/{id}
Authorization: Bearer eyJhbGciOiJSUzI1Ni...
Revocation is immediate. Any integration using the revoked key can no longer obtain a token.
Deactivate a system user
Deactivating a system user from the members list revokes every key it holds at once, in every workspace. Reactivation does not restore the keys; issue new keys after you reactivate the system user.
Key secrets are shown only once. Copy the secret immediately after issuing or rotating a key. If you lose it, revoke the key and issue a new one.
Roles and capabilities
Authenticating proves who you are; your role determines what you may do. The system roles, from most to least privileged, are env_admin, system_admin, builder_admin, user_admin, builder, user, and no_access. Building and publishing AI Employees generally requires builder, builder_admin, or system_admin. When a request is authenticated but the role lacks the required capability, the platform returns 403 Forbidden. When it lacks visibility of a resource, it returns 404 so existence is never leaked across workspaces.
What's next
- API Reference overview — base URL and the service prefix map.
- AI Employee API — build and publish AI Employees once you can authenticate.
- Triggering AI Employees via API — use a JWT or API key to start runs from your own systems.