Skip to main content

Configurable Http Clients

To make an API call, each SDK uses an HttpClient particular to that language or framework. Developers can provide their own configured instance of the HTTP client during client initialization. This gives them the ability to override default configurations used by our SDKs.

Inject A Custom Http Client

To configure the HTTP client, initialize the client instance with a custom HttpClient. This enables you to set up logging, retries, timeouts, or other advanced configurations tailored to your application's specific needs.

SDK Usage Example

Default headers can be useful for testing, custom authentication schemes, compliance, etc. For example, let's say we want to add an Authorization header for authorization and a Custom-Header for compliance or monitoring. Below are some examples that use a custom HTTP client to configure this before passing it into the SDK client.

The Java SDK allows you to provide your own okhttp3.OkHttpClient instances.

import com.mycompany.myapi.MyApiClient;
import com.mycompany.myapi.Environment;
import okhttp3.OkHttpClient;
import okhttp3.Request;

OkHttpClient httpClient = new OkHttpClient.Builder()
.addInterceptor(chain -> {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder()
.header("Authorization", "Bearer your-access-token")
.header("Custom-Header", "CustomValue");

Request request = requestBuilder.build();
return chain.proceed(request);
})
.build();

MyApiClient client = new MyApiClient.Builder()
.httpClientConfig(configBuilder -> configBuilder
.httpClientInstance(httpClient))
.environment(Environment.PRODUCTION)
.build();