Skip to main content

Assign a role to a user with the Node.js SDK

permit.api.users.assignRole() assigns a role to a user in a tenant of the Permit.io environment that your Node.js SDK client is connected to. This reference is for backend developers who grant roles from code, for example after a user signs up.

Prerequisites

Method signature

MethodSignatureStatus
permit.api.users.assignRoleassignRole(assignment: RoleAssignmentCreate): Promise<RoleAssignmentRead>Recommended
permit.api.assignRoleassignRole(assignedRole: RoleAssignmentCreate): Promise<RoleAssignmentRead>Deprecated alias with the same argument

Parameters

The method takes one argument: a RoleAssignmentCreate object. Pass the object itself, not a JSON string.

FieldTypeRequiredDescription
userstringYesKey or ID of the user who receives the role.
rolestringYesKey or ID of the role to assign.
tenantstringNoKey or ID of the tenant in which the user gets the role.
resource_instancestringNoResource instance for an instance-level role, as an ID or in the format resource_type:resource_instance_key.

Example RoleAssignmentCreate object:

{
role: "role",
tenant: "tenant",
user: "user"
}

Example

const response = await permit.api.users.assignRole(assignedRole);

Return value

The method resolves to a RoleAssignmentRead object with id, user, role, tenant, user_id, role_id, tenant_id, and created_at.

If the Permit API returns an error status code, permit.api.users.assignRole() throws a PermitApiError. The deprecated permit.api.assignRole() rethrows the underlying Axios error.