The generated API clients are a work in progress, you can also find our stable clients on the Algolia documentation.

Skip to main content

Customized client usage

You might want to customize how the API client works, by providing additional information to your request, adding user-agents or use your own HTTP requester.

Setting a logger

You can set a custom logger on the client to enable more or less debug output depending on your need.

Send additional information in your request with requestOptions

The requestOptions parameter allows you to merge additional information with the client transporter, such as headers or query parameters.

Note: requestOptions overrides the default parameters that were previously set in the method and client.

await client.search(
{
requests: [
{
indexName: '<YOUR_INDEX_NAME>',
query: '<YOUR_QUERY>',
hitsPerPage: 50,
},
],
},
{
// This header is added to the request
headers: {
'additional-header': 'hello',
},
// As we re-define `hitsPerPage`, it will override the value defined in the method's parameters.
queryParameters: {
hitsPerPage: 100,
},
}
);

Provide your own user-agent

The API clients offers a method for you to append data to the current user-agent.

In the JavaScript client, user-agent are sent in the header via the x-algolia-agent property.

import { searchClient } from '@algolia/client-search';

const client = searchClient('<YOUR_APP_ID>', '<YOUR_API_KEY>');

// This will append `; my user agent (optional version)` to the current value of `x-algolia-agent` for every requests
client.addAlgoliaAgent('my user agent', 'optional version');

Use your own HTTP requester

You can override the default requester behavior by providing your requester logic at the initialization of the client.

The JavaScript client provides an echoRequester, that will return the full payload of the request.

import { searchClient } from '@algolia/client-search';
import { echoRequester } from '@algolia/requester-node-http';

const client = searchClient('<YOUR_APP_ID>', '<YOUR_API_KEY>', {
// The first parameter is the status to return
requester: echoRequester(200),
});