> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/BerriAI/litellm/llms.txt
> Use this file to discover all available pages before exploring further.

# Key Management

> API endpoints for managing API keys in the LiteLLM proxy

## Overview

The LiteLLM proxy provides comprehensive key management endpoints for creating, updating, listing, and deleting API keys with granular access control.

## Generate Key

### POST /key/generate

Create a new API key.

#### Request Body

<ParamField body="models" type="array">
  Models this key can access.

  ```json theme={null}
  {"models": ["gpt-4", "gpt-3.5-turbo"]}
  ```
</ParamField>

<ParamField body="duration" type="string">
  Key expiration duration.

  Examples: `"30d"`, `"1h"`, `"permanent"`
</ParamField>

<ParamField body="metadata" type="object">
  Custom metadata for the key.
</ParamField>

<ParamField body="max_budget" type="number">
  Maximum spending limit for this key in USD.
</ParamField>

<ParamField body="tpm_limit" type="integer">
  Tokens per minute limit.
</ParamField>

<ParamField body="rpm_limit" type="integer">
  Requests per minute limit.
</ParamField>

<ParamField body="team_id" type="string">
  Associate key with a team.
</ParamField>

<ParamField body="user_id" type="string">
  Associate key with a user.
</ParamField>

<ParamField body="aliases" type="object">
  Model aliases for this key.

  ```json theme={null}
  {
    "aliases": {
      "gpt-4": "my-custom-gpt-4-deployment"
    }
  }
  ```
</ParamField>

<ParamField body="config" type="object">
  Additional configuration.
</ParamField>

<ParamField body="permissions" type="object">
  Key permissions.
</ParamField>

#### Response

<ResponseField name="key" type="string">
  The generated API key.
</ResponseField>

<ResponseField name="key_name" type="string">
  Name/identifier for the key.
</ResponseField>

<ResponseField name="expires" type="string">
  Expiration timestamp.
</ResponseField>

<ResponseField name="user_id" type="string">
  Associated user ID.
</ResponseField>

<ResponseField name="team_id" type="string">
  Associated team ID.
</ResponseField>

#### Example

```bash theme={null}
curl -X POST http://localhost:4000/key/generate \
  -H "Authorization: Bearer sk-admin-xxx" \
  -H "Content-Type: application/json" \
  -d '{
    "models": ["gpt-4", "gpt-3.5-turbo"],
    "duration": "30d",
    "max_budget": 100.0,
    "tpm_limit": 100000,
    "rpm_limit": 1000,
    "metadata": {
      "team": "engineering",
      "environment": "production"
    }
  }'
```

```python theme={null}
import requests

response = requests.post(
    "http://localhost:4000/key/generate",
    headers={"Authorization": "Bearer sk-admin-xxx"},
    json={
        "models": ["gpt-4", "gpt-3.5-turbo"],
        "duration": "30d",
        "max_budget": 100.0,
        "tpm_limit": 100000,
        "rpm_limit": 1000
    }
)

key_data = response.json()
print(f"New key: {key_data['key']}")
```

***

## List Keys

### GET /key/list

List all API keys.

#### Query Parameters

<ParamField query="user_id" type="string">
  Filter by user ID.
</ParamField>

<ParamField query="team_id" type="string">
  Filter by team ID.
</ParamField>

#### Response

Returns array of key objects:

<ResponseField name="keys" type="array">
  Array of key objects.

  <Expandable title="key object">
    <ResponseField name="key" type="string">
      The API key (masked).
    </ResponseField>

    <ResponseField name="key_name" type="string">
      Key name/identifier.
    </ResponseField>

    <ResponseField name="models" type="array">
      Models this key can access.
    </ResponseField>

    <ResponseField name="spend" type="number">
      Total spend for this key.
    </ResponseField>

    <ResponseField name="max_budget" type="number">
      Budget limit.
    </ResponseField>

    <ResponseField name="expires" type="string">
      Expiration timestamp.
    </ResponseField>

    <ResponseField name="user_id" type="string">
      Associated user.
    </ResponseField>

    <ResponseField name="team_id" type="string">
      Associated team.
    </ResponseField>
  </Expandable>
</ResponseField>

#### Example

```bash theme={null}
curl -X GET 'http://localhost:4000/key/list' \
  -H "Authorization: Bearer sk-admin-xxx"
```

```python theme={null}
import requests

response = requests.get(
    "http://localhost:4000/key/list",
    headers={"Authorization": "Bearer sk-admin-xxx"}
)

keys = response.json()["keys"]
for key in keys:
    print(f"Key: {key['key_name']}, Spend: ${key['spend']:.2f}")
```

***

## Get Key Info

### GET /key/info

Get detailed information about a specific key.

#### Query Parameters

<ParamField query="key" type="string" required>
  The API key to query.
</ParamField>

#### Response

<ResponseField name="key" type="string">
  The API key (masked).
</ResponseField>

<ResponseField name="models" type="array">
  Models accessible.
</ResponseField>

<ResponseField name="spend" type="number">
  Total spend.
</ResponseField>

<ResponseField name="max_budget" type="number">
  Budget limit.
</ResponseField>

<ResponseField name="metadata" type="object">
  Custom metadata.
</ResponseField>

