Light Logo
JavaScript SDK (v0.39.1)

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_TOKEN environment variable if present; otherwise requests are unauthenticated
  • WRIFTAI_API_BASE_URL environment variable if present; otherwise the default API base url is used
  • globalThis.fetch by default

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.

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>',
  },
});