
# Map a directory group to a role

`POST /v1/directories/{directory_id}/role-mappings` · permission `directories:write`

Maps a group of a SCIM directory (`directory_group_id`) to a role; members of the group receive the role in the directory's organization. The group must belong to this directory. The role must be an environment role or a role of the directory's organization. Mapping to a privileged role additionally requires `roles:write` and the mapping is created approved. A directory that is being deleted accepts no new mappings. At most 500 mappings per directory. The write is audited as `role_mapping.created`.

## Request

| Field | Type | Required | Description |
|---|---|---|---|
| `directory_id` | path | yes | SCIM directory ID (`dir_…`). |
| `directory_group_id` | string | yes | Directory group ID (`dsg_…`). |
| `role_id` | string | yes | Role ID (`rol_…`). |

## Response

`201 Created` with the mapping.

```json
{
  "id": "rmp_01j9xw0x4y5z9a1b3c5d7e9f1h",
  "directory_id": "dir_01j9xm2n4p6q8r0s2t4v6w8y0z",
  "organization_id": "org_01j9xk3m7q8r2s4t6v8w0y2z4a",
  "directory_group_id": "dsg_01j9xn3p5q7r9s1t3v5w7x9y1a",
  "role_id": "rol_01j9xr7s1t2w6x8y0z2a4b6c8e",
  "role_privileged": false,
  "approved_at": null,
  "created_at": "2026-10-13T09:31:00.123456Z"
}
```

## Errors

| HTTP | code | When |
|---|---|---|
| 404 | `not_found` | The directory does not exist in this environment. |
| 403 | `privileged_role_mapping_forbidden` | The role is privileged and the credential lacks `roles:write`. |
| 409 | `conflict` | The group is already mapped on this directory, or the directory is being deleted. |
| 409 | `role_mapping_limit_reached` | The directory already has 500 mappings. |
| 422 | `validation_failed` | `directory_group_id` or `role_id` is missing or invalid, the group does not belong to this directory (`unknown_group`), or the role is outside the directory's scope. |
| 401 | `unauthenticated` | The credential is missing, invalid or revoked. |
| 403 | `insufficient_permissions` | The credential lacks the required permission. |
| 429 | `rate_limited` | The rate limit was exceeded; retry after the `Retry-After` period. |
| 503 | `service_unavailable` | The directory record could not be checked or a dependency is temporarily unavailable; retry after the `Retry-After` period. |

## Example

```bash
curl -i -X POST "https://api.eu.keenauth.com/v1/directories/dir_01j9xm2n4p6q8r0s2t4v6w8y0z/role-mappings" \
  -H "Authorization: Bearer $KEENAUTH_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -d '{"directory_group_id":"dsg_01j9xn3p5q7r9s1t3v5w7x9y1a","role_id":"rol_01j9xr7s1t2w6x8y0z2a4b6c8e"}'
```
