This page shows how to generate a token on your server for Agora SDK versions 2.1.0+. The token is used for joining a channel.

The following programming languages are covered. Choose the one that applies to you:

  • Java
  • C++
  • Python
  • Go
  • PHP
  • Node.js

Java

Initializes the Token Builder

public boolean initTokenBuilder(String originToken);

This method uses the original token to reinitialize the token builder. This method enables the token builder to inherit the App ID, App Certificate, Channel Name, uid, and Privilege of the original token.

Use this method with the Set the User Privilege (setPrivilege) and Remove the User Privilege (removePrivilege) methods to add or delete privileges to the original token.

Name Description
originToken The original token.
Return value
  • 0: Method call succeeded.
  • <0: Method call failed.

Struct of the TokenBuilder

public SimpleTokenBuilder(String appId, String appCertificate, String channelName, String uid);

This method is the struct of SimpleTokenBuilder.

Name Description
App ID ID of the application that you registered in the Agora Dashboard. See Getting an App ID.
App Certificate Certificate of the application that you registered in the Agora Dashboard. See Get an App Certificate.
channelName Name of the channel that the user wants to join.
uid ID of the user who wants to join a channel.

Initializes the User Privileges (initPrivileges)

public boolean initPrivileges(Role role);

This method initializes user privileges by specifying the user role.

Name Description
role

The user role. Each role is associated with a set of user privileges. Choose from the following roles:

  • 0: Attendee. Participants in a voice call or video call.
  • 1: Publisher. Users (hosts) who publish video or/and voice streams in a live broadcast.
  • 2: Subscriber. Users (audience) who need to subscribe to the voice and video streams in a live broadcast.
  • 101: Admin. Administrators.
Return value
  • 0: Method call succeeded.
  • <0: Method call failed.

Sets the User Privilege (setPrivilege)

public void setPrivilege(AccessToken.Privileges privilege, int expireTimestamp);

This method sets the user privilege according to the user role specified in Initialize the User Privileges (initPrivileges).

Name Description
privilege

The user privilege according to the user role:

  • Attendee Privileges:
    • Join a channel.
    • Publish the audio stream.
    • Publish the video stream.
    • Publish the data stream.
  • Publisher Privileges:
    • Join a channel.
    • Publish the audio stream.
    • Publish the video stream.
    • Publish the data stream.
    • Publish the audio CDN.
    • Publish the video CDN.
    • Invite subscribers to publish the audio stream. [1]
    • Invite subscribers to publish the video stream. [1]
    • Invite subscribers to publish the data stream. [1]
  • Subscriber Privileges:
    • Join a channel.
    • Request to publish the audio stream. [1]
    • Request to publish the video stream. [1]
    • Request to publish the data stream. [1]
  • Admin Privileges:
    • Join a channel.
    • Publish the audio stream.
    • Publish the video stream.
    • Publish the data stream.
    • Administrate the channel.
expireTimestamp [2] The privilege expiration time. The default value is 0 (the privilege never expires).

[1] Agora does not support the publisher inviting users to publish a voice/video/data stream or the subscriber requesting to publish a voice/video/data stream.

[2] The expireTimestamp is represented by the number of seconds elapsed since 1/1/1970. For example, if you want to access the Agora Service within 10 minutes after the privilege is generated, set expireTimestamp as the current timestamp + 600 (seconds). The valid time for each privilege is independent.

Removes the User Privilege (removePrivilege)

public void removePrivilege(AccessToken.Privileges privilege);

This method removes a previously added privilege.

Generates a Token (buildToken)

public String buildToken();

This method generates a token in the string format.

C++

Initializes the Token Builder

bool initTokenBuilder(const std::string& originToken);

This method uses the original token to reinitialize the token builder. This method enables the token builder to inherit the App ID, App Certificate, Channel Name, uid, and privilege of the original token.

Use this method with the Set the User Privilege (setPrivilege) and Remove the User Privilege (removePrivilege) methods to add or delete privileges to the original token.

Name Description
originToken The original token.
Return value
  • 0: Method call succeeded.
  • <0: Method call failed.

Struct of the TokenBuilder

