Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunityDoc feedbackLog In
GitHubNuGetDev CommunityDoc feedback

Initialize the Go SDK

This topic describes how to initialize the Optimizely Feature Experimentation Go SDK in your application.

To initialize the OptimizelyClient that exposes API methods like Decide, you need either the SDK key or a hard-coded JSON datafile.

Basic initialization with SDK Key

import optly "github.com/optimizely/go-sdk"

// Instantiates a client that syncs the datafile in the background
optlyClient, err := optly.Client("SDK_KEY_HERE")
if err != nil{
	// handler error
}

Advanced configuration

Static client instance

If you do not want the client to periodically sync the datafile in the background, you can instantiate a static client. This is useful for using the SDK in short-lived environments, such as Lambda functions. You can use the OptimizelyFactory for these more advanced use cases:

import "github.com/optimizely/go-sdk/pkg/client"

optimizelyFactory := &client.OptimizelyFactory{
          Datafile: []byte("DATAFILE_JSON_STRING_HERE"),
}

// Instantiate a static client (no datafile polling)
staticOptlyClient, err := optimizelyFactory.StaticClient()
if err != nil {
	// handle error
}

If you want the SDK to initialize and perform just a one-time remote datafile fetch you can pass in the SDK key instead and instantiate a static client:

import "github.com/optimizely/go-sdk/pkg/client"

optimizelyFactory := &client.OptimizelyFactory{
          SDKKey: "[SDK_KEY_HERE]",
}

// Instantiate a static client that pulls down the datafile one time
staticOptlyClient, err := optimizelyFactory.StaticClient()

Custom initialization

You can further customize the top-level components of the SDKs:

  • Event Processor
  • Config Manager
  • Default Decide Options
  • Decision Service using the OptimizelyFactory
import (
  "time"
 
  "github.com/optimizely/go-sdk/pkg/client"
)

optimizelyFactory := &client.OptimizelyFactory{
		SDKKey: "[SDK_KEY_HERE]",
	}

datafilePollingInterval := 2 * time.Second
eventBatchSize := 20
eventQueueSize := 1500
eventFlushInterval := 10 * time.Second
defaultDecideOptions := decide.OptimizelyDecideOptions{
  IgnoreUserProfileService: true,
}

// Instantiate a client with custom configuration
optimizelyClient, err := optimizelyFactory.Client(
  client.WithPollingConfigManager(datafilePollingInterval, nil),
  client.WithBatchEventProcessor(
    eventBatchSize,
    eventQueueSize,
    eventFlushInterval,
  ),
  client.WithDefaultDecideOptions(defaultDecideOptions),
)
if err != nil {
	// handle error
}

For more details on configuring the event batching see Event Batching. For more details on configuring default decide options, see Decide methods.

Use authenticated datafile in a secure environment

You can fetch the Optimizely Feature Experimentation datafile from an authenticated endpoint using a server-side (only) Optimizely Feature Experimentation SDK, like the Go SDK.

To use an authenticated datafile, download your environment's access token from the Optimizely app at Settings>Environments. Select your secure environment, and copy the Datafile access token. The example below shows how to initialize the Optimizely client using an access token and sdk_key, enabling the client to fetch the authenticated datafile and complete initialization.

// fetch the datafile from an authenticated endpoint
accessToken := `<YOUR_DATAFILE_ACCESS_TOKEN>`
sdkKey := `<YOUR_SDK_KEY>`
factory := client.OptimizelyFactory{SDKKey: sdkKey}
optimizelyClient, err := factory.Client(client.WithDatafileAccessToken(accessToken))