This page documents an earlier version of the API client. For the latest version, see Customize the API clients.
Custom hosts
You can change the default hosts to which the API client connects:Add HTTP headers to every request
Adding HTTP headers to your requests lets you set parameters such as a user identifier or an IP address. This can be useful for analytics, geographical search, or applying API key rate limits.Header | Use cases |
---|---|
X-Algolia-UserToken |
|
X-Forwarded-For |
|
Add custom headers to individual requests
userToken
in events sent to the Insights API (also known as the anonymous user token, or session user token).
Change timeouts for all requests
Network connections and DNS resolution can be slow. That’s why the API clients come with default timeouts.Change timeouts for individual requests
Further customization options for JavaScript
This section describes more customization options for the JavaScript API clients.Change authentication mode
The JavaScript Search API client lets you change how you send your credentials to Algolia. You can set theauthmode
option to:
WithinHeaders
to send the credentials as headersWithinQueryParameters
to send the credentials as URL query parameters.
JavaScript
algoliasearch/lite
(browser):WithinQueryParameters
algoliasearch
(browser):WithinHeaders
algoliasearch
(Node):WithinHeaders
Cache requests and responses
The client caches requests to Algolia and their responses. You can change the location of your caches, or turn off caching completely. If you build your own cache, you must use theCache
type from @algolia/cache-common
.
The following cache types are available:
NullCache
. No caching for requests and responses. Every method call makes an API request.InMemoryCache
. The client stores requests and responses in memory. When you perform the same query again during your search session, the client reads the results from the cache instead of making a new API request. This avoids duplicate API calls, for example, when a user deletes characters from their current query. Similarly, the client retrieves results from the response cache for queries that you’ve already performed during your search session. TheInMemoryCache
resets on page refresh.LocalStorageCache
. The client stores requests and responses inlocalStorage
. When you perform the same query again while the value is still active, the client reads the results from the cache instead of making a new API request. This avoids duplicate API calls, for example, when a user refreshes a page. TheLocalStorageCache
resets values when their TTL has been reached. When local storage isn’t available, for example, when browsing in private mode, it’s better to useBrowserLocalStorageCache
insideFallbackableCache
.FallbackableCache
. An option to conditionally select one of the other cache types.
responsesCache
. Caches responses from Algolia. The client stores the response for a query and returns it when you perform the same query again.requestsCache
. Caches promises with the same request payload. The client stores the promise for a request and returns it when you perform the same request again, while the previous request is still pending.
JavaScript
algoliasearch/lite
(browser):InMemoryCache
algoliasearch
(browser):InMemoryCache
algoliasearch
(Node):NullCache
Cache the state of hosts
The JavaScript API client stores the state of hosts between search requests. This helps to avoid unreachable hosts. The state of the hosts remains in the cache for 2 minutes when the host is down. Whenever a host times out, the API client pushes it to the end of the list of hosts to query on the next request. You can build a custom cache for hosts as long as it respects theCache
type from @algolia/cache-common
.
The following cache types are available:
NullCache
. No caching for hosts.InMemoryCache
. The client stores the state of hosts in memory. The cache resets on page refresh.BrowserLocalStorageCache
. The client stores the state of hosts in the local storage. Refreshing the page doesn’t reset the cache. When local storage isn’t available, for example, when browsing in private mode, it’s better to useBrowserLocalStorageCache
insideFallbackableCache
.FallbackableCache
. An option to conditionally select one of the other cache types.
JavaScript
algoliasearch/lite
(browser):FallbackableCache(BrowserLocalStorageCache, InMemoryCache)
algoliasearch
(browser):FallbackableCache(BrowserLocalStorageCache, InMemoryCache)
algoliasearch
(Node):InMemoryCache
Logging
Thelogger
option helps you understand more about what’s going on with the client.
It accepts any implementation that respects the Logger type from @algolia/logger-common
.
These loggers are available:
NullLogger
. The client doesn’t log anything.ConsoleLogger
.The client logs events withconsole.log
.
JavaScript
algoliasearch/lite
(browser):ConsoleLogger(LogLevelEnum.Error)
algoliasearch
(browser):ConsoleLogger(LogLevelEnum.Error)
algoliasearch
(Node):NullLogger
Change HTTP request methods
Use therequester
option to specify how the client makes network requests.
Your custom implementation must respect the Requester type from @algolia/requester-common
.
The following request methods are available:
BrowserXhrRequester
uses XMLHttpRequestNodeHttpRequester
uses the native HTTP Node.js moduleFetchRequester
uses the fetch API.
JavaScript
algoliasearch/lite
(browser):BrowserXhrRequester
algoliasearch
(browser):BrowserXhrRequester
algoliasearch
(Node):NodeHttpRequester
Custom user agents
You can use a customuserAgent
for interacting with Algolia.
This is useful when you’re building an integration on top of algoliasearch
,
or when building custom clients.
Add to the default user agent
Add a string to the default user agent:JavaScript
userAgent
by default:
algoliasearch/lite
(browser):Algolia for JavaScript (a.b.c); Browser (lite);
algoliasearch
(browser):Algolia for JavaScript (a.b.c); Browser;
algoliasearch
(Node):Algolia for JavaScript (a.b.c); Node.js;
Replace the default user agent
Replace the user agent information by using thecreateUserAgent
function:
JavaScript
userAgent
is:
algoliasearch/lite
(browser):Algolia for JavaScript (x.x.x); Browser (lite);
algoliasearch
(browser):Algolia for JavaScript (x.x.x); Browser;
algoliasearch
(Node):Algolia for JavaScript (x.x.x); Node.js;
Keep-alive connections
By default, the JavaScript API client sends requests with theKeep-Alive
header: Connection: keep-alive
.
To close all remaining connections after you’re done interacting with the Algolia API,
use client.destroy()
:
JavaScript
Build an API client from scratch
For full control, create your API client from scratch with thecreateSearchClient
function.
JavaScript
Pass options to the PHP HTTP client
The PHP API client lets you override what HTTP layer will be used by the search client. You can pass custom options to the underlying HTTP client, for example, to configure a proxy. Choose between these two HTTP clients:- Guzzle, a popular HTTP client for PHP (recommended)
- A custom HTTP client built on top of curl
Guzzle HTTP client
See guzzle optionsPHP
Default HTTP client based on curl
See curl options.PHP