SimpleTokenBuilder();
SimpleTokenBuilder(const std::string& appId, const std::string& appCertificate,
                   const std::string& channelName, uint32_t uid = 0);
SimpleTokenBuilder(const std::string& appId, const std::string& appCertificate,
                   const std::string& channelName, const std::string& uid = "");

This method is the struct of SimpleTokenBuilder.

Name Description
App ID ID of the application that you registered in the Agora Dashboard. See Getting an App ID.
App Certificate Certificate of the application that you registered in the Agora Dashboard. See Get an App Certificate.
channelName Name of the channel that the user wants to join.
uid ID of the user who wants to join a channel.

Initializes the User Privileges (initPrivileges)

bool initPrivileges(Role role);

This method initializes user privileges by specifying the user role.

Name Description
role

The user role. Each role is associated with a set of user privileges. Choose from the following roles:

  • 0: Attendee. Participants in a voice call or video call.
  • 1: Publisher. Users (hosts) who publish video or/and voice streams in a live broadcast.
  • 2: Subscriber. Users (audience) who need to subscribe to the voice and video streams in a live broadcast.
  • 101: Admin. Administrators.
Return value
  • 0: Method call succeeded.
  • <0: Method call failed.

Sets the User Privilege (setPrivilege)

void setPrivilege(AccessToken::Privileges privilege, uint32_t expireTimestamp = 0);

This method sets the user privilege according to the user role specified in Initialize the User Privileges (initPrivileges).

Name Description
privilege

The user privilege according to the user role:

  • Attendee Privileges:
    • Join a channel.
    • Publish the audio stream.
    • Publish the video stream.
    • Publish the data stream.
  • Publisher Privileges:
    • Join a channel.
    • Publish the audio stream.
    • Publish the video stream.
    • Publish the data stream.
    • Publish the audio CDN.
    • Publish the video CDN.
    • Invite subscribers to publish the audio stream. [1]
    • Invite subscribers to publish the video stream. [1]
    • Invite subscribers to publish the data stream. [1]
  • Subscriber Privileges:
    • Join a channel.
    • Request to publish the audio stream. [1]
    • Request to publish the video stream. [1]
    • Request to publish the data stream. [1]
  • Admin Privileges:
    • Join a channel.
    • Publish the audio stream.
    • Publish the video stream.
    • Publish the data stream.
    • Administrate the channel.
expireTimestamp [2] The privilege expiration time. The default value is 0 (the privilege never expires).

[1] Agora does not support the publisher inviting users to publish a voice/video/data stream or the subscriber requesting to publish a voice/video/data stream.
[2] The expireTimestamp is represented by the number of seconds elapsed since 1/1/1970. For example, you want to access the Agora Service within 10 minutes after the privilege is generated, set expireTimestamp as the current timestamp + 600 (seconds). The valid time for each privilege is independent.

Removes the User Privilege (removePrivilege)

void removePrivilege(AccessToken::Privileges privilege);

This method removes a previously added privilege.

Generates a Token (buildToken)

std::string buildToken();

This method generates a token in the string format.

Python

Initializes the Token Builder

def initTokenBuilder(self, originToken);

This method uses the original token to reinitialize the token builder. This method enables the token builder to inherit the App ID, App Certificate, Channel Name, uid, and Privilege of the original token.

Use this method with the Set the User Privilege (setPrivilege) and Remove the User Privilege (removePrivilege) methods to add or delete privileges to the original token.

Name Description
originToken The original token.
Return value
  • 0: Method call succeeded.
  • <0: Method call failed.

Struct of the TokenBuilder

def __init__(self, appID, appCertificate, channelName, uid);

This method is the struct of SimpleTokenBuilder.

Name Description
App ID ID of the application that you registered in the Agora Dashboard. See Getting an App ID.
App Certificate Certificate of the application that you registered in the Agora Dashboard. See Get an App Certificate.
channelName Name of the channel that the user wants to join.
uid ID of the user who wants to join a channel.

Initializes the User Privileges (initPrivileges)

def initPrivileges(self, role);

This method initializes the user privileges by specifying the user role.

Name Description
role

