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
- A
permitclient created with an API key for the environment. See Create a Permit client. - The user, the role, and the tenant exist in the environment. See Sync a user with the Node.js SDK, Create a role with the Node.js SDK, and Create a tenant with the Node.js SDK.
Method signature
| Method | Signature | Status |
|---|---|---|
permit.api.users.assignRole | assignRole(assignment: RoleAssignmentCreate): Promise<RoleAssignmentRead> | Recommended |
permit.api.assignRole | assignRole(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.
| Field | Type | Required | Description |
|---|---|---|---|
user | string | Yes | Key or ID of the user who receives the role. |
role | string | Yes | Key or ID of the role to assign. |
tenant | string | No | Key or ID of the tenant in which the user gets the role. |
resource_instance | string | No | Resource 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.