The Limitly SDK uses API key authentication to interact with the Limitly API.

API Key Setup

To use the SDK, you need a Limitly API key. You can get one from your Limitly Dashboard.

Basic Authentication

Initialize the SDK with your API key:
import { Limitly } from '@limitly/limitly-js';

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

Environment Variables

For security, store your API key in environment variables:
# .env.local
LIMITLY_API_KEY=lk_1234567890abcdef
Then use it in your application:
import { Limitly } from '@limitly/limitly-js';

const limitly = new Limitly({
  apiKey: process.env.LIMITLY_API_KEY!
});

Express.js Example

Here’s how to use authentication in an Express.js application:
import express from 'express';
import { Limitly } from '@limitly/limitly-js';

const app = express();
const limitly = new Limitly({
  apiKey: process.env.LIMITLY_API_KEY!
});

app.use(async (req, res, next) => {
  const apiKey = req.headers['x-api-key'] as string;
  
  if (!apiKey) {
    return res.status(401).json({ error: 'API Key required' });
  }

  const result = await limitly.validation.validate(
    apiKey,
    req.path,
    req.method
  );

  if (!result.success) {
    return res.status(429).json({ error: 'Rate limit exceeded' });
  }

  next();
});

app.get('/api/users', (req, res) => {
  res.json({ message: 'Users retrieved successfully' });
});

Error Handling

The SDK provides comprehensive error handling:
import { Limitly } from '@limitly/limitly-js';

const limitly = new Limitly({
  apiKey: process.env.LIMITLY_API_KEY!
});

try {
  const result = await limitly.validation.validate(
    'user_api_key',
    '/api/users',
    'GET'
  );
  
  if (result.success) {
    console.log('Request allowed');
  } else {
    console.log('Request denied:', result.error);
  }
} catch (error) {
  console.error('Authentication error:', error);
}

Next Steps