All Products
Search
Document Center

API Gateway:EventBridge

Last Updated:Apr 15, 2024

This topic describes how to create and publish an API with EventBridge as the backend service in API Gateway. This topic also describes how to call the API by using the Alibaba Cloud App authentication method and using the AppCode of an application for authentication.

Overview

To create and publish an API with EventBridge as the backend service, you must perform the following steps in sequence:

  • Create a backend service

  • Create an API group

  • Create an API

  • Create an application and grant the application the permissions to call the API

  • Debug the API

  • Call the API

Prerequisites

The EventBridge service is activated, and a custom event bus is created. For more information, see Manage custom event buses.

Create a backend service

  1. Log on to the API Gateway console. In the left-side navigation pane, choose Manage APIs > Backend Services.

  2. In the upper-right corner of the page, click Create Backend Service. In the dialog box that appears, enter the name of the backend service, set Type to EventBridge, and then click Confirm.

  3. On the Backend Services page, find the backend service that you created and select Configure Backend Service and View Associated APIs in the Actions column.

  4. Click the Production tab and click Create in the upper-right corner of the Basic Information section to configure the backend service in the Production environment. You must configure the backend service in the Production environment before you can publish an API to the Production environment.

  5. On the Define Backend Service page, select an event bus that you created in the EventBridge console.

Note

If the event bus that you select and your API Gateway instance are in the same region, API Gateway pushes an event to your event bus when you call an API by using the internal network. In this example, both the event bus and API Gateway instance reside in the China (Hangzhou) region. After you set the parameters, click Publish.

Create an API group

  1. APIs are managed in API groups. You must create an API group before you create an API.

In the left-side navigation pane of the API Gateway console, choose Manage APIs > API Groups. In the upper-right corner of the page, click Create Group. In the dialog box that appears, select an instance, enter a group name, and set a base path. The API group must reside in the same region as the event bus that you use.

After you create the API group, the API group appears on the API Groups page. You can click the group name to go to the Group Details page. On this page, you can bind a domain name, modify basic information, and change the instance type.

API Gateway automatically assigns a public second-level domain name to the API group. This domain name is for debugging and testing purposes only. If you use this domain name to make API calls, you can make 100 calls per day in regions outside the Chinese mainland and 1,000 calls per day in regions in the Chinese mainland. We recommend that you bind an independent domain name after you create an API group. In this example, the default second-level domain name is used.

Create an API

In the left-side navigation pane of the API Gateway console, choose Manage APIs-APIs. In the upper-right corner of the page, click Create API.

  1. Configure basic information for the API

In this step, configure basic information for the API to be created, including the API group to which the API belongs and the name, authentication method, type, and description of the API. In this example, the Group parameter is set to the created API group and the AppCode Authentication parameter is set to Enable AppCode Authentication (Header & Query). Set other parameters as required and click Next.

  1. Configure request information for the API

In this step, define how a client, such as a browser, a mobile app, or a business system, sends a request for the API. The parameters that you need to specify in this step include Request Type, Protocol, Request Path, HTTP Method, Request Mode, and the parameters in the Request Parameters section. Then, click Next. In this example, the Request Mode parameter is set to Pass-through. This option indicates that API Gateway directly passes API requests to EventBridge without processing the requests.

  1. Configure backend service information for the API

In this step, configure the type and URL of the backend service to which API Gateway sends requests received from a client and how parameters are mapped and processed. In this example, Use Existing Backend Service is selected for Configuration Mode, EventBridge is selected for Backend Service Type, and the backend service that you created is selected from the Backend Service drop-down list. Click Next.

Note

You can only select an existing backend service when you create an API with EventBridge as the backend service. You cannot customize the backend service.

  1. Define responses for the API

In this step, configure response information to generate API documentation. The documentation helps API callers better understand APIs. You can define a sample success response and a sample failure response. In this example, this step is skipped. Click Create.

  1. Publish the API

All configurations you perform on an API can take effect only after you publish the API to an environment. In this example, the backend service is configured only in the Production environment. Therefore, you can publish the API only to the Production environment. If you want to publish the API to another environment, you must configure the backend service in the environment in advance. Click Publish in the message that indicates successful API creation. Then follow the on-screen instructions to publish the API to the Production environment.

Create an application and grant the application the permissions to call the API

An application is an identity that you use to call an API. In the preceding step for creating the API, the Security Authentication parameter is set to Alibaba Cloud App. After you publish the API, you must create an application and grant the application the permissions to call the API.

  1. Create an application

In the left-side navigation pane of the API Gateway console, choose Call APIs-Apps to create an application. Two authentication modes are provided for the security authentication method Alibaba Cloud App: an AppKey and AppSecret pair and an AppCode. In this example, an AppCode is used. For more information about the security authentication method Alibaba Cloud App, see API authorization.

  1. Grant the application the permissions to call the API

On the APIs page, find the created API and click Authorize in the Actions column. Set the Stage parameter to the environment to which you have published the API. In this example, the Stage parameter is set to Production. Enter the name of the application that you created in the search box of the Choose Apps for Authorization section. In the search result, select the created application, click Add in the Actions column, and then click Confirm. A message appears to inform you that the application is authorized to call the API.

Debug the API

API Gateway supports online debugging. We recommend that you use this feature to check whether an API is correctly configured before you call this API on clients.

On the APIs page, click the name of the created API. On the API details page, click Debug API in the left-side navigation tree. If you defined request parameters for the API, you can enter different values for the request parameters to check whether the API works as expected.

When you debug an API, make sure that the App Name parameter is set to the authorized application. The environment for debugging must be the one in which the application is authorized to call the API. Otherwise, debugging may fail. In this example, Production is selected as the environment for debugging.

After the debugging succeeds, you can query the event details in your event bus based on the returned event ID.

Call the API

After you perform the preceding steps, you have created the API and the application, authorized the application to call the API, debugged the API, and published the API to the Production environment. In this step, you can call the API in your business system by using the application AppCode.

For more information about how to call the API, see Call an API in simple authentication mode.