
# Replace the roles of a membership

`PUT /v1/organizations/{org_id}/memberships/{membership_id}/roles` · permission `organizations:write`

Replaces the membership's manually assigned roles with `role_ids` (an empty array removes them). Roles granted by IdP mappings or directory sources are not changed. If `role_version` is given it must equal the membership's current `version`, otherwise `409 conflict`. The last admin's admin role cannot be removed. The write is audited as `organization_membership.updated`.

## Request

| Field | Type | Required | Description |
|---|---|---|---|
| `org_id` | path | yes | Organization ID (`org_…`). |
| `membership_id` | path | yes | Membership ID (`mem_…`). |
| `role_ids` | string[] | yes | The full set of role IDs (`rol_…`), at most 50. |
| `role_version` | integer | no | Expected membership `version` (1 or more). |
| `revoke_sessions` | boolean | no | Default `true`. Chooses whether removing a privileged (`is_privileged`) role revokes the user's refresh token families in this organization context (plan §17.4); `false` turns this off. In this release the choice is only recorded in the `revoke_sessions` metadata of the `organization_membership.updated` audit record and no session is revoked; the revocation is planned (`planned`) in the authentication service. |

## Response

`200 OK` with the updated membership and its `ETag`.

```json
{
  "id": "mem_01j9xn5p9s0t4v6w8x0y2z4a6c",
  "organization_id": "org_01j9xk3m7q8r2s4t6v8w0y2z4a",
  "user_id": "usr_01j9xh2k6p7q1r3s5t7v9w1x3y",
  "status": "active",
  "source": "admin_api",
  "reason": null,
  "roles": [
    {
      "role_id": "rol_01j9xr7s1t2w6x8y0z2a4b6c8e",
      "source": "admin_api",
      "assigned_at": "2026-10-12T09:31:00.123456Z"
    }
  ],
  "expires_at": null,
  "created_at": "2026-10-12T09:31:00.123456Z",
  "activated_at": "2026-10-12T09:31:00.123456Z",
  "suspended_at": null,
  "removed_at": null,
  "updated_at": "2026-10-12T09:31:00.123456Z",
  "version": 2
}
```

## Errors

| HTTP | code | When |
|---|---|---|
| 404 | `not_found` | The membership does not exist in this organization. |
| 403 | `role_not_assignable` | A role is not an environment role or a role of this organization. |
| 409 | `conflict` | `role_version` does not match the membership's current version. |
| 409 | `last_org_admin` | The change would leave the organization without an active admin. |
| 422 | `validation_failed` | `role_ids` is missing or invalid, or `role_version` is below 1. |
| 401 | `unauthenticated` | The credential is missing, invalid or revoked. |
| 403 | `insufficient_permissions` | The credential lacks the required permission. Assigning a privileged (`is_privileged`) role also requires `roles:write`; without it nothing is written and `required_permissions` lists both permissions. |
| 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 PUT "https://api.eu.keenauth.com/v1/organizations/org_01j9xk3m7q8r2s4t6v8w0y2z4a/memberships/mem_01j9xn5p9s0t4v6w8x0y2z4a6c/roles" \
  -H "Authorization: Bearer $KEENAUTH_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -d '{"role_ids":["rol_01j9xr7s1t2w6x8y0z2a4b6c8e","rol_01j9xs8t2v3x7y9z1a3b5c7d9f"],"role_version":1}'
```

## Personal data

This endpoint carries personal data: organization memberships and invitations. [Personal data in the KeenAuth APIs](/docs/policies/personal-data) describes what is stored, how long it is kept and how it is deleted. Do not send special categories of personal data (health, biometric data, religion, political opinions, sexual orientation) in any field.
