Skip to main content
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. Starting with the latest SDK versions, init() also automatically configures OpenTelemetry by default, eliminating the need for manual OpenTelemetry setup in most cases.

Basic usage

init.ts
Getting an API Key: Generate an API key by visiting https://gentrace.ai/s/api-keys

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
  5. Automatically configures OpenTelemetry (by default) with proper exporters and settings

Parameters

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

Function signatures

Parameters

options
ClientOptions
default:"{}"
Configuration options for initializing the Gentrace SDK client.

Advanced configuration

Automatic OpenTelemetry setup (default)

auto-otel.ts

Custom OpenTelemetry configuration

custom-otel.ts

Custom OTEL collector endpoint

If you’re using your own OpenTelemetry collector, configure the trace exporter in the otelSetup configuration:
custom-collector.ts
Common OTEL Collector configurations: - Local development: http://localhost:4318/v1/traces - Docker: http://otel-collector:4318/v1/traces - Kubernetes: http://otel-collector.observability.svc.cluster.local:4318/v1/traces
  • Cloud providers: Check your provider’s documentation for the correct endpoint

Disable automatic OpenTelemetry setup

manual-otel.ts

Custom base URL and timeout

custom-config.ts

Custom headers and logging

headers-logging.ts

Environment variables

Instead of passing configuration options directly to init(), consider using 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
.env
env-init.ts

Error handling

The init() function will throw an error if required configuration is missing:
error-handling.ts
Automatic OpenTelemetry Setup: Starting with the latest SDK versions, init() automatically configures OpenTelemetry by default. This eliminates the need for manual OpenTelemetry setup in most cases. If you need custom OpenTelemetry configuration or want to disable automatic setup, use the otelSetup (TypeScript) or otel_setup (Python) parameter.

Best practices

1. Initialize early

Call init() before your main execution logic to ensure Gentrace is configured before using any tracing functionality:
early-init.ts

2. Use environment variables

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

Requirements

  • API Key: A valid Gentrace API key is required. Generate one at https://gentrace.ai/s/api-keys
  • Network Access: The SDK needs to communicate with the Gentrace API
  • Environment: Node.js 20+ (TypeScript) or Python 3.8+ (Python)