It’s recommended to use the Kotlin API client, which is better suited for Android development.
When using search, indexing, and settings API client methods, you can include extra options with your request.
The requestOptions
parameter lets you specify a list of options to send along with the request, such as HTTP headers, timeout settings, and more.
You can use requestOptions
to pass extra HTTP headers to your request.
Here are some headers with different use cases:
- Setting
X-Algolia-UserToken
to use API key rate limits.
- Setting
X-Algolia-UserToken
for analytics purposes. The Analytics API uses the value of this header to distinguish between end users. It takes priority over any value in X-Forwarded-For
. You should use the X-Algolia-UserToken
header if you need to forward the end user’s identity without relying on IP addresses.
- Setting
X-Forwarded-For
for analytics purposes in back-end implementations. If your server sends the end user’s IP along with every search, analytics can distinguish between end users. Otherwise, the analytics uses the server’s IP address, and considers all your users as a single user.
- Setting
X-Forwarded-For
for geolocation purposes, when you perform searches from your back end. This ensures that the geolocation for a search uses the IP address of your end user, and not that of your back-end server. For an example of this, see the aroundLatLngViaIP
parameter.
1
2
3
4
5
| $index = $client->initIndex('indexName');
$res = $index->search('query string', [
'X-Algolia-UserToken' => 'user123'
]);
|
1
2
3
4
5
6
7
| index = client.init_index('indexName')
request_options = {
'X-Algolia-UserToken': 'user123'
}
res = index.search('query string', request_options)
|
1
2
3
4
5
6
7
8
9
10
| const index = client.initIndex('indexName');
const requestOptions = {
headers: { 'X-Algolia-UserToken': 'user123' }
}
index.search('query string', requestOptions)
.then(({ hits }) => {
console.log(hits);
});
|
1
2
3
4
5
6
7
| index = client.init_index('indexName')
request_options = {
'X-Algolia-UserToken': 'user123'
}
res = index.search('query string', request_options)
|
1
2
3
4
5
6
7
8
| let index = client.index(withName: "indexName")
var requestOptions = RequestOptions()
requestOptions.headers["X-Algolia-UserToken"] = "user123"
index.search(query: "query string", requestOptions: requestOptions) { result in
if case .success(let response) = result {
print("Response: \(response)")
}
}
|
1
2
3
4
5
6
7
8
9
10
| RequestOptions requestOptions = new RequestOptions()
.setHeader("X-Algolia-UserToken", "user123");
Index index = client.getIndex("indexName");
Query query = new Query("query string")
index.searchAsync(query, requestOptions, new CompletionHandler() {
@Override
public void requestCompleted(JSONObject content, AlgoliaException error) {
// [...]
}
});
|
1
2
3
4
5
6
7
8
| Index index = client.InitIndex("indexName");
RequestOptions requestOptions = new RequestOptions
{
Headers = new Dictionary<string,string>{ { "X-Algolia-UserToken", "user123" } }
};
var result = index.Search<Result>(new Query("query string"), requestOptions);
|
1
2
3
4
5
6
7
8
9
10
| SearchIndex<Result> index = client.initIndex("indexName", Result.class);
Query query = new Query("query string");
RequestOptions requestOptions =
new RequestOptions().addExtraHeader("X-Algolia-UserToken", "user123");
// Sync
SearchResult<Result> search = index.search(query, requestOptions);
// Async
CompletableFuture<SearchResult<Result>> search = index.searchAsync(query, requestOptions);
|
1
2
3
4
5
6
7
| opts := []interface{}{
opt.ExtraHeaders(map[string]string{
"X-Algolia-UserToken": "user123",
}),
}
res, err := index.Search("query string", opts...)
|
1
2
3
4
5
6
7
| client.execute {
search into "indexName" query Query(
query = Some("query string")
) options RequestOptions(
extraHeaders = Some(Map("X-Algolia-UserToken" -> "user123"))
)
}
|
1
2
3
4
5
6
7
8
| val indexName = IndexName("indexName")
val index = client.initIndex(indexName)
val query = Query("query string")
val requestOptions = requestOptions {
header("X-Algolia-User-ID", "user123")
}
index.search(query, requestOptions)
|
Override request timeouts
While you can configure timeouts when instantiating your client, you can override these or the client’s default timeouts with requestOptions
.
1
2
3
4
5
6
| $index = $client->initIndex('indexName');
$res = $index->search('query string', [
// Set the readTimeout to 20 seconds
'readTimeout' => 20
]);
|
1
2
3
4
5
6
7
8
| index = client.init_index('indexName')
request_options = {
# Set the read_timeout to 20 seconds
'read_timeout': 20
}
res = index.search('query string', request_options)
|
1
2
3
4
5
6
7
8
9
10
11
| const index = client.initIndex('indexName');
const requestOptions = {
// Set the readTimeout to 20 seconds
timeouts: { read: 20 }
}
index.search('query string', requestOptions)
.then(({ hits }) => {
console.log(hits);
});
|
1
2
3
4
5
6
7
8
| index = client.init_index('indexName')
request_options = {
# Set the readTimeout to 20 seconds
"readTimeout": 20,
}
res = index.search('query string', request_options)
|
1
2
3
4
5
6
7
8
9
| let index = client.index(withName: "indexName")
var requestOptions = RequestOptions()
// Set the readTimeout to 20 seconds
requestOptions.readTimeout = 20
index.search(query: "query string", requestOptions: requestOptions) { result in
if case .success(let response) = result {
print("Response: \(response)")
}
}
|
1
2
3
4
5
6
7
8
9
10
11
| RequestOptions requestOptions = new RequestOptions()
// Set the readTimeout to 20 seconds
.setReadTimeout(20000);
Index index = client.getIndex("indexName");
Query query = new Query("query string")
index.searchAsync(query, requestOptions, new CompletionHandler() {
@Override
public void requestCompleted(JSONObject content, AlgoliaException error) {
// [...]
}
});
|
1
2
3
4
5
6
7
8
9
| Index index = client.InitIndex("indexName");
RequestOptions requestOptions = new RequestOptions
{
// Set the readTimeout to 20 seconds
Timeout = 20
};
var result = index.Search<Result>(new Query("query string"), requestOptions);
|
1
2
3
4
5
6
7
8
9
10
11
12
| SearchIndex<Result> index = client.initIndex("indexName", Result.class);
Query query = new Query("query string");
RequestOptions requestOptions =
new RequestOptions()
// Set the timeout to 20 seconds
.setTimeout(20000);
// Sync
SearchResult<Result> search = index.search(query, requestOptions);
// Async
CompletableFuture<SearchResult<Result>> search = index.searchAsync(query, requestOptions);
|
1
2
3
4
5
6
7
8
| // Set the readTimeout to 20 seconds
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
opts := []interface{}{
ctx,
}
res, err := index.Search("query string", opts...)
|
1
2
3
4
| // Not available yet using Request Options
//
// You can instead control a request timeout using a maximum duration on
// client.execute's Future execution.
|
1
2
3
4
5
6
7
8
9
| val indexName = IndexName("indexName")
val index = client.initIndex(indexName)
val query = Query("query string")
val requestOptions = requestOptions {
// Set the readTimeout to 20 seconds
readTimeout = 20
}
index.search(query, requestOptions)
|
Some methods allow you to pass further configurations with the requestOptions
parameter.
You can find method-specific requestOptions
in the Parameters section of each method’s reference.