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

# Authentication

> Set up authentication for the Legnext JavaScript SDK

## Get Your API Key

1. Visit [Legnext Dashboard](https://dashboard.legnext.ai)
2. Log in to your account
3. Navigate to **API Keys** section
4. Create a new API key
5. Copy and save it securely

## Using Your API Key

### Method 1: Direct Initialization

```javascript theme={null}
const { Configuration, ImageApi } = require('@legnext-api/js-sdk');

const config = new Configuration({
    basePath: 'https://api.legnext.ai'
});

const imageApi = new ImageApi(config);

const apiKey = 'your-api-key-here';
// Use apiKey as first parameter in method calls
```

**TypeScript:**

```typescript theme={null}
import { Configuration, ImageApi } from '@legnext-api/js-sdk';

const config = new Configuration({
    basePath: 'https://api.legnext.ai'
});

const imageApi = new ImageApi(config);

const apiKey: string = 'your-api-key-here';
// Use apiKey as first parameter in method calls
```

### Method 2: Environment Variable (Recommended)

Set the environment variable:

```bash theme={null}
# macOS/Linux
export LEGNEXT_API_KEY="your-api-key-here"

# Windows CMD
set LEGNEXT_API_KEY=your-api-key-here

# Windows PowerShell
$env:LEGNEXT_API_KEY="your-api-key-here"
```

Then in your code:

```javascript theme={null}
const apiKey = process.env.LEGNEXT_API_KEY;

const imageApi = new ImageApi(apiClient);
// Use apiKey in method calls
```

### Method 3: .env File (Node.js)

Create a `.env` file:

```env theme={null}
LEGNEXT_API_KEY=your-api-key-here
LEGNEXT_BASE_URL=https://api.legnext.ai
```

Install and use dotenv:

```bash theme={null}
npm install dotenv
```

```javascript theme={null}
require('dotenv').config();

const { Configuration, ImageApi } = require('@legnext-api/js-sdk');

const config = new Configuration({
    basePath: process.env.LEGNEXT_BASE_URL || 'https://api.legnext.ai'
});

const imageApi = new ImageApi(config);
const apiKey = process.env.LEGNEXT_API_KEY;
```

### Method 4: ES Modules

```javascript theme={null}
import 'dotenv/config';
import { Configuration, ImageApi } from '@legnext-api/js-sdk';

const config = new Configuration({
    basePath: process.env.LEGNEXT_BASE_URL || 'https://api.legnext.ai'
});

const imageApi = new ImageApi(config);
const apiKey = process.env.LEGNEXT_API_KEY;
```

## Security Best Practices

⚠️ **Never hardcode API keys in your code**

✅ **Always use environment variables**

✅ **Add `.env` to `.gitignore`**

✅ **Never expose API keys in client-side JavaScript**

✅ **Rotate keys regularly and revoke compromised ones**

***

## Next Steps

* [Quick Start](/sdk/javascript/quickstart)
* [Image Generation API](/sdk/javascript/image-generation)
* [Video Generation API](/sdk/javascript/video-generation)
