Skip to main content

You are viewing Agora Docs forBeta products and features. Switch to Docs

Media Gateway quickstart

Media Gateway enables users to push content to Agora SD-RTN™ using standard streaming protocols such as RTMP and SRT. The channel host automatically publishes this streamed content to the correct channel. Possible use cases are:

  • Enrich the viewing experience with commentary: Seamlessly inject high-definition video source streams into interactive channels to enhance the viewing experience. Multiple anchors from different locations watch and interact in real-time, adding depth to the media with their commentary.

  • Low-latency distribution scenarios: Distribute content to viewers in a faster and more stable way than using a Content Deliver Network (CDN). In scenarios such as online auctions and bidding platforms, bidders receive a real-time video feed enabling them to swiftly place their bids.

  • Recording to live broadcast: Enhance content quality and reduce live production complexity by injecting prerecorded media into a live broadcast. For example, a teacher records aspects of a lesson in advance and adds it to a live classroom. All students and teaching assistants watch the teacher's lecture, and interact in real-time.

  • Avoid interruptions due to host switching: Background music is played continuously in a chat room without interruptions if the host leaves the channel.

Understand the tech

Using Media Gateway RESTful API, you generate a streaming key. This key allows you to push online media streams as live video source streams into Agora channels.

Flow diagram

With Media Gateway, you can employ advanced transcoding capabilities on media streams for enhanced distribution. To enable and configure transcoding options, you use the Media Gateway RESTful API. When transcoding is enabled, you can set parameters such as resolution, frame rate, bit rate, and whether to enable video streams with high and low resolutions.


To activate the Media Gateway, contact Agora support. When you activate Media Gateway, you specify your access area to optimize the transmission quality of the media stream.


The Media Gateway RESTful API supports the following interfaces:

CreateAcquire a stream key. You use the stream key to Configure, Query and Delete the stream.
ConfigureSet the video and audio transcoding configuration.
By default, the video stream is pushed directly to the Agora channel without transcoding. If you need to transcode, use this interface to set the relevant parameters.
QueryQuery information about stream keys, such as the bound UID, the bound channel name, and validity.
DeleteDestroy a stream key.

To use the Media Gateway RESTful API and obtain the relevant code, refer to the API reference. If you need to push multiple streams to the same channel, use a unique uid to generate each stream key.

The stream key generation method depends on whether you use the Agora domain name or a custom domain name. To use the Agora domain name, create a stream key using the Media Gateway RESTful API.

Generate the stream key locally

When using your own domain name for streaming, the stream key can be generated locally. Contact Agora technical support to bind your custom domain name. To generate a stream key locally, you use the following information:

  • The App Id of the Agora project from Agora Console.
  • The App certificates corresponding to your App Id.
  • A channel name.
  • The user Id of the anchor in the Agora channel.
  • The effective duration (seconds) of the stream keys.

The following Node.js sample code demonstrates how to generate a stream key:

const crypto = require('crypto');
const msgpack = require('msgpack-lite');
appcert = "" // Your App certificate from Agora console
channel = ""; // A Video SDK channel name
uid = ""; // The uid of the anchor in the channel
expiresAfter = 86400; // Valid duration of stream key (seconds)
const expiresAt = Math.floor(Date. now() / 1000) + expiresAfter;
const rtcInfo = {
C: channel,
U: uid,
E: expiresAt,
// Serialize using msgpack
const data = msgpack.encode(rtcInfo);
// Randomly generate an initialization vector
const iv = crypto.randomBytes(16);
// Use App certificate as encryption key
const key = Buffer.from(appcert, 'hex');
// Create an AES-128 CTR encryptor
const encrypter = crypto.createCipheriv('aes-128-ctr', key, iv);
// Encrypt the data
const encrypted = Buffer.concat([iv, encrypter.update(data),]);
// Base64 conversion
const streamkey = encrypted.toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, '');
console.log(`streamkey is ${streamkey}`);

Test Media Gateway

This section shows you how to test Agora Media Gateway using OBS Studio, a free and open-source, cross-platform streaming app. Take the following steps to start streaming using OBS Studio:

  1. Download and install OBS studio.

  2. From the main screen, select Settings and switch to the Stream tab.

  3. Fill in the following information:

    Server settings

    • Service: From the dropdown list, select Custom...

    • Server: Enter the scheme rtmp://, followed by the unified Agora domain name, or your own domain name. The unified Agora domain name is rtls-ingress-prod-<region> Replace <region> with the code for your geographical region. You also need to append /live after the domain name.

      For example: rtmp://

      The supported regions and their codes are:

      • na: North America
      • eu: Europe
      • ap: Asia, except Mainland China
      • cn: Mainland China

      To use your own domain name, please contact Agora technical support for configuration.

    • Stream key: A valid stream key, acquired using the Media Gateway RESTful API or generated locally, that corresponds to your server domain name.

  4. Set streaming options to avoid B-frames in source streams:

    To avoid compatibility issues with Agora viewer on the web when transcoding is not enabled, and the source stream contains B-frames, use one of the following methods in OBS studio:

    • Add a x264 encoding parameter

      In OBS Studio, go to Settings > Output > Encoder Settings, and set the x264 Options to bframes=0

      zero B frames

    • Add a Tune parameter

      In OBS Studio, go to Settings > Output > Encoder Settings, and set the Tune parameter to zerolatency

      Zero latency

    Save the configuration and return to the main screen.

  5. Under Sources, press + to add a new source then select Media Source from the menu. Type in a name for your source and Browse to select a video file.

  6. Generate a temporary token in Agora Console for the same channel name you used to generate the stream key but use a different uid.

  7. In your browser, navigate to the Agora web demo and update App ID, Channel, and Token with the values for your temporary token, then click Join.

  8. In OBS Studio, press Start Streaming to begin streaming to your Agora Channel.

    You see a video stream displayed in the channel, with the uid you used to generate the stream key.

  9. Use the Configure, Query, and Delete Media Gateway RESTful API interfaces to manage your stream.


This section contains information that completes the information in this page, or points you to documentation that explains other aspects to this product.


Refer to the Media Gateway RESTful API for parameter details and to obtain the calling code for your platform.

Supported formats and protocosl

Media Gateway supports the following media streaming formats and protocols:

  • Video codec: H.264, H.265
  • Audio codec: AAC, OPUS
  • Streaming protocol: RTMP, SRT
  • Media format: FLV, MPEG-TS