Skip to main content

Customized client usage

You might want to customize how the API client works, by providing additional information to your request, adding user-agents or use your own HTTP requester.

Send additional information in your request with requestOptions

The requestOptions parameter allows you to merge additional information with the client transporter, such as headers or query parameters.

Note: requestOptions overrides the default parameters that were previously set in the method and client.

java
import com.algolia.model.search.*;
import com.algolia.utils.RequestOptions;
client.search(
new SearchMethodParams()
.addRequests(SearchQuery.ofSearchForHits(
new SearchForHits()
.setIndexName("<YOUR_INDEX_NAME>")
.setQuery("<YOUR_QUERY>")
.setHitsPerPage(50)
)
),
new RequestOptions()
// This header is added to the request
.addExtraHeader("additional-header", "hello")
// As we re-define `hitsPerPage`, it will override the value defined in the method's parameters.
.addExtraQueryParameters("hitsPerPage", 100)
);
java
import com.algolia.model.search.*;
import com.algolia.utils.RequestOptions;
client.search(
new SearchMethodParams()
.addRequests(SearchQuery.ofSearchForHits(
new SearchForHits()
.setIndexName("<YOUR_INDEX_NAME>")
.setQuery("<YOUR_QUERY>")
.setHitsPerPage(50)
)
),
new RequestOptions()
// This header is added to the request
.addExtraHeader("additional-header", "hello")
// As we re-define `hitsPerPage`, it will override the value defined in the method's parameters.
.addExtraQueryParameters("hitsPerPage", 100)
);

Provide your own user-agent

The API clients offers a method for you to append data to the current user-agent.

java
import com.algolia.utils.ClientOptions;
import com.algolia.api.SearchClient;
// This will append `; my user agent (optional version)` to the current value of `x-algolia-agent` for every requests
SearchClient client = new SearchClient(
"<YOUR_APP_ID>",
"<YOUR_API_KEY>",
ClientOptions.build().addAlgoliaAgentSegments("my user agent", "optional version")
);
java
import com.algolia.utils.ClientOptions;
import com.algolia.api.SearchClient;
// This will append `; my user agent (optional version)` to the current value of `x-algolia-agent` for every requests
SearchClient client = new SearchClient(
"<YOUR_APP_ID>",
"<YOUR_API_KEY>",
ClientOptions.build().addAlgoliaAgentSegments("my user agent", "optional version")
);

Use your own HTTP requester

You can override the default requester behavior by providing your requester logic at the initialization of the client.

Create your own requester by creating a class that implements com.algolia.utils.Requester, and use it by passing it to the client constructor.

java
import com.algolia.api.SearchClient;
SearchClient client = new SearchClient(
"<YOUR_APP_ID>",
"<YOUR_API_KEY>",
ClientOptions.build().setRequester(new MyOwnRequester())
);
java
import com.algolia.api.SearchClient;
SearchClient client = new SearchClient(
"<YOUR_APP_ID>",
"<YOUR_API_KEY>",
ClientOptions.build().setRequester(new MyOwnRequester())
);