Skip to main content
Version: 4.7.56

init()

The init() function initializes the Gentrace SDK with your API credentials and configuration options. This function must be called early in your application setup to configure the SDK globally before using any other Gentrace functionality.

Basic usage

typescript
import { init } from 'gentrace';
// Basic initialization with API key
init({
apiKey: 'your-gentrace-api-key'
});
// Or using environment variable
init({
apiKey: process.env.GENTRACE_API_KEY
});

Overview

The init() function:

  1. Configures authentication with your Gentrace API key
  2. Sets up global client instances for making API requests
  3. Establishes connection settings like base URL, timeouts, and retry behavior
  4. Enables SDK functionality by making other Gentrace functions available

Parameters

Stainless SDK Generation

The parameters and client options described in this section are automatically generated by the Stainless API SDK generation platform.

Function Signature

typescript
function init(options: ClientOptions = {}): void

ClientOptions

typescript
interface ClientOptions {
apiKey?: string;
baseURL?: string | null;
timeout?: number;
maxRetries?: number;
defaultHeaders?: HeadersLike;
defaultQuery?: Record<string, string | undefined>;
logLevel?: LogLevel;
logger?: Logger;
fetchOptions?: RequestInit;
fetch?: Fetch;
}

Parameters

  • apiKey (string, optional): Your Gentrace API key. If not provided, the SDK will attempt to use the GENTRACE_API_KEY environment variable
  • baseURL (string, optional): Override the default base URL for the API. Defaults to GENTRACE_BASE_URL environment variable or https://gentrace.ai/api. This is particularly useful for self-hosted Gentrace instances (e.g., http://self-hosted-gentrace.example.com/api)
  • timeout (number, optional): Maximum time in milliseconds to wait for a response from the server before timing out a single request
  • maxRetries (number, optional): Maximum number of times to retry a request in case of temporary failure. Default: 2
  • defaultHeaders (object, optional): Default headers to include with every request to the API
  • defaultQuery (object, optional): Default query parameters to include with every request to the API
  • logLevel (LogLevel, optional): Set the log level. Defaults to GENTRACE_LOG environment variable or 'warn'
  • logger (Logger, optional): Custom logger instance. Defaults to globalThis.console
  • fetchOptions (RequestInit, optional): Additional options to be passed to fetch calls
  • fetch (Fetch, optional): Custom fetch function implementation

Advanced configuration

Custom base URL and timeout

typescript
import { init } from 'gentrace';
init({
apiKey: process.env.GENTRACE_API_KEY,
baseURL: 'https://your-custom-domain.com/api',
timeout: 30000, // 30 seconds
maxRetries: 5
});

Custom headers and logging

typescript
import { init } from 'gentrace';
init({
apiKey: process.env.GENTRACE_API_KEY,
defaultHeaders: {
'X-Custom-Header': 'your-value',
'User-Agent': 'MyApp/1.0.0'
},
logLevel: 'debug',
logger: console // or your custom logger
});

Environment variables

Instead of passing configuration options directly to init(), you can use environment variables:

  • GENTRACE_API_KEY: Your Gentrace API key
  • GENTRACE_BASE_URL: Custom base URL for the API (useful for self-hosted instances)
  • GENTRACE_LOG (TypeScript only): Log level setting
bash
# Set environment variables
export GENTRACE_API_KEY="your-gentrace-api-key"
export GENTRACE_BASE_URL="https://your-custom-domain.com/api"
export GENTRACE_LOG="debug"
typescript
import { init } from 'gentrace';
// Initialize without parameters - uses environment variables
init();

Error handling

The init() function will throw an error if required configuration is missing:

typescript
import { init } from 'gentrace';
try {
init({
// Missing apiKey - will throw error if GENTRACE_API_KEY env var not set
});
} catch (error) {
console.error('Failed to initialize Gentrace:', error.message);
}

Best practices

1. Initialize early

Call init() as early as possible in your application, before using any other Gentrace functionality:

typescript
// At the top of your main application file
import { init } from 'gentrace';
// Initialize first
init({
apiKey: process.env.GENTRACE_API_KEY
});
// Then import and use other Gentrace functions
import { experiment, evalOnce } from 'gentrace';

2. Use environment variables

Store sensitive configuration like API keys in environment variables rather than hardcoding them:

bash
# .env file
GENTRACE_API_KEY=your-gentrace-api-key
GENTRACE_BASE_URL=https://gentrace.ai/api

Requirements

  • API Key: A valid Gentrace API key is required
  • Network Access: The SDK needs to communicate with the Gentrace API
  • Environment: Node.js 16+ (TypeScript) or Python 3.8+ (Python)

See also