If you want to change all requests, you can customize the API clients, for example, by 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, edit the client’s configuration.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
require "algolia"

app_id = "ALGOLIA_APPLICATION_ID"
api_key = "ALGOLIA_API_KEY"

client = Algolia::SearchClient.create(
  "ALGOLIA_APPLICATION_ID",
  "ALGOLIA_API_KEY"
)

# Get the configuration object
config = client.api_client.config

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

Reference

connect_timeout
type: Integer
default: 2,000

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

read_timeout
type: Integer
default: 5,000

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

write_timeout
type: Integer
default: 30,000

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

User agent information

The API clients send user agent information as User-Agent header. To append your own information to this parameter, use the add_user_agent_segment function:

1
2
3
4
5
6
7
8
9
require "algolia"

client = Algolia::SearchClient.create(
  "ALGOLIA_APPLICATION_ID",
  "ALGOLIA_API_KEY"
)

# Add information to `User-Agent` header
client.add_user_agent_segment("custom ruby client", "optional version")

This appends “custom ruby client (optional version)” to the User-Agent header.

Headers

To add headers to all requests, use the header_params parameter of the client’s configuration:

1
2
3
4
5
6
7
8
9
10
11
12
require "algolia"

client = Algolia::SearchClient.create(
  "ALGOLIA_APPLICATION_ID",
  "ALGOLIA_API_KEY"
)

# Get the configuration object
config = client.api_client.config

# Add header to all requests
config.header_params["extra-header": "greetings"]

Custom hosts

If you want to proxy your API requests through another server, create a custom configuration and specify the hosts.

1
2
3
4
5
6
7
8
9
10
11
12
13
require "algolia"

hosts = [
    Algolia::Transport::StatefulHost.new("YOUR_SERVER_URL")
]

config = Algolia::Configuration.new(
    "ALGOLIA_APPLICATION_ID",
    "ALGOLIA_API_KEY",
    hosts
)

client = Algolia::SearchClient.create_with_config(config)

Each instance of StatefulHost accepts the following parameters.

url
type: str
Required

URL of your custom server (without scheme).

opts
type: Hash

Optional arguments as key-value pairs:

  • protocol (default: https://). Scheme for the URL, https or http
  • port. Port where your server accepts requests if it’s different from the default.
  • accept (default: CallType::Read | CallType::Write). Whether this server should be used for read requests, write requests, or both.
Did you find this page helpful?
Ruby API clients v3