User role. Each role is associated with a set of user privileges. Choose from the following roles:

  • 0: Attendee. Participants in a voice call or video call.
  • 1: Publisher. Users (hosts) who publish video and/or voice streams in a live broadcast.
  • 2: Subscriber. Users (audience) who need to subscribe to the voice and video streams in a live broadcast.
  • 101: Admin. Administrators.
Return value
  • 0: Method call succeeded.
  • <0: Method call failed.

Sets the User Privilege (setPrivilege)

def setPrivilege(self, privilege, expireTimestamp);

This method sets the user privilege according to the user role specified in Initialize the User Privileges (initPrivileges).

Name Description
privilege

User privilege according to the user role:

  • Attendee Privileges:
    • Join a channel.
    • Publish the audio stream.
    • Publish the video stream.
    • Publish the data stream.
  • Publisher Privileges:
    • Join a channel.
    • Publish the audio stream.
    • Publish the video stream.
    • Publish the data stream.
    • Publish the audio CDN.
    • Publish the video CDN.
    • Invite subscribers to publish the audio stream. [1]
    • Invite subscribers to publish the video stream. [1]
    • Invite subscribers to publish the data stream. [1]
  • Subscriber Privileges:
    • Join a channel.
    • Request to publish the audio stream. [1]
    • Request to publish the video stream. [1]
    • Request to publish the data stream. [1]
  • Admin Privileges:
    • Join a channel.
    • Publish the audio stream.
    • Publish the video stream.
    • Publish the data stream.
    • Administrate the channel.
expireTimestamp [2] The privilege expiration time. The default value is 0 (the privilege never expires).

[1] Agora does not support the publisher inviting users to publish a voice/video/data stream or the subscriber requesting to publish a voice/video/data stream.

[2] The expireTimestamp is represented by the number of seconds elapsed since 1/1/1970. For example, if you want to access the Agora Service within 10 minutes after the privilege is generated, set expireTimestamp as the current timestamp + 600 (seconds). The valid time for each privilege is independent.

Removes the User Privilege (removePrivilege)

def removePrivilege(self, privilege);

This method removes a previously added privilege.

Generates a Token (buildToken)

def buildToken(self);

This method generates a token in the string format.

Go

Initializes the Token Builder

func (builder SimpleTokenBuilder) InitTokenBuilder(originToken string);

This method uses the original token to reinitialize the token builder. This method enables the token builder to inherit the App ID, App Certificate, Channel Name, uid, and Privilege of the original token.

Use this method with the Set the User Privilege (setPrivilege) and Remove the User Privilege (removePrivilege) methods to add or delete privileges to the original token.

Name Description
originToken The original token.
Return value
  • 0: Method call succeeded.
  • <0: Method call failed.

Struct of the TokenBuilder

func CreateSimpleTokenBuilder(appID, appCertificate, channelName string, uid uint32) SimpleTokenBuilder;

This method is the struct of SimpleTokenBuilder.

Name Description
App ID ID of the application that you registered in the Agora Dashboard. See Getting an App ID.
App Certificate Certificate of the application that you registered in the Agora Dashboard. See Get an App Certificate.
channelName Name of the channel that the user wants to join.
uid ID of the user who wants to join a channel.

Initializes the User Privileges (initPrivileges)

func (builder SimpleTokenBuilder) InitPrivileges(role Role);

This method initializes the user privileges by specifying the user role.

Name Description
role

User role. Each role is associated with a set of user privileges. Choose from the following roles:

  • 0: Attendee. Participants in a voice call or video call.
  • 1: Publisher. Users (hosts) who publish video and/or voice streams in a live broadcast.
  • 2: Subscribe. Users (audience) who need to subscribe to the voice and video streams in a live broadcast.
  • 101: Admin. Administrators.
Return value
  • 0: Method call succeeded.
  • <0: Method call failed.

Sets the User Privilege (setPrivilege)

func (builder SimpleTokenBuilder) SetPrivilege(privilege AccessToken.Privileges, expireTimestamp uint32);

This method sets the user privilege according to the user role specified in Initialize the User Privileges (initPrivileges).

Name Description
privilege

