The API Keys module allows you to create, manage, and track API keys for your users.

List API Keys

Get all API keys for your account:
import { Limitly } from '@limitly/limitly-js';

const limitly = new Limitly({
  apiKey: 'your_limitly_api_key'
});

// List all API keys
const keys = await limitly.apiKeys.list();
console.log('API Keys:', keys.data);

Create API Key

Create a new API key for a user:
const newKey = await limitly.apiKeys.create({
  name: 'New API Key',
  user_id: 'user_123',
  plan_id: 'plan_456'
});

console.log('New API Key:', newKey.data?.api_key);

Create API Key Parameters

name
string
required
A descriptive name for the API key
user_id
string
required
The ID of the user who will use this API key
plan_id
string
The ID of the plan to associate with this API key
metadata
object
Additional metadata for the API key

Get API Key

Retrieve a specific API key by ID:
const key = await limitly.apiKeys.get('key_123');
console.log('API Key:', key.data);

Update API Key

Update an existing API key:
const updatedKey = await limitly.apiKeys.update('key_123', {
  name: 'Updated API Key Name',
  metadata: {
    environment: 'production'
  }
});

console.log('Updated API Key:', updatedKey.data);

Update API Key Parameters

name
string
New name for the API key
metadata
object
Updated metadata for the API key

Delete API Key

Delete an API key (soft delete):
const result = await limitly.apiKeys.delete('key_123');
console.log('Delete result:', result.data?.message);

Regenerate API Key

Regenerate an existing API key (creates a new key value):
const regeneratedKey = await limitly.apiKeys.regenerate('key_123');
console.log('New API Key:', regeneratedKey.data?.api_key);

Get API Key Usage

Get usage statistics for a specific API key:
const usage = await limitly.apiKeys.getUsage('key_123');
console.log('Usage:', usage.data);

Usage Response

current_usage
number
Current usage count for the period
limit
number
The limit for the current period
remaining
number
Remaining requests for the period
reset_time
string
When the usage will reset (ISO 8601 format)
period
string
The current period (day, week, month)

Get API Key Requests

Get detailed request history for an API key:
const requests = await limitly.apiKeys.getRequests('key_123');
console.log('Request history:', requests.data);

Request History Response

requests
array
Array of request objects
requests[].timestamp
string
When the request was made (ISO 8601 format)
requests[].endpoint
string
The endpoint that was called
requests[].method
string
The HTTP method used
requests[].status
string
The validation status (allowed/denied)
requests[].ip_address
string
The IP address of the request

Complete Example

import { Limitly } from '@limitly/limitly-js';

const limitly = new Limitly({
  apiKey: 'your_limitly_api_key'
});

async function manageApiKeys() {
  // Create a new API key
  const newKey = await limitly.apiKeys.create({
    name: 'Production API Key',
    user_id: 'user_123',
    plan_id: 'plan_456'
  });
  
  console.log('Created API Key:', newKey.data?.api_key);
  
  // Get usage statistics
  const usage = await limitly.apiKeys.getUsage(newKey.data?.id!);
  console.log('Current usage:', usage.data?.current_usage);
  console.log('Limit:', usage.data?.limit);
  console.log('Remaining:', usage.data?.remaining);
  
  // Get request history
  const requests = await limitly.apiKeys.getRequests(newKey.data?.id!);
  console.log('Recent requests:', requests.data?.requests);
  
  // Update the API key
  await limitly.apiKeys.update(newKey.data?.id!, {
    name: 'Updated Production API Key',
    metadata: {
      environment: 'production',
      team: 'backend'
    }
  });
}

API Key Object

The API key object contains the following properties:
id
string
Unique identifier for the API key
api_key
string
The actual API key value (only shown on creation/regeneration)
name
string
The name of the API key
user_id
string
The ID of the user who owns this API key
plan_id
string
The ID of the plan associated with this API key
created_at
string
When the API key was created (ISO 8601 format)
updated_at
string
When the API key was last updated (ISO 8601 format)
metadata
object
Additional metadata for the API key

Next Steps