The Limitly Next.js SDK provides comprehensive user management capabilities for your application.

Creating Users

Create new user accounts:
import { Limitly } from '@limitly/limitly-nextjs';

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

// Create a new user
const newUser = await limitly.users.create({
  name: 'John Doe',
  email: 'user@example.com'
});

console.log('New user created:', newUser.data?.user_id);

Listing Users

Retrieve all users in your application:
// Get all users
const users = await limitly.users.list();

console.log('Users:', users.data);

Getting User Details

Get detailed information about a specific user:
// Get user details
const user = await limitly.users.get(123);

console.log('User details:', {
  user_id: user.data?.user_id,
  name: user.data?.name,
  email: user.data?.email,
  is_disabled: user.data?.is_disabled,
  created_at: user.data?.created_at
});

Updating Users

Update user properties:
// Update a user
const updatedUser = await limitly.users.update(123, {
  name: 'John Smith',
  email: 'john.smith@example.com'
});

console.log('Updated user:', updatedUser.data);

Deleting Users

Delete user accounts when they’re no longer needed:
// Delete a user
await limitly.users.delete(123);

console.log('User deleted successfully');

User Usage

Get usage statistics for a user:
// Get user usage
const usage = await limitly.users.getUsage(123);

console.log('User usage:', {
  type: usage.data?.type,
  current_usage: usage.data?.current_usage,
  limit: usage.data?.limit,
  percentage_used: usage.data?.percentage_used,
  user_name: usage.data?.user_name,
  plan_name: usage.data?.plan_name
});

User API Keys

Get all API keys associated with a user:
// Get user API keys
const apiKeys = await limitly.users.getApiKeys(123);

console.log('User API keys:', apiKeys.data);

API Route Example

Here’s a complete example of user management in a Next.js API route:
// app/api/users/route.ts
import { Limitly } from '@limitly/limitly-nextjs';

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

export async function POST(request: Request) {
  try {
    const body = await request.json();
    
    const newUser = await limitly.users.create({
      name: body.name,
      email: body.email
    });

    return Response.json({
      success: true,
      user: newUser.data
    });
  } catch (error) {
    return Response.json({
      success: false,
      error: error.message
    }, { status: 500 });
  }
}

export async function GET(request: Request) {
  try {
    const users = await limitly.users.list();

    return Response.json({
      success: true,
      users: users.data,
      count: users.count
    });
  } catch (error) {
    return Response.json({
      success: false,
      error: error.message
    }, { status: 500 });
  }
}

User Status Management

Manage user account status:
// Activate a user
await limitly.users.update(123, {
  is_disabled: false
});

// Suspend a user
await limitly.users.update(123, {
  is_disabled: true
});
Search for users by various criteria:
// Search users by email
const users = await limitly.users.list({
  email: 'user@example.com'
});

console.log('Users found:', users.data);

Error Handling

Handle user management errors:
import { Limitly } from '@limitly/limitly-nextjs';

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

try {
  const user = await limitly.users.create({
    name: 'John Doe',
    email: 'user@example.com'
  });
  
  console.log('User created:', user.data?.user_id);
} catch (error) {
  if (error.message.includes('Email already exists')) {
    console.error('User with this email already exists');
  } else if (error.message.includes('Invalid email')) {
    console.error('Invalid email format');
  } else {
    console.error('Unexpected error:', error.message);
  }
}

User Properties

The SDK supports the following user properties:
  • user_id: Unique identifier for the user
  • name: User’s display name
  • email: User’s email address (optional)
  • is_disabled: Whether the user account is disabled
  • created_at: Timestamp when the user was created
  • updated_at: Timestamp when the user was last updated
  • custom_start: Custom start date for usage tracking (optional)
  • plan: Associated plan information (optional)

Next Steps