User privilege according to the user role:

  • Attendee Privileges:
    • Join a channel.
    • Publish the audio stream.
    • Publish the video stream.
    • Publish the data stream.
  • Publisher Privileges:
    • Join a channel.
    • Publish the audio stream.
    • Publish the video stream.
    • Publish the data stream.
    • Publish the audio CDN.
    • Publish the video CDN.
    • Invite subscribers to publish the audio stream. [1]
    • Invite subscribers to publish the video stream. [1]
    • Invite subscribers to publish the data stream. [1]
  • Subscriber Privileges:
    • Join a channel.
    • Request to publish the audio stream. [1]
    • Request to publish the video stream. [1]
    • Request to publish the data stream. [1]
  • Admin Privileges:
    • Join a channel.
    • Publish the audio stream.
    • Publish the video stream.
    • Publish the data stream.
    • Administrate the channel.
expireTimestamp [2] The privilege expiration time. The default value is 0 (the privilege never expires).

[1] Agora does not support the publisher inviting users to publish a voice/video/data stream or the subscriber requesting to publish a voice/video/data stream.

[2] The expireTimestamp is represented by the number of seconds elapsed since 1/1/1970. For example, if you want to access the Agora Service within 10 minutes after the privilege is generated, set expireTimestamp as the current timestamp + 600 (seconds). The valid time for each privilege is independent.

Removes the User Privilege (removePrivilege)

func (builder SimpleTokenBuilder) RemovePrivilege(privilege AccessToken.Privileges);

This method removes a previously added privilege.

Generates a Token (buildToken)

func (builder SimpleTokenBuilder) BuildToken() (string,error);

This method generates a token in the string format.

PHP

Initializes the Token Builder

function initTokenBuilder($originToken);

This method uses the original token to reinitialize the token builder. This method enables the token builder to inherit the App ID, App Certificate, Channel Name, uid, and Privilege of the original token.

Use this method with the Set the User Privilege (setPrivilege) and Remove the User Privilege (removePrivilege) methods to add or delete privileges to the original token.

Name Description
originToken The original token.
Return value
  • 0: Method call succeeded.
  • <0: Method call failed.

Struct of the TokenBuilder

public function __construct($appID, $appCertificate, $channelName, $uid);

This method is the struct of SimpleTokenBuilder.

Name Description
App ID ID of the application that you registered in the Agora Dashboard. See Getting an App ID.
App Certificate Certificate of the application that you registered in the Agora Dashboard. See Get an App Certificate.
channelName Name of the channel that the user wants to join.
uid ID of the user who wants to join a channel.

Initializes the User Privileges (initPrivileges)

public function initPrivilige($role);

This method initializes the user privileges by specifying the user role.

Name Description
role

User role. Each role is associated with a set of user privileges. Choose from the following roles:

  • 0: Attendee. Participants in a voice call or video call.
  • 1: Publisher. Users (hosts) who publish video and/or voice streams in a live broadcast.
  • 2: Subscriber. Users (audience) who need to subscribe to the voice and video streams in a live broadcast.
  • 101: Admin. Administrators.
Return value
  • 0: Method call succeeded.
  • <0: Method call failed.

Sets the User Privilege (setPrivilege)

public function setPrivilege($privilege, $expireTimestamp)

This method sets the user privilege according to the user role specified in Initialize the User Privileges (initPrivileges).

Name Description
privilege

User privilege according to the user role:

  • Attendee Privileges:
    • Join a channel.
    • Publish the audio stream.
    • Publish the video stream.
    • Publish the data stream.
  • Publisher Privileges:
    • Join a channel.
    • Publish the audio stream.
    • Publish the video stream.
    • Publish the data stream.
    • Publish the audio CDN.
    • Publish the video CDN.
    • Invite subscribers to publish the audio stream. [1]
    • Invite subscribers to publish the video stream. [1]
    • Invite subscribers to publish the data stream. [1]
  • Subscriber Privileges:
    • Join a channel.
    • Request to publish the audio stream. [1]
    • Request to publish the video stream. [1]
    • Request to publish the data stream. [1]
  • Admin Privileges:
    • Join a channel.
    • Publish the audio stream.
    • Publish the video stream.
    • Publish the data stream.
    • Administrate the channel.
