This topic describes how to install CloudFlow SDK for Node.js and provides examples on how to use the SDK to call CloudFlow API operations. In the following examples, CloudFlow SDK for Node.js
is used to call CloudFlow API operations to create a workflow, query the information about a workflow, and asynchronously start the execution of a workflow. This topic also provides the complete SDK integration steps.
Prerequisites
An AccessKey pair is configured. Make sure that an AccessKey pair is created. For more information, see Create an AccessKey pair. To prevent credential leaks, you can write the credentials into environment variables. For more information, see Best practices for using an access credential to call API operations.
Environment requirements
Node.js 8.x or later is used.
Step 1: Import CloudFlow SDK for Node.js to your project
Alibaba Cloud SDK allows you to initiate generic and specialized calls. For more information, see Generic calls and specialized calls. The SDK that you need to import varies based on the call type.
Specialized calls
Alibaba Cloud SDK V2.0 incorporates the main API logic, such as parameter processing, request assembly, and response processing. You can call API operations by installing the SDK dependency package of the service that you want to use.
Log on to SDK Center and select the service that you want to use. In this example, select CloudFlow.
In the All languages field on the Install page, select TypeScript. On the Quick Start tab, select a method to install CloudFlow SDK for TypeScript.
Generic calls
You do not need to install the SDK of a service to initiate generic calls. You need to only import the @alicloud/openapi-client
core package. For information about the latest version of the core package, see alicloud/openapi-client. Run the following command to install the core package:
npm i @alicloud/openapi-client
Step 2: Initialize a request client
Specify the endpoint of CloudFlow based on the region in which you want to use CloudFlow. For information about the endpoint of CloudFlow in each region, see Regions.
The following section provides sample code on how to initialize a client for specialized calls. For information about how to initiate generic calls, see Generic calls and specialized calls.
Use an AccessKey pair
The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using the AccessKey pair to perform operations is a high-risk operation. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. We recommend that you do not save the AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources in your account may be compromised.
In this example, the AccessKey pair is obtained from the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables to implement identity authentication.
For more information about how to configure authentication information, see Manage access credentials.
The method for configuring environment variables varies based on the operating system. For more information, see Configure environment variables in Linux, macOS, and Windows.
'use strict';
const fnf20190315 = require('@alicloud/fnf20190315');
const OpenApi = require('@alicloud/openapi-client');
const Util = require('@alicloud/tea-util');
const Tea = require('@alicloud/tea-typescript');
class Client {
/**
* Use your AccessKey ID and AccessKey secret to initialize the client.
* @return Client
* @throws Exception
*/
static createClient() {
let config = new OpenApi.Config({
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured.
accessKeyId: process.env['ALIBABA_CLOUD_ACCESS_KEY_ID'],
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
accessKeySecret: process.env['ALIBABA_CLOUD_ACCESS_KEY_SECRET'],
});
config.endpoint = `cn-hangzhou.fnf.aliyuncs.com`;
return new fnf20190315.default(config);
}
static async main(args) {
let client = Client.createClient();
}
}
exports.Client = Client;
Client.main(process.argv.slice(2));
Step 3: Use the initialized client to call CloudFlow API operations
After you initialize a client, you can use the client to call CloudFlow API operations.
API operation: CreateFlow
Call the CreateFlow operation to create a workflow. When you call the operation, create a request and configure parameters and runtime settings based on your requirements. You can also customize runtime settings to meet specific requirements.
// Create a request.
let createFlowRequest = new fnf20190315.CreateFlowRequest({
// Specify a name for the workflow that you want to create.
name: 'your_flow_name',
// Define the workflow in conformance with the FDL syntax standard. To ensure forward compatibility, the system supports the old and new versions of workflow definition specifications.
definition: 'The old version:
"
type: flow
version: v1
name: my_flow_name
steps:
- type: pass
name: mypass
"
The new version:
"
Type: StateMachine
SpecVersion: v1
Name: my_flow_name
StartAt: my_state
States:
- Type: Pass
Name: my_state
End: true
"',
// Specify a description for the workflow.
description: 'your test flow',
// Specify the type of the workflow.
type: 'FDL',
});
// Configure runtime settings.
let runtime = new Util.RuntimeOptions({ });
The following sample code provides a complete example on how to create a workflow by using an AccessKey pair:
'use strict';
const fnf20190315 = require('@alicloud/fnf20190315');
const OpenApi = require('@alicloud/openapi-client');
const Util = require('@alicloud/tea-util');
const Tea = require('@alicloud/tea-typescript');
class Client {
/**
* Use your AccessKey ID and AccessKey secret to initialize the client.
* @return Client
* @throws Exception
*/
static createClient() {
let config = new OpenApi.Config({
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured.
accessKeyId: process.env['ALIBABA_CLOUD_ACCESS_KEY_ID'],
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
accessKeySecret: process.env['ALIBABA_CLOUD_ACCESS_KEY_SECRET'],
});
config.endpoint = `cn-hangzhou.fnf.aliyuncs.com`;
return new fnf20190315.default(config);
}
static async main(args) {
let client = Client.createClient();
// Create a request.
let createFlowRequest = new fnf20190315.CreateFlowRequest({
// Specify a name for the workflow that you want to create.
name: 'your_flow_name',
// Define the workflow in conformance with the FDL syntax standard. To ensure forward compatibility, the system supports the old and new versions of workflow definition specifications.
definition: 'The old version:
"
type: flow
version: v1
name: my_flow_name
steps:
- type: pass
name: mypass
"
The new version:
"
Type: StateMachine
SpecVersion: v1
Name: my_flow_name
StartAt: my_state
States:
- Type: Pass
Name: my_state
End: true
"',
// Specify a description for the workflow.
description: 'your test flow',
// Specify the type of the workflow.
type: 'FDL',
});
// Configure runtime settings.
let runtime = new Util.RuntimeOptions({ });
try {
// Write your code to print the response of the API operation if necessary.
await client.createFlowWithOptions(createFlowRequest, runtime);
} catch (error) {
// Handle exceptions with caution in your actual business scenario and do not ignore exceptions in your project. In this example, error messages are printed for reference only.
// Display error messages.
console.log(error.message);
// Provide the URL for troubleshooting.
console.log(error.data["Recommend"]);
Util.default.assertAsString(error.message);
}
}
}
exports.Client = Client;
Client.main(process.argv.slice(2));
API operation: DescribeFlow
Call the DescribeFlow operation to query the information about a workflow. When you call the operation, create a request and configure parameters and runtime settings based on your business requirements. You can also customize runtime settings to meet specific requirements.
// Create a request and specify the request parameters.
let describeFlowRequest = new fnf20190315.DescribeFlowRequest({
// Specify the name of the workflow that you want to query.
name: 'your_flow_name',
});
// Create a runtime configuration object.
let runtime = new Util.RuntimeOptions({ });
The following sample code provides a complete example on how to query the information about a workflow by using an AccessKey pair:
'use strict';
const fnf20190315 = require('@alicloud/fnf20190315');
const OpenApi = require('@alicloud/openapi-client');
const Util = require('@alicloud/tea-util');
const Tea = require('@alicloud/tea-typescript');
class Client {
/**
* Use your AccessKey ID and AccessKey secret to initialize the client.
* @return Client
* @throws Exception
*/
static createClient() {
let config = new OpenApi.Config({
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured.
accessKeyId: process.env['ALIBABA_CLOUD_ACCESS_KEY_ID'],
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
accessKeySecret: process.env['ALIBABA_CLOUD_ACCESS_KEY_SECRET'],
});
config.endpoint = `cn-hangzhou.fnf.aliyuncs.com`;
return new fnf20190315.default(config);
}
static async main(args) {
let client = Client.createClient();
// Create a request.
let describeFlowRequest = new fnf20190315.DescribeFlowRequest({
// Specify the name of the workflow that you want to query.
name: 'your_flow_name',
});
// Configure runtime settings.
let runtime = new Util.RuntimeOptions({ });
try {
// Write your code to print the response of the API operation if necessary.
await client.describeFlowWithOptions(describeFlowRequest, runtime);
} catch (error) {
// Handle exceptions with caution in your actual business scenario and do not ignore exceptions in your project. In this example, error messages are printed for reference only.
// Display error messages.
console.log(error.message);
// Provide the URL for troubleshooting.
console.log(error.data["Recommend"]);
Util.default.assertAsString(error.message);
}
}
}
exports.Client = Client;
Client.main(process.argv.slice(2));
API operation: StartExecution
Call the operation to start the execution of a workflow. The StartExecution operation is an asynchronous operation. When you call the operation, create a request and configure parameters and runtime settings based on your business requirements. You can also customize runtime settings to meet specific requirements.
// Create a request.
let startExecutionRequest = new fnf20190315.StartExecutionRequest({
// Specify the name of the workflow that you want to execute.
flowName: 'your_flow_name',
// Specify a name for the execution.
executionName: 'your_exec_name',
// Specify the input of the execution.
input: '{"key":"value"}',
// Call back TaskToken-related tasks after the workflow execution ends. Example value: 12.
callbackFnFTaskToken: '12',
});
// Configure runtime settings.
let runtime = new Util.RuntimeOptions({ });
The following sample code provides a complete example on how to start the execution of a workflow by using an AccessKey pair:
'use strict';
const fnf20190315 = require('@alicloud/fnf20190315');
const OpenApi = require('@alicloud/openapi-client');
const Util = require('@alicloud/tea-util');
const Tea = require('@alicloud/tea-typescript');
class Client {
/**
* Use your AccessKey ID and AccessKey secret to initialize the client.
* @return Client
* @throws Exception
*/
static createClient() {
let config = new OpenApi.Config({
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured.
accessKeyId: process.env['ALIBABA_CLOUD_ACCESS_KEY_ID'],
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
accessKeySecret: process.env['ALIBABA_CLOUD_ACCESS_KEY_SECRET'],
});
config.endpoint = `cn-hangzhou.fnf.aliyuncs.com`;
return new fnf20190315.default(config);
}
static async main(args) {
let client = Client.createClient();
// Create a request.
let startExecutionRequest = new fnf20190315.StartExecutionRequest({
// Specify the name of the workflow that you want to execute.
flowName: 'your_flow_name',
// Specify a name for the execution.
executionName: 'your_exec_name',
// Specify the input of the execution.
input: '{"key":"value"}',
// Call back TaskToken-related tasks after the workflow execution ends. Example value: 12.
callbackFnFTaskToken: '12',
});
// Configure runtime settings.
let runtime = new Util.RuntimeOptions({ });
try {
// Write your code to print the response of the API operation if necessary.
await client.startExecutionWithOptions(startExecutionRequest, runtime);
} catch (error) {
// Handle exceptions with caution in your actual business scenario and do not ignore exceptions in your project. In this example, error messages are printed for reference only.
// Display error messages.
console.log(error.message);
// Provide the URL for troubleshooting.
console.log(error.data["Recommend"]);
Util.default.assertAsString(error.message);
}
}
}
exports.Client = Client;
Client.main(process.argv.slice(2));
SDK call example
You can use the API-level SDK demos in different programming languages for debugging. To view the sample code, visit OpenAPI Explorer.