NAME
Twilio::Base::BaseClient - Credentials, routing and requests shared by all clients
DESCRIPTION
This is the base class of Twilio::Rest::Client, which is the class to use. It finds the credentials, chooses the host for the configured edge and region, adds the standard headers and passes each request to the HTTP client.
METHODS
new
my $client = Twilio::Base::BaseClient->new($username, $password, \%options);
$username and $password are an Account SID and Auth Token, or an API key SID and secret. They default to the TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN environment variables. The options are:
- account_sid
-
The account to act on, which defaults to
$username. It is required with an API key, and with a credential provider for account resources such as$client->messages. - credential_provider
-
A Twilio::CredentialProvider::CredentialProvider that authenticates requests instead of the username and password, such as Twilio::CredentialProvider::ClientCredentialProvider for OAuth. See "set_credential_provider".
- region, edge
-
Where to route requests. They default to
TWILIO_REGIONandTWILIO_EDGE. Set both: Twilio routes a region without an edge to US1, and hostnames with a region but no edge no longer work. - http_client
-
A Twilio::Http::Client object. The default is Twilio::Http::TinyClient.
- environment
-
A hash reference to read the environment variables from, instead of
%ENV. - user_agent_extensions
-
An array reference of strings to add to the
User-Agentheader. - max_retries
-
How many times to repeat a request that Twilio answers with HTTP 429. The default is 0, which means never. See "Retries".
Retries
With max_retries set, a request that gets a 429 response is sent again, after the number of seconds in the Retry-After header. Without that header, the waits are 1, 2, 4 seconds and so on, up to 60.
Nothing else is retried. After a server error or a lost connection, Twilio may already have accepted a request, so repeating it could send a message twice.
request does not return while it waits, so each retry can block the process for up to 60 seconds. When Retry-After asks for more than that, there is no retry, and the 429 response is returned. When the retries run out, the last response is returned too.
request
my $response = $client->request($method, $url, \%options);
Sends a request and returns a Twilio::Http::Response. The options are those of "request" in Twilio::Http::Client. username and password default to the client's credentials. Without both, the credential provider authenticates the request, unless the request has its own Authorization header. It throws Twilio::Exceptions::ConfigurationException when there are no credentials. It does not treat HTTP error statuses as exceptions.
capture_responses
my ($result, @responses) = $client->capture_responses(sub { ... });
Runs the code and returns what it returns, followed by the Twilio::Http::Response of each request the client made meanwhile, in order. A request repeated after HTTP 429 gives only its last answer. This is what with_metadata uses; see Twilio::Metadata.
build_uri
my $url = $client->build_uri('https://api.twilio.com/2010-04-01/Accounts.json');
Returns the URL with the client's edge and region applied, in the form api.sydney.au1.twilio.com. An edge with no region uses us1. A region or edge already in the URL is kept, unless the client sets its own.
validate_ssl_certificate
$client->validate_ssl_certificate;
Connects to Twilio's TLS test host, using the given HTTP client or the client's own. Throws Twilio::Exceptions::TwilioException on a bad response.
get_arg
my $value = $client->get_arg($value, $environment_variable);
Returns the value if it is not empty, and otherwise the environment variable.
set_credential_provider
$client->set_credential_provider($provider);
Authenticates requests with $provider, and clears the username and password, as twilio-php does. The account SID is kept. A username and password set again later, or given to one request, are used instead of the provider. It throws Twilio::Exceptions::ConfigurationException when $provider is not a credential provider.
invalidate_basic_auth
Clears the username and password.
invalidate_oauth
Removes the credential provider. Requests then need a username and password.
get_credential_provider
get_environment
get_username
get_password
get_account_sid
get_region
get_edge
get_http_client
get_log_level
get_max_retries
Return the current value of each setting.
set_username
set_password
set_account_sid
set_http_client
Replace the current value of each setting.
set_max_retries
$client->set_max_retries(3);
Sets the number of retries. With no argument, it turns retries off. It throws Twilio::Exceptions::ConfigurationException unless the value is a whole number.
set_edge
$client->set_edge('sydney');
Sets the edge. With no argument, it uses TWILIO_EDGE, or unsets the edge.
set_log_level
$client->set_log_level('debug');
Sets the log level. At debug, requests and responses are logged to STDERR with warn, without the Authorization header. With no argument, it uses TWILIO_LOG_LEVEL. Setting DEBUG_HTTP_TRAFFIC=true has the same effect.
LICENSE
Copyright (c) 2026 Francisco Zarabozo. Released under the MIT License.