API client / Getting started

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 lets you set parameters, for example, a user identifier or an IP address. This can be useful for analytics, geographical 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 backend implementations. If your server sends the user’s IP address with every search, analytics can distinguish between 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 backend. This ensures that the geolocation for a search uses the your user’s IP address and not that of your server.
1
2
3
4
5
$index = $client->initIndex('indexName');

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

Make sure to use the same user token for your events (Insights API) and search requests (Search API).

  • If you send the authenticatedUserToken with your events, send the same value with your search requests.
  • If you send the userToken with your events, send the same value with your search requests.

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?