Light Logo
JavaScript SDK (v0.45.1)Reference

API

Client for WriftAI's API.

Class: API

Client for WriftAI's API.

Constructors

Constructor

new API(baseUrl, fetchImpl, maxRetries, accessToken?, headers?): API

Create an instance of the API client.

Parameters

baseUrl

string

Base URL for the API without trailing slashes.

fetchImpl

{(input, init?): Promise<Response>; (input, init?): Promise<Response>; }

Fetch implementation to use.

maxRetries

number

Maximum number of retries on retryable errors. Set to 0 to disable retries.

accessToken?

string

Optional bearer token used for authentication

headers?

Record<string, string>

Optional additional headers (overrides defaults).

Returns

API

Methods

request()

request<T>(path, options): Promise<T>

Sends an HTTP request with automatic retries on transient errors.

Type Parameters

T

T

The expected JSON response shape

Parameters

path

string

The path relative to baseUrl (e.g. /users)

options

Request options

body?

JsonValue

Optional JSON-serializable body

headers?

Record<string, string>

Optional extra request headers

method

"GET" | "POST" | "PUT" | "PATCH" | "DELETE"

The HTTP method to use

params?

Record<string, undefined | string | number | boolean | string[]>

Optional query parameters. Entries with undefined values are silently skipped.

signal?

AbortSignal

Optional AbortSignal to cancel the request.

Returns

Promise<T>

The parsed JSON response

Throws

If the response is not ok (!res.ok)

Example

const users = await client.request<User[]>("/users", { method: "GET" });