Authentication
The SDK requires a NovelAI API key for authentication.
Getting Your API Key
- Log in to NovelAI
- Go to Account Settings
- Navigate to the API section
- Generate or copy your API key
Configuration Methods
Environment Variable (Recommended)
Set the NOVELAI_API_KEY environment variable:
bash
export NOVELAI_API_KEY=your_api_key_hereThen 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.