Use the EventBridge SDK for TypeScript to manage resources and publish events programmatically.
SDK types
EventBridge provides two types of SDKs:
| SDK type | Purpose | Supported operations |
|---|---|---|
| Management API SDK | Manage EventBridge resources such as event buses and rules through operations available in the EventBridge console | All management API operations |
| Data API SDK | Publish event data to an event bus | PutEvents only |
Prerequisites
Before you begin, make sure that you have:
Set up your environment
Install Node.js 8.0 or later from the official download page.
Verify the installation:
node -vInstall TypeScript globally:
npm install -g typescript
Management API SDK
Install the SDK
npm install --save @alicloud/eventbridge20200401@1.0.1Generate 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 -SPublish 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:
| Placeholder | Description |
|---|---|
<endpoint> | EventBridge endpoint for your region |
Key fields in the CloudEvent:
| Field | Description |
|---|---|
source | Event source identifier, such as acs.oss for OSS events |
type | Event type that describes what happened |
specversion | CloudEvents specification version (1.0) |
datacontenttype | Media type of the data field (application/json) |
extensions.aliyuneventbusname | Target event bus name (demo-bus in this example) |
ThecreateClientfunction 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, thetypefield 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
Auto-generate management API sample code in OpenAPI Explorer