This topic provides a quick guide on how to create an API in API Gateway and call the API to invoke a web function in Function Compute. In the call, the AppCode of an application is used according to the Alibaba Cloud App authentication method.
Process
You must perform the following steps in sequence:
Create a web function
Create a web function in Function Compute. For more information, see Create a web function.
ImportantWhen you create the web function, take note that the function must reside in the same region as your API Gateway instance, Runtime must be set to Java 8, and Code Upload Method must be set to Use Sample Code.
On the Functions page of the Function Compute console, click the created function to go to its details page. Click the Configurations tab. In the left-side navigation tree, click Triggers. Find the trigger that you want to use and copy its internal endpoint
https://***-helloworld-**.cn-hangzhou.fcapp.run
in the Configurations column.
Create a backend service
API Gateway allows you to configure backend services as resources of a region to simplify management. You need only to create a backend service in the API Gateway console and configure different URLs for the backend service for different environments. Then, you can directly select the configured backend service to create an API.
Log on to the API Gateway console. In the left-side navigation pane, choose Manage APIs > Backend Services. On the page that appears, click Create Backend Service in the upper-right corner.
In the Create Backend Service dialog box, set Name to
FC-backend
, Type toFunction Compute
, Version toFunction Compute V3.0
, and Function Type toHTTP Function
. Then, click Confirm.On the Backend Services page, click the created
FC-backend
service. On the page that appears, select the Production tab and then click Create in the upper-right corner.In the Basic Information section of the page that appears, set Trigger Path to
https://***-helloworld-**.cn-hangzhou.fcapp.run
that is copied earlier in the Create a web function step. Then, click Publish.
Create an API group
APIs are managed in API groups. Before you create an API, you must create an API group.
Log on to the API Gateway console. In the left-side navigation pane, choose Manage APIs < API Groups. On the API Groups page, click Create Group in the upper-right corner.
In the Create Group dialog box, select an instance from the Instances drop-down list, set Group Name to
FC-Group
and BasePath to/
, and then click Confirm.NoteThe API group must be in the same region as the Function Compute function.
Create an API
Log on to the API Gateway console. In the left-side navigation pane, choose Manage APIs > API Groups.
On the API Groups page, find the created
FC-test
group and click Manage APIs in the Actions column.On the APIs page, click Create API in the upper-right corner.
In the Basic Information step of the Create API wizard, configure the following parameters and click Next.
Parameter
Example
Group
FC-Group
API Name
FC-test
Security Authentication
Alibaba Cloud App
AppCode Authentication
Enable AppCode Authentication (Header & Query)
Signature Algorithm
HMAC_SHA256
In the Define API Request step, configure the following parameters and click Next.
In the Define Backend Service step, configure the following parameters and click Next.
Parameter
Example
Configuration Mode
Use Existing Backend Service
Backend Service Type
Function Compute
Version
Function Compute V3.0
Function Type
HTTP Function
Backend Service
FC-backend
Backend Request Path
/
HTTP Method
GET
Backend Service Timeout Period
10000
NoteIn this step, you configure 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.
In the Define Response step, configure the parameters on the page based on your business requirements and then click Create.
In the message that appears, click Publish.
In the Publish API dialog box that appears, set Environment to Release, enter remarks in the Remarks section, and then click Publish.
Create an application and authorize the application to call the API
An application is an identity that you use to call an API. In the Create an API step of this example, the authentication method is set to Alibaba Cloud App. Therefore, after the API is published, you must create an application and grant the application the permissions to call the API.
Create an application
Log on to the API Gateway console. In the top navigation bar, select a region. In the left-side navigation pane, choose Call APIs > Apps.
On the Apps page, click Create App in the upper-right corner.
In the Create App dialog box, set App Name to
fcAPP
and click Confirm.On the Apps page, click the created
fcApp
application. On the App Details page, you can see that two authentication methods are available:AppKey
andAppCode
. TheAppKey
method includes a pair ofAppKey
andAppSecret
, which works similarly to a pair of username and password. When you call the API, you must pass theAppKey
as a parameter. TheAppSecret
is used to calculate signatures. API Gateway verifies the AppKey-AppSecret pair to authenticate your identity.
Authorize the application to call the API
Log on to the API Gateway console. In the top navigation bar, select a region. In the left-side navigation pane, choose Manage APIs > APIs.
On the APIs page, find the created
FC-test
API and choose > Authorize in the Actions column.On the Authorize page, set the Stage parameter to Release. Enter the name of the application you created in the search bar 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.
Log on to the API Gateway console. In the left-side navigation pane, choose Call APIs > Debug.
On the Debug page, select the
FC-test
API that you created, set Verification Method to Use AppCode, and then click Send Request. The following information indicates that the configuration is successful.
Call the API
By performing the preceding steps, you created and published the API, created and authorized an application to call the API, and debugged the API. In this step, you can use the AppCode provided by API Gateway to call a published API from a business system. For more 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.
curl -i -X GET "http://fd6f8e2b7bf44ab181a56****-cn-hangzhou.alicloudapi.com" -H "Authorization:APPCODE 7d2b7e4945ce44028ab00***"
The following figure shows the result of an API call.