Java API Library proxy configuration with custom HTTP client

Last updated: February 6, 2026

This issue is applicable to customers matching these conditions

Plans: Any

Deployments: Any

Use case: Java API Library with custom

Use case

In order to add a proxy configuration using a custom OkHttp client with the Java API Library.

This is in reference to the Braintrust Java API Library and not the Braintrust Java SDK.

Configuration Steps

Step 1: Replace dependency

Replace braintrust-java with braintrust-java-core in your project dependencies.

<dependency>
  <groupId>com.braintrustdata.api</groupId>
  <artifactId>braintrust-java-core</artifactId>
  <version>VERSION</version>
</dependency>

Step 2: Customize OkHttpClient

Copy the OkHttpClient class from braintrust-java-client-okhttp and customize it as needed. Here we add a proxyAuthenticator to the Builder.

class Builder internal constructor() {
    private var baseUrl: HttpUrl? = null
    private var timeout: Timeout = Timeout.default()
    private var proxy: Proxy? = null
    private var proxyAuthenticator: okhttp3.Authenticator? = null

    fun baseUrl(baseUrl: String) = apply { this.baseUrl = baseUrl.toHttpUrl() }

    fun timeout(timeout: Timeout) = apply { this.timeout = timeout }

    fun proxy(proxy: Proxy?) = apply { this.proxy = proxy }

    fun proxyAuthenticator(authenticator: okhttp3.Authenticator?) = apply {
        this.proxyAuthenticator = authenticator
    }

    fun build(): OkHttpClient =
        OkHttpClient(
            okhttp3.OkHttpClient.Builder()
                .connectTimeout(timeout.connect())
                .readTimeout(timeout.read())
                .writeTimeout(timeout.write())
                .callTimeout(timeout.request())
                .proxy(proxy)
                .proxyAuthenticator(proxyAuthenticator ?: okhttp3.Authenticator.NONE)
                .build(),
            checkRequired("baseUrl", baseUrl),
        )
}

Step 3: Configure client with proxy authentication

Construct BraintrustClientImpl with your customized HTTP client, setting both proxy and authenticator. See how we leverage the proxyAuthenticator we added in the last step.

BraintrustClient client =
    BraintrustOkHttpClient.builder()
        .baseUrl(apiUrl)
        .apiKey(apiKey)
        .proxy(
            new Proxy(
                Proxy.Type.HTTP,
                new InetSocketAddress(proxyHost, proxyPort)))
        .proxyAuthenticator(new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(
                    proxyUsername, proxyPassword.toCharArray());
            }
        })
        .build();