# Grant a role to a user

`POST /v1/admin/roles/assign`

URL: https://docs.spacerun.co/docs/platform/reference/roles/assignAdminRole

- **Operation ID:** `assignAdminRole`
- **Tags:** Roles
- **Base URL:** `http://localhost:8787`

Calls `rbac.grant_role` (permission and priority checks plus audit inside). Staff cannot change their own roles: targeting yourself returns 403 `forbidden` with `details.perm = roles.assign`. Publishes `roles.assigned`.

**Workflow:** [Integration guide](https://docs.spacerun.co/docs/platform/guides/staff-permissions).

**Authentication:** staff Bearer JWT with permission `roles.assign`.

## Authentication

- **Auth:** staff Bearer JWT (`Authorization: Bearer <access_token>`) with the permissions below.
- **Required permissions:** `roles.assign`

## Request body

Required: yes.

Content type: `application/json`

Schema: `object`

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `user_id` | `string<uuid>` | yes | - |
| `role_key` | `string` | yes | Min length: 1. Max length: 48 |
| `expires_at` | `string<date-time>` | no | - |
| `reason` | `string` | no | Max length: 300 |

## Responses

### 204

Granted.

- Header `x-request-id`: Request identifier; quote it when reporting an error.
No response body.

## Errors

### 400

Request validation failed.

- Header `x-request-id`: Request identifier; quote it when reporting an error.
Content type: `application/json`

Schema: `object (ValidationErrorBody)`

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `success` | `boolean` | yes | One of: `false` |
| `error` | `object` | yes | - |
| `error.name` | `string` | yes | One of: `"ZodError"` |
| `error.issues` | `object[]` | yes | - |
| `error.issues[].code` | `string` | yes | - |
| `error.issues[].path` | `(string \| number)[]` | yes | - |
| `error.issues[].message` | `string` | yes | - |

**Request validation**

```json
{
  "success": false,
  "error": {
    "name": "ZodError",
    "issues": [
      {
        "code": "custom",
        "path": [
          "field"
        ],
        "message": "Invalid value"
      }
    ]
  }
}
```

### 401

Missing, malformed, expired or wrong-audience Bearer token (`missing_token`, `invalid_token`, `invalid_audience`).

- Header `x-request-id`: Request identifier; quote it when reporting an error.
Content type: `application/json`

Schema: `object (ErrorBody)`

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `error` | `string` | yes | Stable machine-readable error code, e.g. `forbidden`, `not_found`. |
| `details` | `any` | no | Optional structured context. For 403 it is `{ perm }`. |

**Error response**

```json
{
  "error": "missing_token"
}
```

### 403

Authenticated but lacking the required permission. `details.perm` lists the accepted permission keys.

- Header `x-request-id`: Request identifier; quote it when reporting an error.
Content type: `application/json`

Schema: `object (ErrorBody)`, fields as described above.

**Error response**

```json
{
  "error": "forbidden",
  "details": {
    "perm": "roles.assign"
  }
}
```

### 404

`not_found`: role or user does not exist.

- Header `x-request-id`: Request identifier; quote it when reporting an error.
Content type: `application/json`

Schema: `object (ErrorBody)`, fields as described above.

**Error response**

```json
{
  "error": "not_found"
}
```

### 409

`conflict`: unique violation.

- Header `x-request-id`: Request identifier; quote it when reporting an error.
Content type: `application/json`

Schema: `object (ErrorBody)`, fields as described above.

**Error response**

```json
{
  "error": "conflict"
}
```

### 422

`invalid_request`: rejected by the database function (invalid value or protected/system role).

- Header `x-request-id`: Request identifier; quote it when reporting an error.
Content type: `application/json`

Schema: `object (ErrorBody)`, fields as described above.

**Error response**

```json
{
  "error": "invalid_request"
}
```

### 500

Unexpected server error (`internal`). No details are returned.

- Header `x-request-id`: Request identifier; quote it when reporting an error.
Content type: `application/json`

Schema: `object (ErrorBody)`, fields as described above.

**Error response**

```json
{
  "error": "internal"
}
```

## Examples

### curl

```bash
curl -X POST 'http://localhost:8787/v1/admin/roles/assign' \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -H 'Content-Type: application/json' \
  --data '{"user_id":"3f6c2a1e-8b4d-4e7a-9c1f-2d5b6a7e8f90","role_key":"example","expires_at":"2026-01-15T12:00:00.000Z","reason":"example"}'
```

### TypeScript

```ts
const accessToken = process.env.ACCESS_TOKEN; // user JWT from your sign-in flow

const response = await fetch("http://localhost:8787/v1/admin/roles/assign", {
  method: "POST",
  headers: {
    "Authorization": `Bearer ${accessToken}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "user_id": "3f6c2a1e-8b4d-4e7a-9c1f-2d5b6a7e8f90",
    "role_key": "example",
    "expires_at": "2026-01-15T12:00:00.000Z",
    "reason": "example"
  }),
});
if (!response.ok) throw new Error(`HTTP ${response.status}: ${await response.text()}`);
const data = response.status === 204 || response.status === 205
  ? undefined
  : response.headers.get("content-type")?.includes("json")
    ? await response.json()
    : await response.text();
```