expireTimestamp [2] The privilege expiration time. The default value is 0 (the privilege never expires).

[1] Agora does not support the publisher inviting users to publish a voice/video/data stream or the subscriber requesting to publish a voice/video/data stream.

[2] The expireTimestamp is represented by the number of seconds elapsed since 1/1/1970. For example, if you want to access the Agora Service within 10 minutes after the privilege is generated, set expireTimestamp as the current timestamp + 600 (seconds). The valid time for each privilege is independent.

Removes the User Privilege (removePrivilege)

public function removePriviledge($privilege);

This method removes a previously added privilege.

Generates a Token (buildToken)

public function buildToken();

This method generates a token in the string format.

Node.js

Initializes the Token Builder

initTokenBuilder = function (originToken);

This method uses the original token to reinitialize the token builder. Once called, this method enables the token builder to inherit the App ID, App Certificate, Channel Name, uid, and Privilege of the original token.

Use this method with the Set the User Privilege (setPrivilege) and Remove the User Privilege (removePrivilege) methods to add or delete privileges to the original token.

Name Description
originToken The original token.
Return value
  • 0: Method call succeeded.
  • <0: Method call failed.

Struct of the TokenBuilder

var SimpleTokenBuilder = function (appID, appCertificate, channelName, uid);

This method is the struct of SimpleTokenBuilder.

Name Description
App ID ID of the application that you registered in the Agora Dashboard. See Getting an App ID.
App Certificate Certificate of the application that you registered in the Agora Dashboard. See Get an App Certificate.
channelName Name of the channel that the user wants to join.
uid ID of the user who wants to join a channel.

Initializes the User Privileges (initPrivileges)

this.initPrivileges = function (role);

This method initializes the user privileges by specifying the user role.

Name Description
role

User role. Each role is associated with a set of user privileges. Choose from the following roles:

  • 0: Attendee. Participants in a voice call or video call.
  • 1: Publisher. Users (hosts) who publish video and/or voice streams in a live broadcast.
  • 2: Subscriber. Users (audience) who need to subscribe to the voice and video streams in a live broadcast.
  • 101: Admin. Administrators.
Return value
  • 0: Method call succeeded.
  • <0: Method call failed.

Sets the User Privilege (setPrivilege)

this.setPrivilege = function (privilege, expireTimestamp);

This method sets the user privilege according to the user role specified in Initialize the User Privileges (initPrivileges).

Name Description
privilege

User privilege according to the user role:

  • Attendee Privileges:
    • Join a channel.
    • Publish the audio stream.
    • Publish the video stream.
    • Publish the data stream.
  • Publisher Privileges:
    • Join a channel.
    • Publish the audio stream.
    • Publish the video stream.
    • Publish the data stream.
    • Publish the audio CDN.
    • Publish the video CDN.
    • Invite subscribers to publish the audio stream. [1]
    • Invite subscribers to publish the video stream. [1]
    • Invite subscribers to publish the data stream. [1]
  • Subscriber Privileges:
    • Join a channel.
    • Request to publish the audio stream. [1]
    • Request to publish the video stream. [1]
    • Request to publish the data stream. [1]
  • Admin Privileges:
    • Join a channel.
    • Publish the audio stream.
    • Publish the video stream.
    • Publish the data stream.
    • Administrate the channel.
expireTimestamp [2] The privilege expiration time. The default value is 0 (the privilege never expires).

[1] Agora does not support the publisher inviting users to publish a voice/video/data stream or the subscriber requesting to publish a voice/video/data stream.

[2] The expireTimestamp is represented by the number of seconds elapsed since 1/1/1970. For example, if you want to access the Agora Service within 10 minutes after the privilege is generated, set expireTimestamp as the current timestamp + 600 (seconds). The valid time for each privilege is independent.

Removes the User Privilege (removePrivilege)

this.removePrivilege = function (privilege);

This method removes a previously added privilege.

Generates a Token (buildToken)

this.buildToken = function ();

This method generates a token in the string format.