All Products
Search
Document Center

API Gateway:Create an API with Function Compute as the backend service

Last Updated:Nov 08, 2023

This topic describes how to create and publish an API with Function Compute as the backend service in API Gateway and how to call the API by using an authorized application and the AppCode that is automatically generated for the application if you set the authentication method of the API to Alibaba Cloud App.

1. Overview

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

  • Create a function

  • Create an API group

  • Create an API

  • Create an application and authorize the application to call the API

  • Debug the API

  • Call the API

2. Create a function

Function Compute is an event-driven service. The execution of functions can be triggered by events. API Gateway is supported as an event source for Function Compute. After API Gateway receives a request for an API that uses Function Compute as the backend service, API Gateway triggers the execution of the corresponding function in Function Compute and Function Compute sends the execution result to API Gateway.

Step 1: Create a function.

Log on to the Function Compute console and select the region where you want to create a function. For detailed steps to create a function, see Create a function in the Function Compute console.

3. Create an API group

APIs are managed in API groups. Before you create an API, you must create an API group.

Step 2: Create an API group.

Log on to the API Gateway console. In the left-side navigation pane, choose Open API > API Groups. In the top navigation bar, select the region where you want to create an API group and click Create Group on the API Groups page. In the Create Group dialog box, select the instance to which the API group that you want to create belongs and enter the group name. Note: The API group must reside in the same region as the function that you created in the Function Compute console.

image

Step 3: View the details of the API group.

After an API group is created, it appears on the API Groups page. You can click the group name to go to the Group Details page. On the Group Details page, you can perform operations such as binding a domain name, modifying basic information, and changing the instance type.

API Gateway automatically assigns a public second-level domain name to a created API group. The domain name is used only to debug and test APIs. The number of API calls that you can initiate to access the domain name is limited to 100 calls per day in the China (Hong Kong) region and regions outside the Chinese mainland, and 1,000 calls per day in regions inside the Chinese mainland. We recommend that you bind an independent domain name after you create an API group. In this example, the default public second-level domain name is used.

4. Create an API

In the left-side navigation pane of the API Gateway console, choose Open API > APIs. In the top navigation bar, select the region where the API group you created in Step 3 resides. Then, click Create API.

Step 4: Configure basic information for the API.

In this step, configure basic information for the API that you want to create, including the API group to which the API belongs and the name, authentication method, type, and description of the API. Select the group that you created and configure the corresponding parameters.

image

Step 5: Configure request information for the API.

In this step, define how a client, such as a web browser, a mobile app, or a business system, requests the API. The parameters that you need to configure in this step include 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, which indicates that API Gateway does not process API requests and passes them directly to Function Compute.

image

Step 6: Configure backend service information for the API.

In this step, specify the type and URL of the backend service to which API Gateway sends the requests that are received from a client and how parameters are mapped and processed. In this example, the Backend Service Type parameter is set to Function Compute. After other information, such as the name and service name of the function that you created in Step 1, is also specified, click Next.

image

Step 7: Configure response information for the API.

In this step, specify the response information to generate API documentation. The documentation helps API callers better understand the API. You can configure the Response ContentType, Response Example, and Error Response Example parameters. In this example, this step is skipped. Click Create.

Step 8: Publish the API.

After you click Create, a dialog box appears to inform you that the API is created. The following figure shows the dialog box. API Gateway provides the following environments to which you can publish an API: Production, Pre, and Test. An API takes effect only after you publish the API to a required environment. In this example, Publish is clicked in the dialog box, and Production is selected as the environment to which the API is published.

image

5. Create and authorize an application

Applications are the identities that can be used to call APIs. In Step 4, 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.

Step 9: Create an application.

In the left-side navigation pane of the API Gateway console, choose Call API > Apps. On the Apps page, click Create App. In the Create App dialog box, enter an application name and click Confirm. In the application list, click the name of the created application. The following authentication methods are provided: an AppKey and AppSecret pair and an AppCode. In this example, the AppCode method is used to authenticate the application. For more information, see API authorization.

image

Step 10: Authorize the application to call the API.

In the left-side navigation pane of the API Gateway console, choose Open API > APIs. On the APIs page, find the API that you created and choose More > Authorize in the Actions column. The following dialog box appears. Set the Stage parameter to the environment to which you published the API. In this example, the Stage parameter is set to Production. Enter the name of the application you created in the search box to the right of the Choose Apps for Authorization parameter. In the search result, select the application that you created, 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.

image

6. 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 the API from a client.

Step 11: Debug the API.

On the APIs page of the API Gateway console, click the name of the API that you created. On the API details page, click Debug API in the left-side navigation pane. The following figure shows the page that appears. If you defined request parameters for the API, you can enter different values for the request parameters to check whether the API is correctly configured.

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.

image

The API response is the execution result of the function that you created in the Function Compute console. The following figure shows the code of the function.

7. Call the API

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

Step 12: Call the API.

For information about how to call an API, see Call an API in simple authentication mode. In this example, curl is used to call the API.

The following figure shows the result of an API call.image.png