🎉 Try the public beta of the new docs site at algolia.com/doc-beta! 🎉
API client / Getting started / Customize

Customize your Python client

You can customize the behavior of the API clients by creating a custom configuration This lets you change timeouts, or add HTTP headers.

To modify all requests made with a client, create a custom configuration. To modify individual requests, pass custom request options.

Use a custom host

You can change the default hosts to which the API client connects:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from algoliasearch.configs import Config
from algoliasearch.http.hosts import Host, HostsCollection
from algoliasearch.search_client import SearchClient

class CustomConfig(Config):
    def build_hosts(self):
        # type: () -> HostsCollection

        return HostsCollection(
            [Host("yourdomain.com")]
        )

config = CustomConfig("YourApplicationID", "YourApiKey")
client = SearchClient.create_with_config(config)

Changing the hosts can be useful if you want to proxy the search requests through another server, for example, to process the request or response, or to perform custom analytics.

If you use this API Client with Google AppEngine, the client uses the urlfetch module instead of the request module. Please be aware of urlfetch’s limits. Note that SSL certificates aren’t verified for calls to domains other than algolia.net due to the lack of Server Name Indication (SNI) support in urlfetch. To run unit tests on the AppEngine stub, please define the APPENGINE_RUNTIME environment variable.

Add HTTP headers to every request

Adding HTTP headers to your configuration allow you to set parameters for every request, for example, a user identifier or an IP address. This can be useful for analytics, geographic searches, or to implement API key rate limits.

For an overview of available HTTP headers, see Add HTTP headers to your requests

1
2
3
4
config = SearchConfig('YourApplicationID', 'YourWriteAPIKey')
config.headers['NAME-OF-HEADER'] = 'value-of-header'

client = SearchClient.create_with_config(config)

Change timeouts for all requests

Network connections and DNS resolution can be slow. That’s why the API clients come with default timeouts.

You shouldn’t change the default timeouts, unless you have a good reason.

1
2
3
4
5
6
config = SearchConfig('YourApplicationID', 'YourWriteAPIKey')
config.connect_timeout = 2 # connection timeout in seconds
config.read_timeout = 5 # read timeout in seconds
config.write_timeout =  30 # write timeout in seconds

client = SearchClient.create_with_config(config)
Did you find this page helpful?