NAME

Twilio::Jwt::AccessToken - A token that lets a client use Twilio directly

SYNOPSIS

use Twilio::Jwt::AccessToken;
use Twilio::Jwt::Grants::VoiceGrant;

my $token = Twilio::Jwt::AccessToken->new(
  $account_sid, $api_key_sid, $api_key_secret, 3600, 'alice'
);

my $grant = Twilio::Jwt::Grants::VoiceGrant->new;
$grant->set_outgoing_application_sid($application_sid);
$grant->set_incoming_allow(1);
$token->add_grant($grant);

print $token->to_jwt;

DESCRIPTION

An Access Token gives a browser or a mobile app limited use of Twilio, without handing it your credentials. It is signed with an API key, so create one for this and keep its secret on your server.

What a token allows comes from the grants added to it: see Twilio::Jwt::Grants::ChatGrant, Twilio::Jwt::Grants::PlaybackGrant, Twilio::Jwt::Grants::SyncGrant, Twilio::Jwt::Grants::TaskRouterGrant, Twilio::Jwt::Grants::VideoGrant and Twilio::Jwt::Grants::VoiceGrant.

A token converts to its text in string context.

METHODS

new

my $token = Twilio::Jwt::AccessToken->new(
  $account_sid, $signing_key_sid, $secret, $ttl, $identity, $region
);

$ttl is how many seconds the token lasts, and defaults to 3600. $identity and $region are optional.

add_grant

$token->add_grant($grant);

Adds a grant and returns the token. It dies when $grant is not a grant object.

add_claim

$token->add_claim($name, $value);

Adds a claim of your own. It cannot replace the claims the token needs, such as iss or exp.

to_jwt

my $text = $token->to_jwt($algorithm);

Returns the signed token. $algorithm defaults to HS256. The expiry counts from this call.

get_identity

set_identity

get_nbf

set_nbf

nbf is the time, in seconds since the epoch, before which the token is not valid.

get_region

set_region

Read and change each value. The set_ methods return the token.

LICENSE

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