> ## Documentation Index
> Fetch the complete documentation index at: https://docs.limitly.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# API Key Management

> Manage API keys for your users via the REST API

Manage API keys for your users using the Limitly REST API.

## List API Keys

Get all API keys for your account:

```bash theme={null}
curl -X GET \
  -H "Authorization: Bearer lk_1234567890abcdef1234567890abcdef" \
  https://api.limitly.dev/functions/v1/keys
```

### Response

```json theme={null}
{
  "data": [
    {
      "id": "key_123",
      "name": "Production API Key",
      "user_id": 456,
      "plan_id": "plan_789",
      "status": "active",
      "created_at": "2024-01-01T00:00:00Z",
      "updated_at": "2024-01-01T00:00:00Z"
    }
  ],
  "count": 1
}
```

## Create API Key

Create a new API key for a user:

```bash theme={null}
curl -X POST \
  -H "Authorization: Bearer lk_1234567890abcdef1234567890abcdef" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "New API Key",
    "user_id": 123,
    "plan_id": "plan_456",
    "status": "active"
  }' \
  https://api.limitly.dev/functions/v1/keys
```

### Request Parameters

<ParamField body="name" type="string" required>
  A descriptive name for the API key
</ParamField>

<ParamField body="user_id" type="number" required>
  The ID of the user who will use this API key
</ParamField>

<ParamField body="plan_id" type="string">
  The ID of the plan to associate with this API key
</ParamField>

<ParamField body="status" type="string">
  The status of the API key (active, inactive)
</ParamField>

### Response

```json theme={null}
{
  "data": {
    "id": "key_123",
    "api_key": "lk_user_abcdef1234567890abcdef1234567890",
    "name": "New API Key",
    "user_id": 123,
    "plan_id": "plan_456",
    "status": "active",
    "created_at": "2024-01-01T00:00:00Z",
    "updated_at": "2024-01-01T00:00:00Z"
  }
}
```

## Get API Key

Retrieve a specific API key by ID:

```bash theme={null}
curl -X GET \
  -H "Authorization: Bearer lk_1234567890abcdef1234567890abcdef" \
  https://api.limitly.dev/functions/v1/keys/key_123
```

### Response

```json theme={null}
{
  "data": {
    "id": "key_123",
    "name": "Production API Key",
    "user_id": 456,
    "plan_id": "plan_789",
    "status": "active",
    "created_at": "2024-01-01T00:00:00Z",
    "updated_at": "2024-01-01T00:00:00Z"
  }
}
```

## Update API Key

Update an existing API key:

```bash theme={null}
curl -X PUT \
  -H "Authorization: Bearer lk_1234567890abcdef1234567890abcdef" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Updated API Key Name",
    "status": "inactive"
  }' \
  https://api.limitly.dev/functions/v1/keys/key_123
```

### Request Parameters

<ParamField body="name" type="string">
  New name for the API key
</ParamField>

<ParamField body="status" type="string">
  New status for the API key (active, inactive)
</ParamField>

## Delete API Key

Delete an API key:

```bash theme={null}
curl -X DELETE \
  -H "Authorization: Bearer lk_1234567890abcdef1234567890abcdef" \
  https://api.limitly.dev/functions/v1/keys/key_123
```

### Response

```json theme={null}
{
  "data": {
    "message": "API key deleted successfully"
  }
}
```

## Regenerate API Key

Regenerate an existing API key (creates a new key value):

```bash theme={null}
curl -X POST \
  -H "Authorization: Bearer lk_1234567890abcdef1234567890abcdef" \
  https://api.limitly.dev/functions/v1/keys/key_123/regenerate
```

### Response

```json theme={null}
{
  "data": {
    "id": "key_123",
    "api_key": "lk_user_newabcdef1234567890abcdef1234567890",
    "name": "Production API Key",
    "user_id": 456,
    "plan_id": "plan_789",
    "status": "active",
    "created_at": "2024-01-01T00:00:00Z",
    "updated_at": "2024-01-01T00:00:00Z"
  }
}
```

## Get API Key Usage

Get usage statistics for a specific API key:

```bash theme={null}
curl -X GET \
  -H "Authorization: Bearer lk_1234567890abcdef1234567890abcdef" \
  https://api.limitly.dev/functions/v1/keys/key_123/usage
```

### Response

```json theme={null}
{
  "data": {
    "current_usage": 45,
    "limit": 1000,
    "remaining": 955,
    "reset_time": "2024-02-01T00:00:00Z",
    "period": "month"
  }
}
```

## Get API Key Requests

Get detailed request history for an API key:

```bash theme={null}
curl -X GET \
  -H "Authorization: Bearer lk_1234567890abcdef1234567890abcdef" \
  https://api.limitly.dev/functions/v1/keys/key_123/requests
```

### Response

```json theme={null}
{
  "data": {
    "requests": [
      {
        "timestamp": "2024-01-01T12:00:00Z",
        "endpoint": "/api/users",
        "method": "GET",
        "status": "allowed",
        "ip_address": "192.168.1.1"
      }
    ],
    "count": 1
  }
}
```

## Error Responses

### Invalid API Key

```json theme={null}
{
  "error": "Invalid API key",
  "code": "INVALID_API_KEY"
}
```

### API Key Not Found

```json theme={null}
{
  "error": "API key not found",
  "code": "API_KEY_NOT_FOUND"
}
```

### Validation Error

```json theme={null}
{
  "error": "Validation failed",
  "code": "VALIDATION_ERROR",
  "details": {
    "field": "name",
    "message": "Name is required"
  }
}
```

## Pagination

List endpoints support pagination with the following query parameters:

<ParamField body="page" type="number">
  Page number (default: 1)
</ParamField>

<ParamField body="limit" type="number">
  Number of items per page (default: 10, max: 100)
</ParamField>

### Example

```bash theme={null}
curl -X GET \
  -H "Authorization: Bearer lk_1234567890abcdef1234567890abcdef" \
  "https://api.limitly.dev/functions/v1/keys?page=2&limit=20"
```

## Next Steps

* [Plans](/rest-api/plans) - Create and manage usage plans
* [Users](/rest-api/users) - Manage user accounts and usage
* [Request Validation](/rest-api/validation) - Validate user requests
