Overview
A role is a permission set: which objects an app can read or write, which fields it can see, and which platform-level capabilities it can use. Every app’s logic functions and front components inherit the permissions of the role marked withdefineApplicationRole() (see The default function role below).
src/roles/restricted-company-role.ts
recordsPermissions holds everything a role can do on records:
canReadAllRecords,canCreateAllRecords,canUpdateAllRecords,canSoftDeleteAllRecordsandcanDestroyAllRecordsat its top level apply to every object.canCreateAllRecordsfollowscanUpdateAllRecordswhen it is not set.objectslists what differs for one object: the same five grants for all its records,unreadableFieldsanduneditableFields(field universal identifiers; a field that cannot be read cannot be edited either), and its row-level records permissions.
Roles outside the workspace Custom application
An app installed from the marketplace, from a tarball or withtwenty app dev runs inside other
people’s workspaces, so its roles only describe what the app and its agents can do:
- Only agents hold them. Twenty stores the default function role
and the roles of your agents with
canBeAssignedToUsersandcanBeAssignedToApiKeysset tofalse, whatever the source says. Any otherdefineRole()must set both tofalseitself. - No field or row-level permissions.
unreadableFields,uneditableFields,rowLevelRecordsPermissionsand their deprecated forms (fieldPermissions,rowLevelPermissionPredicates,rowLevelPermissionPredicateGroups) are refused. The workspace configures them on its own roles.
twenty app build and dev report a role that breaks these rules as a warning. plan, apply
and dev then fail unless the target is the workspace Custom application, and publish always
fails. A sync sent straight to the API is refused with MARKETPLACE_APPLICATION_ROLE_ASSIGNABILITY
or MARKETPLACE_APPLICATION_ROLE_RESTRICTION. An installed app that predates these rules can
upgrade by removing what they refuse: turning a flag off or dropping a field or row-level
permission is accepted.
The workspace Custom application, the project twenty app pull creates for a workspace (see
Pulling an installed app back to source),
is the place for roles meant for workspace members or API keys, and for the field and row-level
permissions described below.
SystemPermissionFlag.USER_APPS and SystemPermissionFlag.WORKSPACE_APPS
are available from twenty-sdk 2.54 onward; earlier versions name them
CONNECTED_ACCOUNTS and APPLICATIONS. The old names are deprecated aliases
for the same flags, and a role keeps each flag once. Enumerating
SystemPermissionFlag, for example with Object.keys, still lists the old
names. The API also reports these flags under their old keys, for example in
currentUserWorkspace.permissionFlags.Row-level security
Only the workspace Custom application can declare row-level and field permissions. See
Roles outside the workspace Custom application.
recordsPermissions.objects can restrict some operations to part of its records with
rowLevelRecordsPermissions, a list of rules. A rule grants operations (canReadRecords,
canCreateRecords, canUpdateRecords, canSoftDeleteRecords, canDestroyRecords) on the records
matched by the predicates nested inside it; canCreateRecords follows canUpdateRecords when
omitted. A rule can also carry its own unreadableFields and
uneditableFields, which apply to the records it matches; such a rule needs at least one predicate
group.
A predicate references a field by its universalIdentifier, an operand, and (optionally) a
workspaceMember field whose value is injected at query time — so you can express “the record’s owner
relation is the current workspace member”. With relationFieldUniversalIdentifier, a predicate
compares a field of the records related through that relation field instead, for example the
messageChannelId of a message’s channel associations. This role can create any company, but reads
and updates only the companies it owns:
src/roles/partner-role.ts
Combining predicates with groups
The predicates of a rule are combined withAND. To combine some of them with OR (or to nest
logic), add predicateGroups to the rule: a group has a logicalOperator, its own predicates,
and may contain further predicateGroups. This role lets a partner see an Opportunity it either
owns or is the point of contact for:
src/roles/partner-opportunities-role.ts
Servers older than Twenty 2.59 do not read
recordsPermissions: declare engines.twenty as
>=2.59.0 in package.json when your app must not install on them.Deprecated properties
BeforerecordsPermissions, a role declared the same permissions with separate properties. They
still work, and defineRole prints a deprecation warning naming the ones a role uses. The build
sends them as written and Twenty converts them when it syncs the app. A role cannot mix
recordsPermissions with the deprecated grant properties:
Predicates declared on the role had no grants of their own. They are converted to one rule per
top-level predicate group, and one for the ungrouped predicates of each object. The rule takes the
operations the role had on that object, and the object’s
can…AllRecords are set to false so
that the predicates keep restricting it.
Notes:
- Give every rule, predicate and group a stable
universalIdentifier(any uuid) — it keys the entity across upgrades. - Predicates can reference fields owned by your app or by Twenty’s standard objects.
- Row-level security is enforced for workspaces on the Organization plan or above. On other plans the rules declared in an app manifest still sync, they are simply not enforced.
- The Metadata API behaves differently from the manifest here: without the entitlement,
upsertRowLevelPermissionRulesis rejected withROW_LEVEL_PERMISSION_FEATURE_DISABLEDrather than syncing. A workspace on a plan that does not include row-level security therefore cannot create predicates outside of an app manifest, even though therowLevelPermissionPredicatesfield is present on theRoletype when you introspect the schema.
The default function role
When you scaffold a new app, the CLI creates a default role file declared withdefineApplicationRole():
src/default-role.ts
defineApplicationRole() is a thin wrapper around defineRole() that flags the role used as your application’s default at install time. Validation is identical to defineRole, but the build pipeline auto-wires its universalIdentifier into the application manifest’s defaultRoleUniversalIdentifier — so you do not need to reference it from defineApplication yourself.
Notes:
- Exactly one
defineApplicationRole(...)is allowed per app — the manifest build will fail if it finds more than one. - Use
defineRole()(notdefineApplicationRole()) for any additional roles your app ships. - Setting
defaultRoleUniversalIdentifierexplicitly ondefineApplication()is still supported for backward compatibility, but is deprecated in favor ofdefineApplicationRole(). It can also name a role your app does not define, such as the workspace custom application’s built-in default role kept bytwenty app pull; the build then cannot check that role, so it must exist in the target workspace.
Agent roles
The application role is what a workspace reviews when it installs your app, so it must cover everything the app itself can do. Any role you assign to an agent throughroleUniversalIdentifier on defineAgent() must not grant more than the application role: no object action, field access, permission flag or row-level scope the application role lacks. If it does, the build fails with an error listing the excess grants. Roles the workspace Custom application ships for workspace members or API keys are not held to this rule, since the app never runs with them. The check needs the application role in your app’s source, so it is skipped when the default role is defined elsewhere.
Best practices
- Start from the scaffolded role, then progressively restrict it — the default grants read, update and soft-delete access on every object, which is rarely what you want in production.
- List in
recordsPermissions.objectsonly the objects your functions actually need. permissionFlagUniversalIdentifierscontrol access to platform-level capabilities. Keep them minimal.- A permission flag your app declares with
definePermissionFlag()carries apermissionType:'tool'(the default) for a capability an agent or a user can be granted,'settings'for a flag that unlocks a settings area. - See a working example:
examples/hello-world/src/roles/default-role.ts.