Create and publish an API with EventBridge as the backend service in API Gateway, and then call the API by using AppCode authentication.
Overview
Complete the following steps in sequence to create and publish an API with EventBridge as the backend service:
-
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.
Step 1: Create a backend service
-
Log on to the API Gateway console. In the left-side navigation pane, choose Manage APIs > Backend Services.
-
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.
-
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.
-
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.
-
On the Define Backend Service page, select an event bus that you created in the EventBridge console.
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.
Step 2: Create an API group
-
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 the API group is created, it appears on the API Groups page. Click the group name to go to the Group Details page, where 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.
Step 3: 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.
-
Configure basic information for the API.
Configure basic information for the API, including the API group, name, authentication method, type, and description. 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.
-
Configure request information for the API.
Define how a client, such as a browser, a mobile app, or a business system, sends a request for the API. Specify the Request Type, Protocol, Request Path, HTTP Method, Request Mode, and request parameters, and then click Next. In this example, the Request Mode parameter is set to Pass-through. This means API Gateway passes API requests directly to EventBridge without processing them.
-
Configure backend service information for the API.
Configure the backend service type, URL, and parameter mapping. 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.
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.
-
Define responses for the API.
Configure response information to generate API documentation that helps API callers understand the API. You can define a sample success response and a sample failure response. In this example, this step is skipped. Click Create.
-
Publish the API.
API configurations take effect only after you publish the API to an environment. In this example, the backend service is configured only in the Production environment, so you can publish the API only to the Production environment. To publish the API to another environment, configure the backend service in that environment first. Click Publish in the message that indicates successful API creation, and then follow the on-screen instructions to publish the API to the Production environment.
Step 4: Create an application and grant the application the permissions to call the API
An application is an identity used to call an API. Because the Security Authentication parameter was set to Alibaba Cloud App when you created the API, you must create an application and authorize it to call the API.
-
Create an application.
In the left-side navigation pane of the API Gateway console, choose Call APIs-Apps to create an application. The Alibaba Cloud App authentication method supports two modes: an AppKey and AppSecret pair and an AppCode. In this example, an AppCode is used. For more information, see API authorization.
-
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.
Step 5: Debug the API
API Gateway provides online debugging. Use this feature to verify that an API is correctly configured before you call it from 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, you can enter different values to verify that the API works as expected.
When debugging, make sure that the App Name parameter is set to the authorized application and the debugging environment matches the environment in which the application is authorized. Otherwise, debugging may fail. In this example, Production is selected.
After the debugging succeeds, you can query the event details in your event bus based on the returned event ID.
Step 6: Call the API
You have now created and published the API, created and authorized the application, and debugged the API. You can call the API from your business system by using the AppCode.
For more information about how to call the API, see Call an API using simple authentication (AppCode).