Help Center/ FunctionGraph/ Developer Guide/ Node.js/ Developing a Node.js Function Using Huawei Cloud SDKs
Updated on 2025-12-10 GMT+08:00

Developing a Node.js Function Using Huawei Cloud SDKs

Huawei Cloud API Explorer provides API reference documents and SDK code examples for each cloud service.

This section uses the API for querying the function list as an example to describe how to develop a Node.js function on the FunctionGraph console using Huawei Cloud SDKs.

Step 1: Creating a Function Agency

  1. Log in to the IAM console.
  2. In the navigation pane on the left, choose Permissions > Policies/Roles. On the displayed page, click Create Custom Policy in the upper right corner.
  3. Take the agency to be created for the API used to query the function list as an example. Configure a custom policy that contains the permission for querying the function list, as shown in Figure 1, and click OK.
    For more information about permissions, see Basic Concepts About Permissions.
    Figure 1 Custom policy for querying a function list

  4. In the navigation pane of the IAM console, choose Agencies. Then, click Create Agency in the upper right corner.
  5. Configure agency parameters. After the parameters are configured, as shown in Figure 2, click OK. The system displays a message indicating that the creation is successful. Click Authorize.
    Figure 2 Entering basic information

  6. On the displayed page, select the custom policy created in 3, click Next, select the authorization scope based on the actual needs, and click OK.

Step 2: Creating a Node.js Function

  1. Log in to the FunctionGraph console and click Create Function in the upper right corner.
  2. Create a Node.js event function from scratch, select the agency created in Step 1: Creating a Function Agency, select the latest runtime version, and click Create Function.
  3. On the Code tab, scroll down to the Dependencies area and click Add.
  4. Select Private for Type and click Create Dependency as shown in Figure 3. The dependency creation page is displayed.
    Figure 3 Selecting dependencies

  5. Create the required Node.js dependency for the function by referring to Huawei Cloud Node.js SDK and Configuring a Dependency for a Function.

    Note that the runtime version of the dependency must be the same as that of the Node.js function.

  6. After the dependency is created, return to 4 and add the created dependency.

Step 3: Obtaining SDK Sample Code from APIE

  1. Open API Explorer, select the required API, click the Sample Code tab, and select the Node.js language, as shown in Figure 4.

    Figure 4 APIE sample code

    1. The API for querying functions is used as an example.
    2. Enter the parameters required by the API. For details about the parameters, see the corresponding section in the API reference. In this example, see Querying Functions.
    3. Copy the code generated by API Explorer and paste it in the code editing box of the function created in Step 2: Creating a Node.js Function.

  2. You are advised to configure the AK/SK in the function environment variables. For details, see Configuring Environment Variables. And you can use the context.getUserData(string key) method to obtain the AK/SK in the code.

    The modified code is as follows:

    const core = require('@huaweicloud/huaweicloud-sdk-core');
    const functiongraph = require("@huaweicloud/huaweicloud-sdk-functiongraph/v2/public-api");
    exports.handler = async (event, context) => {
        const ak = context.getUserData("AK");
        const sk = context.getUserData("SK");
        const endpoint = "https://functiongraph.cn-north-4.myhuaweicloud.com";
        const project_id = "project_id";
        const credentials = new core.BasicCredentials()
            .withAk(ak)
            .withSk(sk)
            .withProjectId(project_id)
        const client = functiongraph.FunctionGraphClient.newBuilder()
            .withCredential(credentials)
            .withEndpoint(endpoint)
            .build();
        const request = new functiongraph.ListFunctionsRequest();
        request.marker = "marker";
        request.maxitems = "maxitems";
        request.packageName = "package_name";
        request.funcName = "func_name";
        const result = client.listFunctions(request);
        result.then(result => {
            console.log("JSON.stringify(result)::" + JSON.stringify(result));
        }).catch(ex => {
            console.log("exception:" + JSON.stringify(ex));
        });
        const output =
        {
            'statusCode': 200,
            'headers':
            {
                'Content-Type': 'application/json'
            },
            'isBase64Encoded': false,
            'body': JSON.stringify(event),
        }
        return output;
    }

  3. (Optional) To use a more secure authentication mode, replace the following code:

    const ak = context.getUserData("AK");
    const sk = context.getUserData("SK");
    const credentials = new core.BasicCredentials()
        .withAk(ak)
        .withSk(sk)
        .withProjectId(project_id)

    with

    const ak = context.getSecurityAccessKey();
    const sk = context.getSecuritySecretKey();
    const st = context.getSecurityToken();
    const credentials = new core.BasicCredentials()
        .withAk(ak)
        .withSk(sk)
        .withProjectId(project_id)
        .with_security_token(st)