> ## 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.

# Next.js SDK Users

> Manage user accounts using the Limitly Next.js SDK

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

## Creating Users

Create new user accounts:

```typescript theme={null}
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:

```typescript theme={null}
// Get all users
const users = await limitly.users.list();

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

## Getting User Details

Get detailed information about a specific user:

```typescript theme={null}
// 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:

```typescript theme={null}
// 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:

```typescript theme={null}
// Delete a user
await limitly.users.delete(123);

console.log('User deleted successfully');
```

## User Usage

Get usage statistics for a user:

```typescript theme={null}
// 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:

```typescript theme={null}
// 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:

```typescript theme={null}
// 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:

```typescript theme={null}
// Activate a user
await limitly.users.update(123, {
  is_disabled: false
});

// Suspend a user
await limitly.users.update(123, {
  is_disabled: true
});
```

## User Search

Search for users by various criteria:

```typescript theme={null}
// 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:

```typescript theme={null}
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

* [API Key Management](/sdk/nextjs/api-keys) - Create API keys for users
* [Plans](/sdk/nextjs/plans) - Assign users to plans
* [Request Validation](/sdk/nextjs/validation) - Validate requests for users
