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

1. Overview

You must perform the following steps in sequence:

  • Create a function in Function Compute
  • Create an API group in API Gateway
  • Create an API operation
  • Create and authorize an application
  • Debug the API operation
  • Call the API operation

2. Create a function in Function Compute

Function Compute is an event-driven compute service. Functions are event-driven, that is, when an event occurs, the event triggers the execution of the corresponding function. API Gateway is an event source for Function Compute. After API Gateway receives a request for an API operation 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 results to API Gateway.

Step 1: Create a function by using the apigateway template in Function Compute

Log on to the Function Compute console. Select a region in the top navigation bar. In the left-side navigation pane, click Service/Function. On the page that appears, click Create Function. In the Create Function step, click Template Function. Then, select the api-gateway-nodejs6 template and click Next. This template is used to create a function whose event source is API Gateway.

Step 2: Configure the function

For more information about how to configure a function, see Function Compute documentation. In this example, set the parameters as required, as shown in the following figure.

3. Create an API group in API Gateway

In API Gateway, API operations are managed in API groups. Before you create an API operation, you must create an API group.

Step 3: Create an API group in API Gateway

Log on to the API Gateway console. In the left-side navigation pane, choose Publish APIs > API Groups. Select a region in the top navigation bar and click Create Group on the Group List page. In the Create Group dialog box, select an instance to which the API group to be created belongs and enter a group name. In this example, set the Instance parameter to Shared Instance(VPC Network)(api-shared-vpc-001) and enter testFunctionGroup in the Group Name field. The API group must be in the same region as the function that you created in Function Compute.

Step 4: View details of the API group

After you create the API group, the API group appears on the Group List page. You can click the group name to go to the details page, where you can perform operations such as binding a domain name, modifying basic information, and changing the instance type.

After an API group is created, API Gateway automatically creates a public second-level domain name for the API group. This default second-level domain name can only be used for testing API calls and can be used for a maximum of 1,000 times per day. We recommend that you bind an independent domain name after you create an API group. In this example, the default second-level domain name for testing API calls is used.

4. Create an API operation

In the left-side navigation pane, choose Publish APIs > APIs. Make sure that the current region is the same region where the API group you created resides. On the API List page, click Create API.

Step 5: Configure basic information for the API operation

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

Step 6: Configure request information for the API operation

In this step, define how a client, such as a browser, a mobile app, or a business system, sends a request for the API operation. The parameters to be set in this step include Request Type, Protocol, Request Path, HTTP Method, Request Mode, and those in the Input Parameter Definition section. In this example, set the Request Mode parameter to Request Parameter Passthrough, which indicates that API Gateway does not process API requests and pass them directly to Function Compute.

Step 7: Configure backend service information for the API operation

In this step, configure a backend service type and address for the API operation and the mapping relationship between request and response parameters. In this example, set the Backend Service Type parameter to FunctionCompute. Configure other information, such as the name and service name of the function that you created in Function Compute, and click Get Authorization. Then, click Next.

Step 8: Configure response information for the API operation

In this step, configure response information to generate an API reference in Alibaba Cloud API Gateway SDK. This API reference can help API users better understand the API operation. You can set parameters such as ContentType of Response, Sample of Returned Results, and Sample of Returned Failure. In this example, this step is skipped. Click Create.

Step 9: Publish the API operation

After you create or modify the API operation, a message appears to inform you that the API operation is created or modified. All configurations of the API operation take effect only after you publish the API operation. API Gateway provides three environments to which you can publish an API operation: Release, Pre, and Test. In this example, click Deploy in the message. In the dialog box that appears, set the Select The Stage To Release To parameter to Release, enter your remarks, and then click Deploy.

5. Create and authorize an application

Applications are identities that you use to call API operations. In step 5, the Security Certification parameter is set to Alibaba Cloud APP. Therefore, after you publish the API operation, you must create and authorize an application for calling the API operation.

Step 10: Create an application

In the left-side navigation pane, choose Consume APIs > APPs. On the APP List page, click Create APP. As shown in the following figure, for an API operation whose Security Certification parameter is set to Alibaba Cloud APP, two authentication modes are provided for its applications: AppKey and AppCode. In this example, the AppCode mode is used to authenticate the application. For more information about the Alibaba Cloud APP authentication method, see Call an API operation by using an AppCode.

Step 11: Authorize the application

In the left-side navigation pane, choose Publish APIs > APIs. On the API List page, find the API operation you created and click Authorize in the Operation column. A dialog box appears, as shown in the following figure. Set the Select The Stage For Authorization parameter to the environment to which you have published the API operation. In this example, set this parameter to Release. Search for the application you created, click Add, and then click OK. A message appears to inform you that the application is authorized to call the API operation.

6. Debug the API operation

API Gateway supports online debugging. We recommend that you use this feature to check whether an API operation is correctly configured before you allow it to be called on clients.

Step 12: Debug the API operation

On the API List page, find the API operation you created and click Debug in the Operation column. A page appears, as shown in the following figure. If you have defined request parameters for the API operation, you can enter different values for the request parameters to check whether the API operation is correctly configured.

When you debug the API operation, make sure that the AppName parameter is set to an authorized application. The Stage parameter must be set to the environment where the application is authorized, otherwise the debugging may fail. In this example, set the Stage parameter to RELEASE.

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

7. Call the API operation

Now you have created the API operation and the application, authorized the application to call the API operation, debugged the API operation, and published the API operation to the online environment. In this step, you can call the API operation in your business system by using the AppCode.

Step 13: Call the API operation

For more information about how to call an API operation, see Call an API operation by using an AppCode. In this example, curl is used to call the API operation.

The following figure shows a sample response.