API Reference / API Methods / Request options

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.

Add HTTP headers

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'
]);

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
]);

Extra options

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.

Did you find this page helpful?