Skip to content

Authentication

The SDK requires a NovelAI API key for authentication.

Getting Your API Key

  1. Log in to NovelAI
  2. Go to Account Settings
  3. Navigate to the API section
  4. Generate or copy your API key

Configuration Methods

Set the NOVELAI_API_KEY environment variable:

bash
export NOVELAI_API_KEY=your_api_key_here

Then initialize the client without parameters:

typescript
import { NovelAI } from 'novelai-sdk-unofficial';

const client = new NovelAI();

Direct Initialization

Pass the API key directly to the constructor:

typescript
import { NovelAI } from 'novelai-sdk-unofficial';

const client = new NovelAI({ apiKey: 'your-api-key' });

Using .env File

With a package like dotenv:

typescript
import 'dotenv/config';
import { NovelAI } from 'novelai-sdk-unofficial';

const client = new NovelAI();

Client Options

typescript
const client = new NovelAI({
  apiKey: 'your-api-key',      // API key
  imageBase: 'https://...',    // Custom image API base URL
  textBase: 'https://...',     // Custom text API base URL
  timeout: 30000,              // Request timeout in ms (default: 30000)
});

Error Handling

If no API key is provided, the SDK throws MissingAPIKeyError:

typescript
import { NovelAI, MissingAPIKeyError } from 'novelai-sdk-unofficial';

try {
  const client = new NovelAI();
} catch (error) {
  if (error instanceof MissingAPIKeyError) {
    console.error('Please provide an API key');
  }
}

Resource Cleanup

Client instances can be reused - no need to create a new instance for each request.

Released under the MIT License.