
# Map an external group to a role

`POST /v1/connections/{connection_id}/role-mappings` · permission `connections:write`

Maps a group value sent by the identity provider (`external_group_key`, matched exactly) to a role; at sign-in the membership receives the role with source `idp_mapping`. For an organization connection the role must be an environment role or a role of that organization. Mapping to a privileged role additionally requires `roles:write` and the mapping is created approved. At most 500 mappings per connection. The write is audited as `role_mapping.created`; the group value is not written to the audit log.

## Request

| Field | Type | Required | Description |
|---|---|---|---|
| `connection_id` | path | yes | Enterprise connection ID (`conn_…`). |
| `external_group_key` | string | yes | Group value from the IdP, up to 1024 characters, no control characters. |
| `role_id` | string | yes | Role ID (`rol_…`). |

## Response

`201 Created` with the mapping.

```json
{
  "id": "rmp_01j9xw0x4y5z9a1b3c5d7e9f1h",
  "connection_id": "conn_01j9xp6q0r1s5t7v9w1x3y5z7b",
  "organization_id": "org_01j9xk3m7q8r2s4t6v8w0y2z4a",
  "external_group_key": "cn=engineering,ou=groups,dc=example,dc=com",
  "role_id": "rol_01j9xr7s1t2w6x8y0z2a4b6c8e",
  "role_privileged": false,
  "approved_at": null,
  "created_at": "2026-10-12T09:31:00.123456Z"
}
```

## Errors

| HTTP | code | When |
|---|---|---|
| 404 | `not_found` | The connection does not exist in this environment or is not an enterprise connection. |
| 403 | `privileged_role_mapping_forbidden` | The role is privileged and the credential lacks `roles:write`. |
| 409 | `conflict` | The group value is already mapped on this connection. |
| 409 | `role_mapping_limit_reached` | The connection already has 500 mappings. |
| 422 | `validation_failed` | `external_group_key` or `role_id` is missing or invalid, or the role is outside the connection'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` | A dependency is temporarily unavailable; retry after the `Retry-After` period. |

## Example

```bash
curl -i -X POST "https://api.eu.keenauth.com/v1/connections/conn_01j9xp6q0r1s5t7v9w1x3y5z7b/role-mappings" \
  -H "Authorization: Bearer $KEENAUTH_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -d '{"external_group_key":"cn=engineering,ou=groups,dc=example,dc=com","role_id":"rol_01j9xr7s1t2w6x8y0z2a4b6c8e"}'
```
