Avnology ID
SDKsGo SDK

Configuration

Advanced configuration options for the Go SDK including retry policies, custom HTTP clients, and context usage.

Configuration

The Go SDK uses the functional options pattern for configuration. All options are passed to NewClient().

Retry policy

client, err := avnologyid.NewClient(
    avnologyid.WithBaseURL("https://api.id.avnology.com"),
    avnologyid.WithAPIKey("ak_live_..."),
    avnologyid.WithRetries(5),
    avnologyid.WithRetryDelay(500 * time.Millisecond),
    avnologyid.WithRetryMaxDelay(30 * time.Second),
)

The SDK retries on transient failures (network errors, HTTP 429, 502, 503, 504) with exponential backoff and jitter. Rate limit responses are handled automatically using the Retry-After header.

Custom HTTP client

Provide your own *http.Client for custom TLS configuration, proxy settings, or instrumentation.

httpClient := &http.Client{
    Timeout: 15 * time.Second,
    Transport: &http.Transport{
        MaxIdleConns:        100,
        MaxIdleConnsPerHost: 10,
        IdleConnTimeout:     90 * time.Second,
    },
}

client, err := avnologyid.NewClient(
    avnologyid.WithBaseURL("https://api.id.avnology.com"),
    avnologyid.WithAPIKey("ak_live_..."),
    avnologyid.WithHTTPClient(httpClient),
)

Context and cancellation

Every method accepts context.Context as its first parameter for cancellation and deadline propagation.

// With timeout
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

users, err := client.Admin.ListUsers(ctx, &avnologyid.ListUsersParams{
    PageSize: 50,
})
if err != nil {
    if errors.Is(err, context.DeadlineExceeded) {
        log.Println("Request timed out")
    }
}

Request hooks

Add custom headers or logging to every request.

client, err := avnologyid.NewClient(
    avnologyid.WithBaseURL("https://api.id.avnology.com"),
    avnologyid.WithAPIKey("ak_live_..."),
    avnologyid.WithRequestHook(func(req *http.Request) {
        req.Header.Set("X-Request-Source", "my-service")
        req.Header.Set("X-Correlation-ID", uuid.NewString())
    }),
    avnologyid.WithResponseHook(func(resp *http.Response) {
        remaining := resp.Header.Get("RateLimit-Remaining")
        if remaining != "" {
            log.Printf("Rate limit remaining: %s", remaining)
        }
    }),
)

See also

On this page