CloudFlow SDK for Python lets you manage workflows and executions programmatically. Install the SDK, initialize a client, and call API operations to create flows, query flow details, and start executions.
Prerequisites
Python 3.7 or later
An AccessKey pair stored in environment variables. If you have not created one yet, see Create an AccessKey pair. For security best practices, see Best practices for using access credentials.
Install the SDK
Alibaba Cloud SDK supports two call types: specialized calls (product-specific SDK) and generic calls (core SDK only). For a detailed comparison, see Generic calls and specialized calls.
Specialized calls (recommended)
Run the following command to install the CloudFlow SDK:
pip install alibabacloud_fnf20190315==1.1.3To verify the installation:
python -c "import alibabacloud_fnf20190315; print('CloudFlow SDK installed successfully')"You can find the latest version and alternative installation methods on the CloudFlow SDK page in OpenAPI Explorer. Select Python in the All languages field.
Generic calls
Generic calls require only the core alibabacloud-tea-openapi package -- no product-specific SDK needed:
pip install alibabacloud-tea-openapiFor the latest core package version, see tea-openapi.
Initialize a client
Set the endpoint to match the region where you use CloudFlow. For the full list of regional endpoints, see Regions.
The following example initializes a client for specialized calls using an AccessKey pair. For generic call initialization, see Generic calls and specialized calls.
The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To reduce risk, use a RAM user to call API operations. Do not save your AccessKey ID or AccessKey secret in project code. For more information, see Manage access credentials.
The following code reads credentials from the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables. To set up these variables on your operating system, see Configure environment variables in Linux, macOS, and Windows.
import os
from alibabacloud_fnf20190315.client import Client as fnf20190315Client
from alibabacloud_tea_openapi import models as open_api_models
def create_client() -> fnf20190315Client:
"""Initialize a CloudFlow client with AccessKey credentials."""
config = open_api_models.Config(
access_key_id=os.environ["ALIBABA_CLOUD_ACCESS_KEY_ID"],
access_key_secret=os.environ["ALIBABA_CLOUD_ACCESS_KEY_SECRET"],
)
# Replace with the endpoint for your region
config.endpoint = "cn-hangzhou.fnf.aliyuncs.com"
return fnf20190315Client(config)Call CloudFlow API operations
After you initialize a client, use it to call any CloudFlow API operation. The following sections demonstrate three operations. All examples share the same create_client() function defined above.
CreateFlow
CreateFlow creates a flow. Provide a name, a Flow Definition Language (FDL) definition, and a type.
CloudFlow supports two FDL specification versions. The old version uses type: flow, and the new version uses Type: StateMachine. Both are accepted for forward compatibility.
import os
import sys
from typing import List
from alibabacloud_fnf20190315.client import Client as fnf20190315Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_fnf20190315 import models as fnf_20190315_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient
class Sample:
def __init__(self):
pass
@staticmethod
def create_client() -> fnf20190315Client:
"""Initialize a CloudFlow client with AccessKey credentials."""
config = open_api_models.Config(
access_key_id=os.environ["ALIBABA_CLOUD_ACCESS_KEY_ID"],
access_key_secret=os.environ["ALIBABA_CLOUD_ACCESS_KEY_SECRET"],
)
config.endpoint = "cn-hangzhou.fnf.aliyuncs.com"
return fnf20190315Client(config)
@staticmethod
def main(args: List[str]) -> None:
client = Sample.create_client()
create_flow_request = fnf_20190315_models.CreateFlowRequest(
name="<your-flow-name>",
definition="""\
Type: StateMachine
SpecVersion: v1
Name: my_flow_name
StartAt: my_state
States:
- Type: Pass
Name: my_state
End: true""",
description="my test flow",
type="FDL",
)
runtime = util_models.RuntimeOptions()
try:
response = client.create_flow_with_options(create_flow_request, runtime)
print(response.body)
except Exception as error:
print(error.message)
print(error.data.get("Recommend"))
UtilClient.assert_as_string(error.message)
if __name__ == "__main__":
Sample.main(sys.argv[1:])For asynchronous calls, replace create_flow_with_options with create_flow_with_options_async and use await.
Replace the following placeholders with your values:
| Placeholder | Description | Example |
|---|---|---|
<your-flow-name> | Name for the new flow | my_flow_name |
DescribeFlow
DescribeFlow retrieves the details of a flow, including its definition and metadata.
import os
import sys
from typing import List
from alibabacloud_fnf20190315.client import Client as fnf20190315Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_fnf20190315 import models as fnf_20190315_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient
class Sample:
def __init__(self):
pass
@staticmethod
def create_client() -> fnf20190315Client:
"""Initialize a CloudFlow client with AccessKey credentials."""
config = open_api_models.Config(
access_key_id=os.environ["ALIBABA_CLOUD_ACCESS_KEY_ID"],
access_key_secret=os.environ["ALIBABA_CLOUD_ACCESS_KEY_SECRET"],
)
config.endpoint = "cn-hangzhou.fnf.aliyuncs.com"
return fnf20190315Client(config)
@staticmethod
def main(args: List[str]) -> None:
client = Sample.create_client()
describe_flow_request = fnf_20190315_models.DescribeFlowRequest(
name="<your-flow-name>",
)
runtime = util_models.RuntimeOptions()
try:
response = client.describe_flow_with_options(describe_flow_request, runtime)
print(response.body)
except Exception as error:
print(error.message)
print(error.data.get("Recommend"))
UtilClient.assert_as_string(error.message)
if __name__ == "__main__":
Sample.main(sys.argv[1:])Replace <your-flow-name> with the name of the flow to query. For asynchronous calls, use describe_flow_with_options_async with await.
StartExecution
StartExecution starts an asynchronous execution of a flow.
import os
import sys
from typing import List
from alibabacloud_fnf20190315.client import Client as fnf20190315Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_fnf20190315 import models as fnf_20190315_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient
class Sample:
def __init__(self):
pass
@staticmethod
def create_client() -> fnf20190315Client:
"""Initialize a CloudFlow client with AccessKey credentials."""
config = open_api_models.Config(
access_key_id=os.environ["ALIBABA_CLOUD_ACCESS_KEY_ID"],
access_key_secret=os.environ["ALIBABA_CLOUD_ACCESS_KEY_SECRET"],
)
config.endpoint = "cn-hangzhou.fnf.aliyuncs.com"
return fnf20190315Client(config)
@staticmethod
def main(args: List[str]) -> None:
client = Sample.create_client()
start_execution_request = fnf_20190315_models.StartExecutionRequest(
flow_name="<your-flow-name>",
execution_name="<your-execution-name>",
input='{"key":"value"}',
callback_fn_ftask_token="12",
)
runtime = util_models.RuntimeOptions()
try:
response = client.start_execution_with_options(start_execution_request, runtime)
print(response.body)
except Exception as error:
print(error.message)
print(error.data.get("Recommend"))
UtilClient.assert_as_string(error.message)
if __name__ == "__main__":
Sample.main(sys.argv[1:])Replace the following placeholders with your values:
| Placeholder | Description | Example |
|---|---|---|
<your-flow-name> | Name of the flow to run | my_flow_name |
<your-execution-name> | Unique name for this execution | exec-20260311 |
For asynchronous calls, use start_execution_with_options_async with await.
What's next
Browse and debug more API operations in OpenAPI Explorer.
See the full CloudFlow API reference for all available operations.