The API client methods accept additional parameters for adding headers or query parameters, or for changing the default timeouts for individual requests.

To customize all requests, you can customize the client.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package org.example;

import com.algolia.api.SearchClient;
import com.algolia.config.RequestOptions;
import com.algolia.model.search.*;

import java.time.Duration;

public class Main {
    public static void main(String[] args) {
        var client = new SearchClient(
            "ALGOLIA_APPLICATION_ID",
            "ALGOLIA_API_KEY"
        );

        var searchParams = new SearchParamsObject().setQuery("SEARCH_QUERY");

        var requestOptions = new RequestOptions()
            // Add a custom HTTP header to this request
            .addExtraHeader("extra-header", "greetings")
            // Add query parameters to this request
            .addExtraQueryParameters("queryParam", "value")
            // Adjust timeout for this request
            .setReadTimeout(Duration.ofSeconds(100));

        client.searchSingleIndex(
            "ALGOLIA_INDEX_NAME",
            searchParams,
            Hit.class,
            requestOptions
        );
    }
}

Reference

headers
type: Map<String,String>

Additional headers as key-value pairs to send with this request. Use the RequestOptions.addExtraHeader method to set extra headers.

queryParameters
type: Map<String,String>

Additional query parameters to send with this request. They only take effect with API operations that support query parameters. Otherwise, they’re ignored. Use the RequestOptions.addExtraQueryParameters method to set extra query parameters.

readTimeout
type: Duration
default: 5

Maximum number of seconds to wait for a response from the server (for GET requests). Use the RequestOptions.setReadTimeout method to change this timeout.

writeTimeout
type: Duration
default: 30

Maximum number of seconds to wait for a response from the server (for requests other than GET). Use the RequestOptions.setWriteTimeout method to change this timeout.

Did you find this page helpful?
Java API clients v4