If you want to change all requests, you can customize the API clients, for example, by using a custom HTTP client, changing the user agent information, or changing the default timeouts.

To customize individual requests, see Request options.

Timeouts

To change the timeouts for all requests, pass the timeouts as parameters when creating the client.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import { searchClient } from "@algolia/client-search";

const client = searchClient(
  "ALGOLIA_APPLICATION_ID",
  "ALGOLIA_API_KEY",
  {
    // Adjust timeouts
    timeouts: {
        read: 10000,
        write: 10000,
        connect: 10000,
    },
  },
);

timeouts

connect
type: number
default: 1,000 (browser), 2,000 (Node.js)

Maximum number of milliseconds to wait for the connection to be established.

read
type: number
default: 2,000 (browser), 5,000 (Node.js)

Maximum number of milliseconds to wait for a response from the server for read requests.

write
type: number
default: 30,000 (browser and Node.js)

Maximum number of milliseconds to wait for a response from the server for write requests.

User agent information

The API clients send user agent information as x-algolia-agent query parameter. To append your own information to this parameter, use the addAlgoliaAgent function:

1
2
3
4
5
6
7
8
import { searchClient } from "@algolia/client-search";

const client = searchClient(
  "ALGOLIA_APPLICATION_ID",
  "ALGOLIA_API_KEY",
);

client.addAlgoliaAgent("custom javascript client", "optional version");

This appends “custom javascript client (optional version)” to the x-algolia-agent query parameter.

Custom HTTP requesters

You can provide your own client for making HTTP requests with the requester parameter. For example, to use the echoRequester from the @algolia/requester-node-http package, which returns the full request payload:

1
2
3
4
5
6
7
8
9
10
11
import { searchClient } from "@algolia/client-search";
import { echoRequester } from "@algolia/requester-node-http";

const client = searchClient(
  "ALGOLIA_APPLICATION_ID",
  "ALGOLIA_API_KEY",
  {
    // The first parameter is the status to return
    requester: echoRequester(200),
  }
);

Headers

To add headers to all requests, use the baseHeaders parameter when creating the client:

1
2
3
4
5
6
7
8
9
10
11
12
import { searchClient } from "@algolia/client-search";

const client = searchClient(
  "ALGOLIA_APPLICATION_ID",
  "ALGOLIA_API_KEY",
  {
    // Add header to all requests
    baseHeaders: {
      "extra-header": "greetings",
    },
  },
);

Query parameters

To add query parameters to all requests, use the baseQueryParameters parameter when creating the client:

1
2
3
4
5
6
7
8
9
10
11
12
import { searchClient } from "@algolia/client-search";

const client = searchClient(
  "ALGOLIA_APPLICATION_ID",
  "ALGOLIA_API_KEY",
  {
    // Add query parameters to all requests
    baseQueryParameters: {
      queryParam: "value",
    },
  },
);

Custom hosts

If you want to proxy your API requests through another server, use the hosts parameter when creating the client.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import { searchClient } from '@algolia/client-search';

const client = searchClient(
  "ALGOLIA_APPLICATION_ID",
  "ALGOLIA_API_KEY",
  {
    // Add your own servers
    hosts: [
      {
        url: "YOUR_SERVER_URL",
        accept: "readWrite",
        protocol: "https",
      },
    ],
  },
);

Each instance of Host accepts the following parameters.

url
type: string
Required

URL of your custom server (without scheme).

accept
type: read | write | readWrite
Required

Whether this server can be used for read requests, write requests, or both.

protocol
type: http | https
Required

Scheme for the URL.

port
type: number?

Port where your server accepts requests if deviating from the default.

Did you find this page helpful?
JavaScript API clients v5