> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mka1.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Custom roles

> Create custom roles and give them to members of your organization with the API and the SDKs.

A custom role is a named set of scopes that an owner or admin of your organization gives to a member.
This guide shows how to create roles, change and delete them, and give them to members, with the MKA1 SDKs, the CLI and the HTTP API.
For every field and status code, see the [Organization](https://docs.mka1.com/api-reference/organization/list-an-organizations-custom-roles.md) section of the API reference.

## Before you start

* **Use an owner's or admin's API key.** Listing, creating, changing and deleting roles, and giving them to members, need the API key of a person who is an owner or admin of the organization. Service account keys are refused for these calls. Listing members and reading your own scopes need only membership of the organization.
* **Give the key the right scopes.** The key needs `read:org` to list roles and members, and `write:org` to create, change, delete or give a role.
* **Know your organization's ID.** Every request takes it as `orgId`. The examples use the placeholder `<org-id>`.
* **Pass the API key on each call.** These operations take their credentials as a per-call argument: the first argument in the TypeScript SDK, `security` in the Python and C# SDKs, and `--bearer-auth` in the CLI. The CLI examples pass the `MKA1_BEARER_AUTH` variable from your shell.

## Add a role

### List roles and supported scopes

Start by listing the organization's roles.
The response holds `roles`, the organization's custom roles, and `supportedScopes`, the scopes a custom role can hold.
Choose a role's scopes from `supportedScopes`.

```ts TypeScript SDK theme={null}
import { SDK } from '@meetkai/mka1';

const sdk = new SDK({ bearerAuth: 'Bearer <mka1-api-key>' });
const security = { bearerAuth: 'Bearer <mka1-api-key>' };

const { roles, supportedScopes } = await sdk.auth.orgs.listRoles(security, {
  orgId: '<org-id>',
});
```

```python Python SDK theme={null}
from meetkai_mka1 import SDK

sdk = SDK(bearer_auth="Bearer <mka1-api-key>")
security = {"bearer_auth": "Bearer <mka1-api-key>"}

result = sdk.auth.orgs.list_roles(security=security, org_id="<org-id>")
print(result.roles, result.supported_scopes)
```

```csharp C# SDK theme={null}
using MeetKai.MKA1;
using MeetKai.MKA1.Types.Requests;

var sdk = new SDK(bearerAuth: "Bearer <mka1-api-key>");
var apiKey = "Bearer <mka1-api-key>";

var result = await sdk.Auth.Orgs.ListRolesAsync(
    security: new ListOrgRolesSecurity() { BearerAuth = apiKey },
    orgId: "<org-id>");

Console.WriteLine(string.Join(", ", result.Object!.SupportedScopes));
```

```bash CLI theme={null}
mka1 auth orgs list-roles --org-id <org-id> --bearer-auth "$MKA1_BEARER_AUTH"
```

```bash Bash theme={null}
curl https://apigw.mka1.com/api/v1/authentication/orgs/<org-id>/roles \
  --header 'Authorization: Bearer <mka1-api-key>'
```

### Create a role

Create a role with a `name` and a list of `scopes`.
The name is 1 to 64 characters with no comma, control or invisible character.
It must differ from every other role's name in the organization, ignoring case, and cannot be `owner`, `admin` or `member`.
A role may hold no scopes.
An organization can have up to 100 custom roles.

```ts TypeScript SDK theme={null}
const { role } = await sdk.auth.orgs.createRole(security, {
  orgId: '<org-id>',
  requestBody: {
    name: 'Support agent',
    scopes: ['read:responses', 'read:files'],
  },
});
```

```python Python SDK theme={null}
result = sdk.auth.orgs.create_role(
    security=security,
    org_id="<org-id>",
    name="Support agent",
    scopes=["read:responses", "read:files"],
)
role = result.role
```

```csharp C# SDK theme={null}
var created = await sdk.Auth.Orgs.CreateRoleAsync(
    security: new CreateOrgRoleSecurity() { BearerAuth = apiKey },
    orgId: "<org-id>",
    body: new CreateOrgRoleRequestBody()
    {
        Name = "Support agent",
        Scopes = new List<string>() { "read:responses", "read:files" },
    });

var role = created.Object!.Role;
```

```bash CLI theme={null}
mka1 auth orgs create-role --org-id <org-id> --bearer-auth "$MKA1_BEARER_AUTH" \
  --name 'Support agent' \
  --scopes read:responses \
  --scopes read:files
```

```bash Bash theme={null}
curl https://apigw.mka1.com/api/v1/authentication/orgs/<org-id>/roles \
  --request POST \
  --header 'Content-Type: application/json' \
  --header 'Authorization: Bearer <mka1-api-key>' \
  --data '{
    "name": "Support agent",
    "scopes": ["read:responses", "read:files"]
  }'
```

The response returns `201` with the new `role`: its `id`, `name`, `scopes`, `createdAt` and `updatedAt`.
Keep the role's `id`: you use it to change, delete and give the role.

The API answers:

* `409` with `ROLE_NAME_TAKEN` when another role already has the name.
* `400` with `ROLE_SCOPES_UNSUPPORTED` when a scope is not in `supportedScopes`.
* `409` with `ROLE_LIMIT_REACHED` when the organization already has 100 custom roles.

## Manage roles

### Rename a role or change its scopes

Send a new `name`, new `scopes`, or both.
`scopes` replaces the role's whole set, so send every scope the role should hold.
A change applies to every member who holds the role.

```ts TypeScript SDK theme={null}
const { role: updated } = await sdk.auth.orgs.updateRole(security, {
  orgId: '<org-id>',
  roleId: '<role-id>',
  requestBody: {
    name: 'Support lead',
    scopes: ['read:responses', 'read:files', 'write:files'],
  },
});
```

```python Python SDK theme={null}
result = sdk.auth.orgs.update_role(
    security=security,
    org_id="<org-id>",
    role_id="<role-id>",
    name="Support lead",
    scopes=["read:responses", "read:files", "write:files"],
)
```

```csharp C# SDK theme={null}
var updated = await sdk.Auth.Orgs.UpdateRoleAsync(
    security: new UpdateOrgRoleSecurity() { BearerAuth = apiKey },
    orgId: "<org-id>",
    roleId: "<role-id>",
    body: new UpdateOrgRoleRequestBody()
    {
        Name = "Support lead",
        Scopes = new List<string>() { "read:responses", "read:files", "write:files" },
    });
```

```bash CLI theme={null}
mka1 auth orgs update-role --org-id <org-id> --role-id <role-id> --bearer-auth "$MKA1_BEARER_AUTH" \
  --name 'Support lead' \
  --scopes read:responses \
  --scopes read:files \
  --scopes write:files
```

```bash Bash theme={null}
curl https://apigw.mka1.com/api/v1/authentication/orgs/<org-id>/roles/<role-id> \
  --request PATCH \
  --header 'Content-Type: application/json' \
  --header 'Authorization: Bearer <mka1-api-key>' \
  --data '{
    "name": "Support lead",
    "scopes": ["read:responses", "read:files", "write:files"]
  }'
```

The response returns the updated `role`.
The name and scope rules for creating a role apply here too, with one exception: a scope the role already holds is kept even if it is no longer in `supportedScopes`.

### Delete a role

```ts TypeScript SDK theme={null}
const { deleted } = await sdk.auth.orgs.deleteRole(security, {
  orgId: '<org-id>',
  roleId: '<role-id>',
});
```

```python Python SDK theme={null}
result = sdk.auth.orgs.delete_role(
    security=security,
    org_id="<org-id>",
    role_id="<role-id>",
)
```

```csharp C# SDK theme={null}
var deleted = await sdk.Auth.Orgs.DeleteRoleAsync(
    security: new DeleteOrgRoleSecurity() { BearerAuth = apiKey },
    orgId: "<org-id>",
    roleId: "<role-id>");
```

```bash CLI theme={null}
mka1 auth orgs delete-role --org-id <org-id> --role-id <role-id> --bearer-auth "$MKA1_BEARER_AUTH"
```

```bash Bash theme={null}
curl https://apigw.mka1.com/api/v1/authentication/orgs/<org-id>/roles/<role-id> \
  --request DELETE \
  --header 'Authorization: Bearer <mka1-api-key>'
```

The response returns the role's `id` and `deleted: true`.

While a member still holds the role, the API answers `409` with `ROLE_IN_USE` and keeps the role.
[Clear the role](#give-change-or-clear-a-members-role) from each member who holds it, then delete it again.

## Give a role to a member

A custom role is given to a member whose organization role is `member`.
A member holds at most one custom role, and keeps the organization role `member`.

### Find the member

List the organization's members to find the person.
Each member has an `id`, which is the `memberId` you send when you give a role.
It is not the same as `userId` or `user.id`.

```ts TypeScript SDK theme={null}
const { members } = await sdk.auth.orgs.listMembers(security, {
  orgId: '<org-id>',
});

const member = members.find((m) => m.user.email === 'alex@example.com');
```

```python Python SDK theme={null}
result = sdk.auth.orgs.list_members(security=security, org_id="<org-id>")

member = next(m for m in result.members if m.user.email == "alex@example.com")
```

```csharp C# SDK theme={null}
var listed = await sdk.Auth.Orgs.ListMembersAsync(
    security: new ListOrgMembersSecurity() { BearerAuth = apiKey },
    orgId: "<org-id>");

var member = listed.Object!.Members.First(m => m.User.Email == "alex@example.com");
```

```bash CLI theme={null}
mka1 auth orgs list-members --org-id <org-id> --bearer-auth "$MKA1_BEARER_AUTH"
```

```bash Bash theme={null}
curl https://apigw.mka1.com/api/v1/authentication/orgs/<org-id>/members \
  --header 'Authorization: Bearer <mka1-api-key>'
```

Each member in the list also carries `role`, `customRoles` and `scopes`.
The list returns up to 100 members per call, with the organization's `total`. In a larger organization, page through it with `limit` and `offset`.

### Give, change or clear a member's role

Send the member's custom roles as `roleIds`.
The list replaces the roles the member holds:

* To give a role, send a list with that role's `id`.
* To change it, send a list with the other role's `id`.
* To clear it, send an empty list.

```ts TypeScript SDK theme={null}
const { member: assigned } = await sdk.auth.orgs.setMemberRoles(security, {
  orgId: '<org-id>',
  memberId: '<member-id>',
  requestBody: {
    roleIds: ['<role-id>'],
  },
});

// To clear the member's custom role, send an empty list
await sdk.auth.orgs.setMemberRoles(security, {
  orgId: '<org-id>',
  memberId: '<member-id>',
  requestBody: { roleIds: [] },
});
```

```python Python SDK theme={null}
result = sdk.auth.orgs.set_member_roles(
    security=security,
    org_id="<org-id>",
    member_id="<member-id>",
    role_ids=["<role-id>"],
)

# To clear the member's custom role, send an empty list
sdk.auth.orgs.set_member_roles(
    security=security,
    org_id="<org-id>",
    member_id="<member-id>",
    role_ids=[],
)
```

```csharp C# SDK theme={null}
var assigned = await sdk.Auth.Orgs.SetMemberRolesAsync(
    security: new SetOrgMemberRolesSecurity() { BearerAuth = apiKey },
    orgId: "<org-id>",
    memberId: "<member-id>",
    body: new SetOrgMemberRolesRequestBody()
    {
        RoleIds = new List<string>() { "<role-id>" },
    });

// To clear the member's custom role, send an empty list
await sdk.Auth.Orgs.SetMemberRolesAsync(
    security: new SetOrgMemberRolesSecurity() { BearerAuth = apiKey },
    orgId: "<org-id>",
    memberId: "<member-id>",
    body: new SetOrgMemberRolesRequestBody() { RoleIds = new List<string>() });
```

```bash CLI theme={null}
mka1 auth orgs set-member-roles --org-id <org-id> --member-id <member-id> --bearer-auth "$MKA1_BEARER_AUTH" \
  --role-ids <role-id>

# To clear the member's custom role, send an empty list
mka1 auth orgs set-member-roles --org-id <org-id> --member-id <member-id> --bearer-auth "$MKA1_BEARER_AUTH" \
  --body '{"roleIds": []}'
```

```bash Bash theme={null}
curl https://apigw.mka1.com/api/v1/authentication/orgs/<org-id>/members/<member-id>/roles \
  --request PUT \
  --header 'Content-Type: application/json' \
  --header 'Authorization: Bearer <mka1-api-key>' \
  --data '{ "roleIds": ["<role-id>"] }'

# To clear the member's custom role, send an empty list
curl https://apigw.mka1.com/api/v1/authentication/orgs/<org-id>/members/<member-id>/roles \
  --request PUT \
  --header 'Content-Type: application/json' \
  --header 'Authorization: Bearer <mka1-api-key>' \
  --data '{ "roleIds": [] }'
```

The response returns the `member` with its `role`, its `customRoles` (`id` and `name`) and its `scopes`.

The API answers:

* `409` with `CUSTOM_ROLE_REQUIRES_MEMBER` when the person is an owner, an admin or a service account. Change their organization role to `member` first. An empty list is accepted for anyone.
* `400` with `CUSTOM_ROLE_LIMIT` when `roleIds` holds more than one role.

A member who is later made an owner or admin, or who leaves the organization, no longer holds the custom role.

### Read a person's scopes

`scopes` on each member in the [member list](#find-the-member) is the person's scopes in the organization.
For a member who holds a custom role, it is that role's scopes.
For an owner, an admin, or a member without a custom role, it is what their organization role can put on an API key.
A change to a role shows on the next read.

To read your own role and scopes, call `getMe` with your own API key.
Any human member of the organization can make this call with a key that has `read:org`.

```ts TypeScript SDK theme={null}
const me = await sdk.auth.orgs.getMe(security, { orgId: '<org-id>' });

console.log(me.role, me.customRoles, me.scopes);
```

```python Python SDK theme={null}
me = sdk.auth.orgs.get_me(security=security, org_id="<org-id>")

print(me.role, me.custom_roles, me.scopes)
```

```csharp C# SDK theme={null}
var me = await sdk.Auth.Orgs.GetMeAsync(
    security: new GetOrgMeSecurity() { BearerAuth = apiKey },
    orgId: "<org-id>");

Console.WriteLine(string.Join(", ", me.Object!.Scopes));
```

```bash CLI theme={null}
mka1 auth orgs get-me --org-id <org-id> --bearer-auth "$MKA1_BEARER_AUTH"
```

```bash Bash theme={null}
curl https://apigw.mka1.com/api/v1/authentication/orgs/<org-id>/me \
  --header 'Authorization: Bearer <mka1-api-key>'
```

The response returns your `organizationId`, `userId`, `memberId`, `role`, `customRoles` and `scopes`.

## Related guides

* [Teams and members](https://docs.mka1.com/docs/teams-and-members.md) for the organization's built-in roles: owner, admin and member.
* [Authentication](https://docs.mka1.com/docs/authentication.md) for API keys and their scopes.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.