Authentication overview

Agora.io's authentication system is designed to secure access to its real-time communication services, ensuring that only authorized applications and users can join calls, streams, or messaging channels. This is primarily achieved through a token-based mechanism that verifies the identity of clients attempting to connect to the Agora.io platform. Tokens serve as temporary credentials, providing fine-grained control over user permissions and channel access.

The core components of Agora.io's authentication involve an App ID and an App Certificate, both obtained from the Agora.io Console. The App ID identifies your application, while the App Certificate is a secret key used to generate secure tokens. For production environments, it is strongly recommended to generate these tokens on a secure server rather than directly within client applications. This server-side approach minimizes the risk of exposing sensitive credentials and allows for robust access control logic to be implemented before a client receives a token.

When a client application needs to join an Agora.io channel, it first requests a token from your application server. The server, using the App ID, App Certificate, channel name, and user ID, generates a unique token. This token is then returned to the client, which uses it to authenticate with the Agora.io service. This process ensures that even if a client's token is compromised, its validity is limited by its expiration time and scope, reducing potential security risks. Detailed steps for token generation are available in the Agora authentication flow documentation.

Supported authentication methods

Agora.io primarily supports token-based authentication, which provides a secure and flexible method for controlling access to real-time communication channels. While a basic App ID mode exists for testing, tokens are the standard and recommended approach for any production deployment due to their enhanced security features.

Token-based authentication is the most secure method. It involves generating a dynamic key, or token, on a trusted server, which is then passed to the client. This token grants temporary access to specific Agora.io features, such as joining a particular channel with a specified user ID. Tokens can be configured with an expiration time, channel name, and user ID, providing granular control and limiting the impact of a compromised token. The process typically involves:

  1. Your client application requests a token from your application server.
  2. Your server uses your Agora.io App ID and App Certificate to generate a token for the specified channel and user.
  3. The server sends the token back to the client.
  4. The client uses the token to join the Agora.io channel.

App ID authentication is a simpler method primarily intended for development and testing. In this mode, clients use only the App ID to connect to Agora.io services. This method offers less security because the App ID is exposed directly in the client application, making it vulnerable to unauthorized use if captured. It is not recommended for production environments where security and access control are critical.

The choice between these methods depends on your application's security requirements. For any deployed application, token-based authentication is essential to protect your communication channels and user data. The Agora token use cases explain these scenarios in more detail.

Authentication Method Comparison

Method When to Use Security Level
Token-based Authentication (recommended) All production environments; requires server-side token generation. High: Tokens are temporary, server-generated, and provide granular access control. Protects App Certificate.
App ID Authentication (for testing only) Development and testing phases; not suitable for production. Low: App ID is exposed client-side, making channels vulnerable to unauthorized access.

Getting your credentials

To begin using Agora.io services, you will need to obtain your App ID and App Certificate from the Agora.io Console. These credentials are fundamental for authenticating your applications and generating secure tokens.

  1. Create an Agora.io Account: If you don't already have one, register for an account on the Agora.io Console.
  2. Create a Project: After logging in, navigate to the Project Management section. Click 'Create' to set up a new project. You will need to provide a project name.
  3. Retrieve App ID and App Certificate: Once your project is created, its details page will display your unique App ID. To retrieve your App Certificate, you might need to enable it for your project if it's not already active. This is typically done by clicking an 'Edit' or 'Configure' button next to the App ID and selecting 'Enable App Certificate' or similar option to generate one. The App Certificate is a string of characters that acts as a secret key. It's crucial to keep this secure. Make sure to copy both the App ID and App Certificate, as the App Certificate will only be displayed once for security reasons. If lost, you might need to regenerate it, which invalidates previous certificates.

For server-side token generation, your application server will need access to both the App ID and App Certificate. Ensure these are stored securely, for example, as environment variables or in a secure configuration management system, and never hardcoded into your client-side applications. The Agora getting started guide provides specific steps for different platforms.

Authenticated request example

While Agora.io's primary interactions are through SDKs, the core of authenticated access relies on a server-generated token. Here's a conceptual example of how a server-side token generation might look in Python, followed by how a client would use this token via an Agora.io SDK. This example focuses on the server-side logic for generating a token, which is then used by the client to authenticate with Agora.io services.

Server-Side Token Generation (Python)

This Python example demonstrates how an application server would generate an Agora.io token using the RtcTokenBuilder from the Agora.io Python library. This token includes necessary information like the App ID, App Certificate, channel name, user ID, and an expiration timestamp.


import time
from agora_token_builder import RtcTokenBuilder, RtmTokenBuilder # Assuming these are installed

