Roles let you limit what someone who can open your app is allowed to do inside it. You declare roles in helix.config.ts, restrict an HTTP function with an access export, and assign roles to people in the dashboard. A request from someone without a permitted role gets a 403 and your handler never runs. Roles need @trayai/helix-sdk and @trayai/helix-cli 1.34.0 or later.
Two questions, two layers
| Layer | Question it answers | Where you set it |
|---|---|---|
| Access Control settings | Who can open the app? | The project’s Access Control tab in the dashboard |
| Roles | What can someone who opened it do? | roles and access in your code, assignments in the dashboard |
A person without access never reaches the app, whatever roles they hold. Roles only narrow what an allowed person can use. Roles come from your own configuration, so they don’t depend on how your directory is organized. For the first layer, see identity and roles.
Declare roles
Roles are a flat map in helix.config.ts. The key is the role id you use in code. The value is the name the dashboard shows when someone assigns it.
// helix.config.ts
import { defineConfig } from '@trayai/helix-sdk';
export default defineConfig({
name: 'support-desk',
workspaceId: 'f8e7d6c5-4b3a-2190-8765-abcdef012345',
roles: {
admin: 'Administrator',
editor: 'Content Editor',
viewer: 'Read-Only Viewer',
},
});
Role ids and names are 1 to 255 characters each, and a project can declare up to 100 roles. The declared roles are part of each deployment: when a deployment goes live, the dashboard offers that deployment’s roles for assignment.
If you remove a role from config and redeploy, assignments to that role are kept but grant nothing, and the dashboard marks them so you can clean them up. If you declare the role again later, the assignments work again.
Restrict a function by role
Export access from an HTTP function file, the same way you export input and output.
// functions/api/v1/settings.put.ts
import { defineFunction } from '@trayai/helix-sdk';
import { z } from 'zod';
// Only admins can update settings
export const access = {
allowRoles: ['admin'],
};
export const input = z.object({
theme: z.enum(['light', 'dark']),
});
export default defineFunction<typeof input>(async (ctx) => {
await saveSettings(ctx, ctx.input);
return { saved: true };
});
You can also pass access as an option to defineFunction. If you set it in both places, the option on defineFunction wins.
| Field | Type | Behavior |
|---|---|---|
allowRoles | string[] | The request is allowed only if the user holds at least one listed role. Must not be empty. |
denyRoles | string[] | The request is rejected if the user holds any listed role. |
- Deny wins. If you set both,
denyRolesis checked first. A user who holds a denied role is rejected even if they also hold an allowed one. - No
accessexport means no role restriction. Anyone who can open the app can call the function. - An anonymous caller holds no roles. A function with
allowRolesrejects it. A function with onlydenyRoleslets it through. - The check runs before your input is validated. A rejected caller learns nothing about the shape of your input.
- A malformed rule fails when the function loads. An unknown key such as
allowedRoles, a value that isn’t an array of role ids, or an emptyallowRolesstops the function from loading, so a typo can’t silently allow everyone. accessapplies to HTTP functions. Schedules run without a user, so there is nothing to check. Tools served from your MCP endpoint don’t enforceaccess, although they can readctx.identity.user.rolesand check it themselves.
A request rejected by allowRoles returns 403 with a JSON body:
{
"error": "forbidden",
"message": "This endpoint requires one of the following roles: admin"
}
A request rejected by denyRoles returns the same shape with the message You do not have access to this endpoint. The rule is the same in helix dev and in production, so you can verify a restriction locally.
Read roles in a handler
For logic a declarative rule can’t express, such as showing a different dashboard to admins, read the roles from ctx.identity.
// functions/api/v1/dashboard.get.ts
import { defineFunction } from '@trayai/helix-sdk';
export default defineFunction(async (ctx) => {
const isAdmin = ctx.identity?.user.roles.includes('admin') ?? false;
return isAdmin ? getFullDashboard(ctx) : getLimitedDashboard(ctx);
});
ctx.identity.user.roles holds the role ids the person has been given on this app, and is [] when they hold none. ctx.identity.org carries the organization as { id, name }. Reading ctx.identity in a scheduled function throws, because a schedule runs without a user. See ctx.identity for the rest of the object.
Assign roles in the dashboard
Roles are assigned per person, in the Users section of the project’s Access Control tab. A person can hold several roles. The roles on offer are the ones the live deployment declares, shown by their display names.
Anyone who can add and remove people on the app can also assign roles: Contributors and Admins. Everyone else sees the roles read-only. Assigning a role the app doesn’t declare is refused.
Add people with roles
Choose Add people, pick the people, then pick their roles under Roles (optional). Everyone you picked is added to the app and given those roles in one step. Each person’s row afterward lists their role names, two at a time, with a +N count that shows the rest on hover.
To change roles later, open the row’s menu and choose to edit roles. The dialog names the person, lists the app’s roles, and shows a filter field once the app declares more than eight.
People with a role but no access
A person can hold a role without having been added to the app. Their row shows Not added beside the name. If the app’s general access setting doesn’t cover them either, the row shows an amber Can’t open the app instead. Choose Add to app from the row’s menu to add them.
Roles the app no longer declares
If you remove a role from helix.config.ts and redeploy, anyone still holding it keeps an amber pill with a tooltip saying the app no longer has the role. A leftover role grants nothing. The edit dialog groups leftover roles together, and you can remove them there or declare the role again to restore them. Leftover roles that don’t fit on the row get their own amber count, separate from the blue +N, so a working role never looks removed.
Remove a person
Removing someone from the app also removes the roles they hold in it. The dialog says so before you confirm. On an app open to the whole workspace or organization, a removed person can still open the app if the general access setting covers them, but they hold no roles. On a workspace-wide app, someone outside the workspace loses access entirely.
Each assignment and removal is recorded in the organization’s activity trail, with who made the change, who received the role, and which role. Assigning a role someone already holds isn’t recorded again.
Assigning roles from the CLI isn’t available. For the access side of the same tab, see identity administration.
Test as a role locally
helix dev has no sign-in, so it gives every request a static identity, and ctx.identity is populated by default. By default that is a dev user who holds every role you declared, so allowRoles rules never block you while you build. A function with denyRoles does reject the default dev user whenever it lists a role you declared, so test those with an identity file or the X-Helix-Dev-Roles header.
email: dev@localhost
name: Dev User
org: Dev Org
roles: every role declared in helix.config.ts
To work as someone with fewer roles, pass an identity file:
helix dev --identity ./.helix/identities/editor.json
{
"user": {
"email": "alice@company.com",
"roles": ["editor"]
}
}
Anything you leave out falls back to the default dev user, so { "user": { "roles": ["viewer"] } } is enough to test as a viewer. A file that contains just null simulates an anonymous request, where ctx.identity is null. Helix checks the file when the dev server starts and refuses to run if the file is missing, isn’t valid JSON, or names a role you didn’t declare. After that, edits to the file apply to the next request without a restart.
helix init creates two starting points under .helix/identities/: example-user.json, which has no roles so you edit it, and anonymous.json. Copy them for each persona you want to test.
For a one-off request, send a header instead:
| Header | Effect |
|---|---|
X-Helix-Dev-Roles | A comma-separated list of role ids that replaces the user’s roles for this request. An id you didn’t declare returns 400. A blank value means no roles. |
X-Helix-Dev-User | An email address that replaces the user’s email for this request. The name is derived from it. |
# Check that a viewer can't update settings
curl -i -X PUT -H "X-Helix-Dev-Roles: viewer" localhost:3000/api/v1/settings
These headers and the identity file only work in helix dev. They do nothing on a deployed app.