# Create API key

Create a new API key. For service keys, pass keyType:"service". Optionally scope to specific projects via projectIds (ADMIN on each). Omit projectIds for full org access. Pass assignedToUserId to mint the key for another member, and permissionMode:"restricted" with a permissions list to grant exactly those permissions. Minting a service key or a key for another member requires organization admin rights, held by both the key making the request and its member. A key can grant at most what the key making the request holds. The plaintext token is returned once — store it securely.

> Page: https://langwatch.ai/docs/api-reference/api-keys/create-api-key
> Source: https://github.com/langwatch/langwatch/blob/main/docs/api-reference/api-keys/create-api-key.mdx
> Index of all pages: https://langwatch.ai/docs/llms.txt

## Endpoint

`POST https://app.langwatch.ai/api/v1/api-keys`

## 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"}
```

## Request body (required)

#### application/json

Schema:

```json
{
  "type": "object",
  "properties": {
    "keyType": {
      "default": "personal",
      "description": "A personal key acts as the user who created it and needs explicit bindings. A service key is not tied to a user.",
      "type": "string",
      "enum": ["personal","service"]
    },
    "name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 100,
      "description": "Human-readable name for this key"
    },
    "description": {"type":"string","maxLength":500},
    "expiresAt": {
      "description": "ISO 8601 timestamp after which the key stops working",
      "type": "string",
      "format": "date-time"
    },
    "assignedToUserId": {
      "description": "Organization admins only: the member who owns the key and whose access caps it. Defaults to the caller.",
      "type": "string",
      "minLength": 1
    },
    "permissionMode": {
      "default": "all",
      "type": "string",
      "enum": ["all","readonly","restricted"],
      "description": "'all' and 'readonly' take their meaning from the bindings alone; 'restricted' additionally requires an explicit permissions list."
    },
    "permissions": {
      "type": "array",
      "items": {"type":"string","pattern":"^[a-z][a-zA-Z0-9_-]*:[a-z][a-zA-Z0-9_-]*$"},
      "description": "Restricted mode only: the exact resource:action permissions the key's CUSTOM bindings grant."
    },
    "bindings": {
      "description": "What this key may do, and where. Required for a personal key.",
      "maxItems": 20,
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "role": {
            "type": "string",
            "enum": ["ADMIN","MEMBER","VIEWER","CUSTOM"],
            "description": "CUSTOM grants exactly the listed permissions and requires permissionMode 'restricted'."
          },
          "scopeType": {"type":"string","enum":["ORGANIZATION","TEAM","PROJECT"]},
          "scopeId": {"type":"string","minLength":1}
        },
        "required": ["role","scopeType","scopeId"]
      }
    },
    "projectIds": {
      "description": "Service keys only: restricts the key to these projects",
      "maxItems": 50,
      "type": "array",
      "items": {"type":"string","minLength":1}
    }
  },
  "required": ["name"]
}
```

## Request example

```bash
curl --request POST \
  --url 'https://app.langwatch.ai/api/v1/api-keys' \
  --header "Authorization: Bearer $LANGWATCH_API_KEY" \
  --header 'Content-Type: application/json' \
  --data '{"keyType":"personal","name":"string","description":"string","expiresAt":"2026-01-01T00:00:00Z","assignedToUserId":"string","permissionMode":"all","permissions":["string"],"bindings":[{"role":"ADMIN","scopeType":"ORGANIZATION","scopeId":"string"}],"projectIds":["string"]}'
```

## Responses

### 201

Success

#### application/json

Schema:

```json
{
  "type": "object",
  "properties": {
    "token": {"type":"string","minLength":1},
    "apiKey": {
      "type": "object",
      "properties": {
        "id": {"type":"string","minLength":1},
        "name": {"type":"string"},
        "createdAt": {"type":"string","format":"date-time"}
      },
      "required": ["id","name","createdAt"],
      "additionalProperties": false
    }
  },
  "required": ["token","apiKey"],
  "additionalProperties": false
}
```

### 401

Invalid or missing API key token

### 403

Requested binding exceeds what the key making the request holds, or the scope does not belong to this organization (api_key_scope_violation); a service key or a key for another member was requested without organization admin rights (api_key_admin_required)

### 422

Validation error, for example a missing name or empty bindings (validation_error), or a name LangWatch reserves for its own keys (api_key_reserved_name)

> ## 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.
