If you want to change all requests, you can customize the API clients, for example, by changing the default timeouts.

To change individual requests, see Request options.

Examples on this page show how to customize the Search API client. To customize other API clients, use the appropriate Config class: For example:

1
2
3
from algoliasearch.recommend.config import RecommendConfig
from algoliasearch.insights.config import InsightsConfig
from algoliasearch.analytics.config import AnalyticsConfig

Timeouts

To change the timeouts for all requests, create a custom configuration and modify the timeout parameters:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from algoliasearch.search.client import SearchClientSync
from algoliasearch.search.config import SearchConfig

# Create custom configuration
config = SearchConfig(
    app_id="ALGOLIA_APPLICATION_ID",
    api_key="ALGOLIA_API_KEY",
)

# Adjust timeouts
config.connect_timeout = 10_000
config.read_timeout = 10_000
config.write_timeout = 10_000

# Create client with custom configuration
client = SearchClientSync(config=config)
connect_timeout
type: int
default: 5,000

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

read_timeout
type: int
default: 5,000

Maximum number of milliseconds to wait for a response from the server (for GET requests).

write_timeout
type: int
default: 5,000

Maximum number of milliseconds to wait for a response from the server (for requests other than GET).

Headers

To add headers to all requests, use the headers parameter:

1
2
3
4
5
6
7
8
9
from algoliasearch.search.config import SearchConfig

config = SearchConfig(
    app_id="ALGOLIA_APPLICATION_ID",
    api_key="ALGOLIA_API_KEY",
)

# Add header to all requests
config.headers["extra-header"] = "greetings"

Custom hosts

If you want to proxy your API requests through another server, use the hosts parameter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from algoliasearch.http.hosts import Host, HostsCollection
from algoliasearch.search.config import SearchConfig

config = SearchConfig(
    app_id="ALGOLIA_APPLICATION_ID",
    api_key="ALGOLIA_API_KEY",
)
config.hosts = HostsCollection(
    hosts=[
        Host(url="YOUR_SERVER_URL"),
    ]
)

client = SearchClientSync(config=config)

Each instance of Host accepts the following parameters.

url
type: str
Required

URL of your custom server (without scheme).

scheme
type: str
default: https

Scheme for the URL, https or http.

accept
type: CallType.READ | CallType.WRITE | (CallType.READ | CallType.WRITE) | None
default: None

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

port
type: int
default: None

Port where your server accepts requests if deviating from the

priority
type: int
default: None

If you use multiple hosts, and you create the HostsCollection list with reorder_hosts=True, you can specify which host should be queried first by giving it a higher priority.

Did you find this page helpful?
Python API clients v4