Initializing
Guide on how to initialize the WriftAI Javascript client
This section demonstrates how to initialize the WriftAI Javascript client with practical examples.
Basic Initialization
If your environment variables are set (WRIFTAI_ACCESS_TOKEN and optionally WRIFTAI_API_BASE_URL),
you can initialize the client without passing any options.
import { WriftAI } from 'wriftai';
const wriftai = new WriftAI();This automatically uses:
WRIFTAI_ACCESS_TOKENenvironment variable if present; otherwise requests are unauthenticatedWRIFTAI_API_BASE_URLenvironment variable if present; otherwise the default API base url is usedglobalThis.fetchby default- Up to 2 automatic retries on transient errors (429 rate limits and 5xx server errors) with exponential backoff.
Custom Initialization
If you want full control or are not using environment variables, you can provide options manually.
Use a Specific API Base URL
import { WriftAI } from 'wriftai';
const wriftai = new WriftAI({
apiBaseUrl: 'https://api.wrift.ai/v1',
});Provide an Access Token Directly
import { WriftAI } from 'wriftai';
const wriftai = new WriftAI({
accessToken: 'your_access_token_here',
});Provide a fetch Implementation
import { WriftAI } from 'wriftai';
const wriftai = new WriftAI({
fetch: globalThis.fetch, // or your own fetch implementation
});For example, you might provide a wrapped fetch that logs requests, injects additional headers, or uses a timeout.
Configure Retries
By default the client retries up to 2 times on 429 (rate limit) and 5xx (server error) responses. For 429 responses the client waits until the server's x-ratelimit-reset time; for 5xx responses it uses exponential backoff starting at 0.5 seconds and capping at 8 seconds.
import { WriftAI } from 'wriftai';
// Increase retries
const wriftai = new WriftAI({
maxRetries: 5,
});
// Disable retries entirely
const wriftai = new WriftAI({
maxRetries: 0,
});Add or Override Request Headers
import { WriftAI } from 'wriftai';
const wriftai = new WriftAI({
headers: {
'X-App-Name': 'my-app',
'X-Environment': 'production',
// You can override defaults too:
// Authorization: 'Bearer <custom-token>',
},
});