Light Logo
Go SDK (v0.46.1)

Initializing

Guide on how to initialize the WriftAI Go client

This section demonstrates how to initialize the WriftAI Go 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 "github.com/wriftai/wriftai-go"

client := wriftai.NewClient()

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
  • http.DefaultClient for making HTTP requests
  • 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 "github.com/wriftai/wriftai-go"

client := wriftai.NewClient(
    wriftai.WithAPIBaseURL("https://api.wrift.ai/v1"),
)

Provide an Access Token Directly

import "github.com/wriftai/wriftai-go"

client := wriftai.NewClient(
    wriftai.WithAccessToken("your_access_token_here"),
)

Provide a Custom HTTP Client

import (
    "net/http"
    "time"

    "github.com/wriftai/wriftai-go"
)

httpClient := &http.Client{
    Timeout: 10 * time.Second,
}

client := wriftai.NewClient(
    wriftai.WithHTTPClient(httpClient),
)

This is useful if you want to configure timeouts, proxies, custom transports, or middleware.

Add or Override Request Headers

import "github.com/wriftai/wriftai-go"

client := wriftai.NewClient(
    wriftai.WithAPIHeaders(map[string]string{
        "X-App-Name":    "my-app",
        "X-Environment": "production",
        // You can override defaults too:
        // "Authorization": "Bearer <custom-token>",
    }),
)

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 "github.com/wriftai/wriftai-go"

// Increase retries
client := wriftai.NewClient(
    wriftai.WithMaxRetries(5),
)

// Disable retries entirely
client := wriftai.NewClient(
    wriftai.WithMaxRetries(0),
)

Combine Multiple Options

All options can be freely combined:

client := wriftai.NewClient(
    wriftai.WithAccessToken("your_access_token_here"),
    wriftai.WithAPIBaseURL("https://api.wrift.ai/v1"),
    wriftai.WithHTTPClient(&http.Client{}),
    wriftai.WithAPIHeaders(map[string]string{
        "X-App-Name": "my-app",
    }),
    wriftai.WithMaxRetries(5),
)