NAME

Twilio::CredentialProvider::ClientCredentialProvider - OAuth 2.0 client credentials

SYNOPSIS

use Twilio::CredentialProvider::ClientCredentialProvider;
use Twilio::Rest::Client;

my $client = Twilio::Rest::Client->new(
  undef, undef,
  {
    credential_provider => Twilio::CredentialProvider::ClientCredentialProvider->new({
      client_id     => $client_id,
      client_secret => $client_secret,
    }),
    account_sid => $account_sid,
  }
);

my $message = $client->messages->create($to, { from => $from, body => 'Hello' });

DESCRIPTION

Authenticates requests with an access token from an OAuth app, instead of an Auth Token or an API key. Create the app in the Twilio Console, under Account settings, OAuth applications, and choose its scopes there. See https://www.twilio.com/docs/iam/oauth-apps/account-oauth-apps.

The first request asks Twilio for a token, with the client_credentials grant. Later requests use the same token until 30 seconds before it expires, and then a new one is fetched. Clients that share a provider share its token.

Set account_sid on the client to use account resources such as $client->messages: an OAuth app has no username to take it from. The TWILIO_ACCOUNT_SID environment variable also sets it.

This differs from twilio-php in a few ways:

METHODS

new

my $provider = Twilio::CredentialProvider::ClientCredentialProvider->new({
  client_id     => $client_id,
  client_secret => $client_secret,
  token_manager => $token_manager,    # optional
});

client_id and client_secret are required, and it throws Twilio::Exceptions::ConfigurationException without them. token_manager is a Twilio::Http::BearerToken::TokenManager that gets tokens some other way; the default is a Twilio::Http::BearerToken::ApiTokenManager.

to_auth_strategy

Returns the provider's Twilio::AuthStrategy::TokenAuthStrategy, the same one each time.

token_manager_class

Returns the class of the default token manager.

get_client_id

get_token_manager

Return what "new" was given or built.

LICENSE

Copyright (c) 2026 Francisco Zarabozo. Released under the MIT License.