# Replace with your actual credentials
APP_ID = "YOUR_AGORA_APP_ID"
APP_CERTIFICATE = "YOUR_AGORA_APP_CERTIFICATE"

# Channel and User Info
CHANNEL_NAME = "testChannel"
UID = 0  # 0 for integer uid, or "username" for string uid
ROLE_RTC = RtcTokenBuilder.Role_Publisher

# Token Expiration Time
TOKEN_LIFE_TIME_IN_SECONDS = 3600  # 1 hour
current_timestamp = int(time.time())
expiration_timestamp = current_timestamp + TOKEN_LIFE_TIME_IN_SECONDS

def generate_agora_token():
    token = RtcTokenBuilder.buildTokenWithUid(
        APP_ID,
        APP_CERTIFICATE,
        CHANNEL_NAME,
        UID,
        ROLE_RTC,
        expiration_timestamp
    )
    return token

# Example usage in a web framework (e.g., Flask/Django endpoint)
# @app.route('/agora/token')
# def get_agora_token_endpoint():
#     token = generate_agora_token()
#     return jsonify({"token": token})

print(f"Generated Agora.io Token: {generate_agora_token()}")

Client-Side Usage (JavaScript with Web SDK)

Once the client receives the token from the application server, it uses the Agora.io Web SDK to join a channel. The client.join() method accepts the App ID, channel name, user ID, and the generated token as parameters.


// Assuming AgoraRTC client is initialized
const client = AgoraRTC.createClient({ mode: "rtc", codec: "vp8" });

const APP_ID = "YOUR_AGORA_APP_ID"; // Still needed for client initialization
const CHANNEL_NAME = "testChannel";
const UID = null; // Use null for Agora to assign UID, or specify one

// This token would be fetched from your server
const serverFetchedToken = "YOUR_SERVER_GENERATED_TOKEN"; 

async function joinChannel() {
  try {
    // Join the channel with the server-generated token
    await client.join(APP_ID, CHANNEL_NAME, serverFetchedToken, UID);
    console.log("Joined channel successfully with token!");
    // Publish local tracks, subscribe to remote tracks, etc.
  } catch (error) {
    console.error("Failed to join channel with token:", error);
  }
}

joinChannel();

This two-step process—server-side token generation followed by client-side token usage—is critical for maintaining security in Agora.io applications. Further details on setting up your Agora project and integrating SDKs are available in the Agora developer documentation.

Security best practices

Implementing strong authentication practices is vital for securing your Agora.io applications and protecting your users' privacy. Adhering to these best practices will help mitigate common security risks associated with real-time communication platforms.

  • Always use Token-based Authentication: For any production deployment, enable and use tokens. Never rely solely on the App ID for authentication in live applications, as this exposes your project to unauthorized access. The Agora official documentation on token security emphasizes this point.
  • Generate Tokens Server-Side: Your application server should be responsible for generating Agora.io tokens. This ensures that your App Certificate remains confidential and is never exposed in client-side code (web, mobile, desktop). Server-side generation allows you to implement additional authorization logic before issuing a token.
  • Set Short Token Expiration Times: Configure tokens to expire after a minimal, appropriate duration. This reduces the window of opportunity for a compromised token to be exploited. For example, if a call is expected to last 30 minutes, generate a token valid for 45-60 minutes.
  • Use Specific Channel Names and User IDs: When generating tokens, specify the exact channel name and user ID that the token is valid for. Avoid generating generic tokens that can be used across multiple channels or by any user.
  • Secure Your App Certificate: Treat your Agora.io App Certificate as a highly sensitive secret. Store it securely on your server, ideally using environment variables or a secrets management service (e.g., AWS Secrets Manager, Google Secret Manager), rather than hardcoding it into your codebase. Rotate your App Certificate periodically as a precautionary measure.
  • Implement Rate Limiting for Token Requests: Protect your token generation endpoint on your application server with rate limiting to prevent abuse and denial-of-service attacks. If an attacker attempts to rapidly request tokens, rate limiting can block their access. Consult common web security guidelines, such as those provided by Cloudflare's rate limiting documentation, for implementation strategies.
  • Validate User Identity Before Issuing Tokens: Before your server generates and issues an Agora.io token, always verify the requesting user's identity and authorization within your own application's authentication system. Ensure they have the necessary permissions to access the requested channel.
  • Monitor and Audit Access: Regularly monitor access logs and audit trails provided by Agora.io and your own application logs. Look for unusual patterns or suspicious activity that might indicate unauthorized access attempts.
  • Keep SDKs and Libraries Updated: Ensure your Agora.io SDKs and the token generation libraries on your server are always up-to-date. Updates often include critical security patches and performance improvements.