<ResponseField name="tpm_limit" type="integer">
  TPM limit.
</ResponseField>

<ResponseField name="rpm_limit" type="integer">
  RPM limit.
</ResponseField>

#### Example

```bash theme={null}
curl -X GET 'http://localhost:4000/key/info?key=sk-litellm-xxx' \
  -H "Authorization: Bearer sk-admin-xxx"
```

***

## Update Key

### POST /key/update

Update an existing API key.

#### Request Body

<ParamField body="key" type="string" required>
  The key to update.
</ParamField>

<ParamField body="models" type="array">
  Update accessible models.
</ParamField>

<ParamField body="max_budget" type="number">
  Update budget limit.
</ParamField>

<ParamField body="tpm_limit" type="integer">
  Update TPM limit.
</ParamField>

<ParamField body="rpm_limit" type="integer">
  Update RPM limit.
</ParamField>

<ParamField body="metadata" type="object">
  Update metadata.
</ParamField>

#### Example

```bash theme={null}
curl -X POST http://localhost:4000/key/update \
  -H "Authorization: Bearer sk-admin-xxx" \
  -H "Content-Type: application/json" \
  -d '{
    "key": "sk-litellm-xxx",
    "max_budget": 200.0,
    "tpm_limit": 200000
  }'
```

```python theme={null}
import requests

response = requests.post(
    "http://localhost:4000/key/update",
    headers={"Authorization": "Bearer sk-admin-xxx"},
    json={
        "key": "sk-litellm-xxx",
        "max_budget": 200.0,
        "models": ["gpt-4", "claude-2"]
    }
)
```

***

## Delete Key

### POST /key/delete

Delete an API key.

#### Request Body

<ParamField body="keys" type="array" required>
  Array of keys to delete.

  ```json theme={null}
  {"keys": ["sk-litellm-xxx", "sk-litellm-yyy"]}
  ```
</ParamField>

#### Example

```bash theme={null}
curl -X POST http://localhost:4000/key/delete \
  -H "Authorization: Bearer sk-admin-xxx" \
  -H "Content-Type: application/json" \
  -d '{"keys": ["sk-litellm-xxx"]}'
```

```python theme={null}
import requests

response = requests.post(
    "http://localhost:4000/key/delete",
    headers={"Authorization": "Bearer sk-admin-xxx"},
    json={"keys": ["sk-litellm-xxx"]}
)
```

***

## Regenerate Key

### POST /key/regenerate

Regenerate a new key value while keeping settings.

#### Request Body

<ParamField body="key" type="string" required>
  The key to regenerate.
</ParamField>

#### Response

<ResponseField name="key" type="string">
  The new API key.
</ResponseField>

#### Example

```bash theme={null}
curl -X POST http://localhost:4000/key/regenerate \
  -H "Authorization: Bearer sk-admin-xxx" \
  -H "Content-Type: application/json" \
  -d '{"key": "sk-litellm-xxx"}'
```

***

## Block/Unblock Key

### POST /key/block

Temporarily block a key.

```bash theme={null}
curl -X POST http://localhost:4000/key/block \
  -H "Authorization: Bearer sk-admin-xxx" \
  -H "Content-Type: application/json" \
  -d '{"key": "sk-litellm-xxx"}'
```

### POST /key/unblock

Unblock a previously blocked key.

```bash theme={null}
curl -X POST http://localhost:4000/key/unblock \
  -H "Authorization: Bearer sk-admin-xxx" \
  -H "Content-Type: application/json" \
  -d '{"key": "sk-litellm-xxx"}'
```

## Complete Example

```python theme={null}
import requests
import json

BASE_URL = "http://localhost:4000"
ADMIN_KEY = "sk-admin-xxx"

headers = {
    "Authorization": f"Bearer {ADMIN_KEY}",
    "Content-Type": "application/json"
}

# 1. Create a new key
key_response = requests.post(
    f"{BASE_URL}/key/generate",
    headers=headers,
    json={
        "models": ["gpt-4", "gpt-3.5-turbo"],
        "duration": "30d",
        "max_budget": 100.0,
        "tpm_limit": 100000,
        "metadata": {"team": "engineering"}
    }
)
new_key = key_response.json()["key"]
print(f"Created key: {new_key}")

# 2. Get key info
info_response = requests.get(
    f"{BASE_URL}/key/info",
    headers=headers,
    params={"key": new_key}
)
print(f"Key info: {json.dumps(info_response.json(), indent=2)}")

# 3. Update the key
update_response = requests.post(
    f"{BASE_URL}/key/update",
    headers=headers,
    json={
        "key": new_key,
        "max_budget": 150.0
    }
)
print("Key updated")

# 4. List all keys
list_response = requests.get(
    f"{BASE_URL}/key/list",
    headers=headers
)
keys = list_response.json()["keys"]
print(f"Total keys: {len(keys)}")

# 5. Delete the key
delete_response = requests.post(
    f"{BASE_URL}/key/delete",
    headers=headers,
    json={"keys": [new_key]}
)
print("Key deleted")
```

## Related

* [Team Management](/api/proxy/teams)
* [User Management](/api/proxy/users)
* [Usage Tracking](https://docs.litellm.ai/docs/proxy/users)
