All Products
Search
Document Center

EventBridge:EventBridge SDK for TypeScript

Last Updated:Mar 11, 2026

Use the EventBridge SDK for TypeScript to manage resources and publish events programmatically.

SDK types

EventBridge provides two types of SDKs:

SDK typePurposeSupported operations
Management API SDKManage EventBridge resources such as event buses and rules through operations available in the EventBridge consoleAll management API operations
Data API SDKPublish event data to an event busPutEvents only

Prerequisites

Before you begin, make sure that you have:

Set up your environment

  1. Install Node.js 8.0 or later from the official download page.

  2. Verify the installation:

       node -v
  3. Install TypeScript globally:

       npm install -g typescript

Management API SDK

Install the SDK

npm install --save @alicloud/eventbridge20200401@1.0.1

Generate sample code

Use OpenAPI Explorer to auto-generate sample code for any management API operation. For details, see Automatic generation of SDK examples.

Data API SDK

Install the SDK

Install the EventBridge data SDK and the output library:

npm install @alicloud/eventbridge -S
npm install @alicloud/tea-console -S

Publish events with PutEvents

The Data API SDK supports only the PutEvents operation. The following example initializes a client, constructs a CloudEvent, and publishes it to an event bus.

import EventBridge, * as $EventBridge from '@alicloud/eventbridge';
import Util from '@alicloud/tea-util';
import Console from '@alicloud/tea-console';
import * as $tea from '@alicloud/tea-typescript';

export default class Client {

    /**
     * Initialize the EventBridge client.
     * Reads credentials from environment variables ALIBABA_CLOUD_ACCESS_KEY_ID
     * and ALIBABA_CLOUD_ACCESS_KEY_SECRET.
     */
    static async createClient(): Promise<EventBridge> {
        let config = new $EventBridge.Config({ });
        SetAccessKeyId(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")).
        SetAccessKeySecret(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")).
        SetEndpoint("<endpoint>")
        return new EventBridge(config);
    }

    /**
     * Build a CloudEvent and publish it to the specified event bus.
     */
    static async PutEvents(client: EventBridge): Promise<void> {
        let event = new $EventBridge.CloudEvent({ });
        event.datacontenttype = "application/json";
        event.data = Util.toBytes("test");
        event.id = "a5074581-7e74-4e4c-868f-47e7afdf8445";
        event.source = "acs.oss";
        event.specversion = "1.0";
        event.type = "oss:ObjectCreated:PostObject";
        event.time = "2020-08-24T13:54:05.965Asia/Shanghai";
        event.subject = "1.0";
        event.type = "acs:oss:cn-hangzhou:1234567:xls-papk/game_apk/123.jpg";
        event.extensions = {
            aliyuneventbusname: "demo-bus",
        };
        try {
            let resp = await client.putEvents([
                event
            ]);
            Console.log("--------------------Publish event to the aliyun EventBus--------------------");
            Console.log(Util.toJSONString($tea.toMap(resp)));
        } catch (error) {
            Console.log(error.message);
        }
    }

    static async main(args: string[]): Promise<void> {
        let client = await Client.createClient();
        await Client.PutEvents(client);
    }

}

Client.main(process.argv.slice(2));

Replace the following placeholder with your actual value:

PlaceholderDescription
<endpoint>EventBridge endpoint for your region

Key fields in the CloudEvent:

FieldDescription
sourceEvent source identifier, such as acs.oss for OSS events
typeEvent type that describes what happened
specversionCloudEvents specification version (1.0)
datacontenttypeMedia type of the data field (application/json)
extensions.aliyuneventbusnameTarget event bus name (demo-bus in this example)
The createClient function in this example uses Go-style syntax (SetAccessKeyId, os.Getenv). [To be confirmed: the correct TypeScript syntax for setting AccessKey credentials in the EventBridge SDK config object. Refer to the latest SDK documentation for the proper TypeScript initialization pattern.]
In this example, the type field is assigned twice. The second assignment (acs:oss:cn-hangzhou:1234567:xls-papk/game_apk/123.jpg) overwrites the first (oss:ObjectCreated:PostObject). [To be confirmed: which value is the correct event type for this example.]

What's next