Light Logo
Go SDK (v0.41.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

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>",
    }),
)

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",
    }),
)