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 in an app by using an AppCode. The AppCode is automatically generated for the app when you set the authentication method of the API to Alibaba Cloud App.
You must perform the following steps in sequence:
Create a function in Function Compute
Create an API group in API Gateway
Create an API
Create an app and authorize the app to call the API
Debug the API
Call the API
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 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
Services & Functions. On the Services page, select a service or create one. 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
APIs are managed in API groups. Before you create an API, 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
Open API >
API Groups. Select a region in the top navigation bar and click Create Group on the API Groups 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 Instances parameter to Shared Instance(VPC Network)(api-shared-vpc-002) 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 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.
4. Create an API
In the left-side navigation pane, choose
Open API >
APIs. Make sure that the current region is the same region where the API group you created resides. On the APIs page, click Create API.
Step 5: Configure basic information for the API
In this step, configure the 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, set the Group parameter to the API group you created and the AppCode Authentication parameter to Enable AppCode Authentication (Header & Query). Set other parameters as required and click Next.
Step 6: 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, set the Request Mode parameter to Pass-through, 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.
In this step, configure the type and URL of the backend service to which API Gateway sends the requests received from a client and how parameters are mapped and processed. 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. Then, click Next.
Step 8: Configure response information for the API
In this step, configure response information to generate API documentation. The documentation helps API callers better understand APIs. You can set parameters such as Response ContentType, Response Example, and Error Response Example. In this example, this step is skipped. Click
Step 9: Publish the API
After you click Create, a message appears to inform you that the API is created, as shown in the following figure. API Gateway provides three environments to which you can publish an API: Production, Pre, and Test. All configurations you perform on an API can take effect only after you publish the API to a required environment. In this example, click Publish in the message that indicates successful API creation. In the Publish API dialog box, set the Environment parameter to Production, enter remarks, and then click Publish.
5. Create and authorize an app
Apps are the identities that you use to call APIs. In Step 5, the Security Authentication parameter is set to Alibaba Cloud App. Therefore, after you publish the API, you must create an app and grant the app the permissions to call the API.
Step 10: Create an app
In the left-side navigation pane, choose
Call API >
Apps. On the Apps page, click Create App. In the Create App dialog box, enter an app name and click Confirm. In the app list, click the name of the created app. Two authentication modes are provided: an AppKey and AppSecret pair and AppCode. In this example, the AppCode mode is used to authenticate the app. For more information about the security authentication method Alibaba Cloud App, see Authorize an application to call an API operation.
Step 11: Grant the app the permissions to call the API
In the left-side navigation pane, choose Open API >
APIs. On the APIs page, find the created API and choose More >
Authorize in the Actions column. A dialog box appears, as shown in the following figure. 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 app you created in the search box of the Choose Apps for Authorization section. In the search result, select the created app, click Add in the Actions column, and then click Confirm. A message appears to inform you that the app is authorized to call the API.
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 this API on clients.
Step 12: Debug the API
APIs page, click the name of the created API. On the API details page, click
Debug API in the left-side navigation pane. The following figure shows the page that appears. If you have 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 the API, make sure that the App Name parameter is set to the authorized app. The environment for debugging must be the one in which the app is authorized to call the API. Otherwise, the debugging may fail. In this example, Production is selected as the environment for debugging.
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
After you perform the preceding steps, you have created the API and the app, authorized the app 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 AppCode.
Step 13: Call the API
For more information about how to call the API, see Call an API operation by using an AppCode. In this example, curl is used to call the API.