All Products
Search
Document Center

EventBridge:EventBridge SDK for Go

Last Updated:Mar 11, 2026

EventBridge provides two Go SDKs:

  • Management API SDK -- Perform operations on the EventBridge console programmatically.

  • Data API SDK -- Publish events to an event bus through the PutEvents operation.

Prerequisites

Before you begin, make sure that you have:

Verify your Go version:

go version

Install the SDK

Enable Go modules, configure the Go module proxy, and initialize your project:

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
go mod init

Then install the package for your use case:

SDK typeInstall command
Management APIgo get github.com/alibabacloud-go/eventbridge-20200401/v2
Data APIgo get github.com/alibabacloud-go/eventbridge-sdk

(Optional) Install the output library for formatted console output:

go get github.com/alibabacloud/tea-console

Publish events with the data API SDK

The data API SDK supports only the PutEvents operation.

Sample code

This example publishes a single event to an event bus:

package main

import (
	"fmt"
	"os"

	eventbridge "github.com/alibabacloud-go/eventbridge-sdk/eventbridge"
)

func main() {
	// Configure the client with credentials from environment variables.
	config := new(eventbridge.Config).
		SetAccessKeyId(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")).
		SetAccessKeySecret(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")).
		SetEndpoint("<endpoint>")

	client, err := eventbridge.NewClient(config)
	if err != nil {
		panic(err)
	}

	// Build a CloudEvent with the required attributes.
	event := new(eventbridge.CloudEvent).
		SetDatacontenttype("application/json").
		SetData([]byte("test")).
		SetId("id").
		SetSource("source").
		SetTime("2020-08-24T13:54:05.965Asia/Shanghai").
		SetSubject("1.0").
		SetType("type").
		SetExtensions(map[string]interface{}{
			"aliyuneventbusname": "BusName",
		})

	// Publish the event.
	resp, err := client.PutEvents([]*eventbridge.CloudEvent{event})
	if err != nil {
		panic(err)
	}
	fmt.Println(resp)
}

Set the following environment variables before you run the code:

Environment variableDescription
ALIBABA_CLOUD_ACCESS_KEY_IDYour AccessKey ID
ALIBABA_CLOUD_ACCESS_KEY_SECRETYour AccessKey secret

In the code, replace the following placeholder values:

PlaceholderDescriptionExample
<endpoint>EventBridge endpoint for your region<account-id>.eventbridge.<region-id>.aliyuncs.com
BusNameName of the target event busmy-event-bus
idUnique identifier for the eventabc-1234
sourceEvent producer identitymyapp.orders
typeEvent type descriptororder.created
The sample code uses panic(err) for brevity. In production, implement proper error handling, including retry logic for transient failures.

CloudEvent attributes

Each event requires the following attributes:

AttributeMethodDescription
idSetIdUnique identifier for the event
sourceSetSourceEvent producer identity
typeSetTypeEvent type descriptor
timeSetTimeTimestamp of the event
datacontenttypeSetDatacontenttypeMedia type of the data field (for example, application/json)
dataSetDataEvent payload as a byte slice
subjectSetSubjectSubject of the event in context of the producer
extensionsSetExtensionsCustom extension attributes. Use the aliyuneventbusname key to specify the target event bus

Use the management API SDK

The management API SDK provides programmatic access to EventBridge console operations.

After installation, generate sample code for any management API operation through OpenAPI Explorer. For details, see Automatic generation of SDK examples.