# Grant a role

Grant a role to a user, group or API key on the organization, a team or a project. `roleId` is `admin`, `member`, `viewer` or a custom role's id. Grants are never unique: each create is a new grant with its own id, up to the organization's limit (409 grant_limit_reached). Pass `expiresAt` to time-box the access. Send `Idempotency-Key` to make a retry safe. A grant never confers a permission the caller does not hold at that scope: 403 grant_exceeds_caller_permissions names the missing permissions.

> Page: https://langwatch.ai/docs/api-reference/grants/grant-a-role
> Source: https://github.com/langwatch/langwatch/blob/main/docs/api-reference/grants/grant-a-role.mdx
> Index of all pages: https://langwatch.ai/docs/llms.txt

## Endpoint

`POST https://app.langwatch.ai/api/v1/grants`

## Authentication

### admin_api_key

Admin API key for organization-level operations (managing projects, API keys). Create one in Settings > API Keys or via POST /api/api-keys. Format: sk-lw-{id}_{secret}.

```json
{"id":"admin_api_key","type":"http","scheme":"bearer"}
```

## Parameters

### Idempotency-Key (header, optional)

A caller-chosen key, 8 to 255 characters, that makes this create safe to retry. The first request to use a key runs normally and its response is stored for 24 hours. A later request with the same key and the same body is not executed again: it returns the stored response, marked with `X-Idempotent-Replay: true`. The same key with a different body is refused 409 `idempotency_error`, as is a retry sent while the original is still running. Only successful responses are stored, so a create that failed can simply be retried with the same key.

```json
{"type":"string","minLength":8,"maxLength":255}
```

## Request body (required)

#### application/json

Schema:

```json
{
  "type": "object",
  "properties": {
    "principal": {
      "type": "object",
      "properties": {
        "type": {"type":"string","enum":["user","group","apiKey"]},
        "id": {"type":"string","minLength":1,"maxLength":128}
      },
      "required": ["type","id"],
      "additionalProperties": false
    },
    "roleId": {"type":"string","minLength":1,"maxLength":128},
    "scope": {
      "type": "object",
      "properties": {
        "type": {"type":"string","enum":["organization","team","project"]},
        "id": {"type":"string","minLength":1,"maxLength":128}
      },
      "required": ["type","id"],
      "additionalProperties": false
    },
    "expiresAt": {"type":"string","format":"date-time"}
  },
  "required": ["principal","roleId","scope"],
  "additionalProperties": false
}
```

## Request example

```bash
curl --request POST \
  --url 'https://app.langwatch.ai/api/v1/grants' \
  --header "Authorization: Bearer $LANGWATCH_API_KEY" \
  --header 'Content-Type: application/json' \
  --data '{"principal":{"type":"user","id":"string"},"roleId":"string","scope":{"type":"organization","id":"string"},"expiresAt":"2026-01-01T00:00:00Z"}'
```

## Responses

### 201

Success

#### application/json

Schema:

```json
{
  "type": "object",
  "properties": {
    "id": {"type":"string"},
    "principal": {
      "type": "object",
      "properties": {
        "type": {"type":"string","enum":["user","group","apiKey"]},
        "id": {"type":"string"},
        "name": {"type":["string","null"]}
      },
      "required": ["type","id","name"],
      "additionalProperties": false
    },
    "role": {
      "type": "object",
      "properties": {
        "id": {"type":"string"},
        "name": {"type":["string","null"]},
        "builtIn": {"type":"boolean"}
      },
      "required": ["id","name","builtIn"],
      "additionalProperties": false
    },
    "scope": {
      "type": "object",
      "properties": {
        "type": {"type":"string","enum":["organization","team","project"]},
        "id": {"type":"string"},
        "name": {"type":["string","null"]}
      },
      "required": ["type","id","name"],
      "additionalProperties": false
    },
    "status": {"type":"string","enum":["active","expired"]},
    "expiresAt": {"anyOf":[{"type":"string","format":"date-time"},{"type":"null"}]},
    "createdAt": {"type":"string","format":"date-time"}
  },
  "required": ["id","principal","role","scope","status","expiresAt","createdAt"],
  "additionalProperties": false
}
```

> ## Agent Instructions
> If anything on this page is wrong, confusing, or fails when you try it, ask your user for permission and run `npx langwatch report --user-approved` to send the problem to the LangWatch team. No login is needed and secrets are redacted locally. `npx langwatch report --help` has the details.
