API Reference / API Methods / Add request options

You can change the default timeouts, or add extra HTTP headers to your API requests by passing options. These API client methods support passing request options as extra key-value mappings:

To add options to all requests, you can create a custom configuration.

We released a new version of the JavaScript API client in public beta. Read the beta documentation for more information.

We released a new version of the PHP API client in public beta. Read the beta documentation for more information.

We released a new version of the Java API client in public beta. Read the beta documentation for more information.

Add HTTP headers to your requests

Adding HTTP headers to your requests allow you to set parameters, for example, a user identifier or an IP address. This can be useful for analytics, geo search, or to implement API key rate limits.

You can add these headers to your requests:

Header Use case
X-Algolia-UserToken Use API key rate limits
X-Algolia-UserToken The Analytics API uses the value of this header to distinguish between users. It takes priority over any value in X-Forwarded-For. Use the X-Algolia-UserToken header to forward the user’s identity without relying on IP addresses.
X-Forwarded-For For analytics in back-end implementations. If your server sends the user’s IP address 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.
X-Forwarded-For For geolocation, 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 server.
1
2
3
4
5
$index = $client->initIndex('indexName');

$res = $index->search('query string', [
  'X-Algolia-UserToken' => 'user123'
]);

Change timeouts for your requests

Network connections and DNS resolution can be slow. That’s why the API clients come with default timeouts.

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 accept extra request options. You can find them in the Parameters section for each method’s reference.

Did you find this page helpful?