Integration with Control Flow Example

Connecting two API endpoints together

Watch the demo

Video outline

In this video, we show you how to extend the simple integration example. We'll be adding query params, dynamic headers, and control flow to the our multi-step workflow execution. We'll use a Laminar Keyword to run a workflow depending on if a condition is met.

Objective

To send data individually for each user by making a POST request to a destination, dynamically adding query parameters and headers, and incorporating control flow in the workflow execution.

Key Steps

  1. Create a new flow in Laminar named "Determine if should send user" with no source to execute control flow logic using a Laminar Keyword.

  2. Use jq to extract data from the first step by accessing the key: .step_1.data.users.

  3. Pass the users array to a new key in an object using .users from step1.data.users.

  4. Add query parameters and headers dynamically using lam.queryParams and lam.headers.

  5. Include a body using lam.body with the desired object data.

  6. Create a new workflow called "Recursively Send Data" to send requests for each user in the user array.

  7. Use the lam.result syntax to execute a workflow by ID for each user.

  8. Implement conditions using an if statement to filter data based on specific attributes like total spent.

Cautionary Notes

  • Ensure proper syntax and structure while adding query parameters, headers, and body data.

  • Verify workflow IDs and payload data for accurate execution.

  • Double-check conditions and statements to filter data correctly.

Tips for Efficiency

  • Use lam.workflowId and lam.payload to iterate over users and pass data to the specified workflow.

  • Utilize if statements to apply conditions for filtering data based on specific attributes.

  • Test the workflow execution to ensure accurate data transmission and control flow implementation.

Data Transformation

We transformed our first JSON with control flow using this JQ:

.step_1.data.users |
map(
if .totalSpent > 20 then
{
    "lam.workflowId": 49,
    "lam.payload": {
        "lam.queryParams": {
            "email": .email
        },
        "lam.body": .
    }
}
else
    empty
end
)
View the transformation log
2024-06-10 02:40:05,624 - INFO - Logging to lam_run_49_71_cd18bf1f-4c5a-4922-8b5a-7b2110ce9967_20240610_024005.log
2024-06-10 02:40:05,625 - INFO - Running command with program file: /tmp/program11614070107740914765.lam, input: /tmp/input17048584742003229031.json, workspace_id: 49, flow_id: 71, as_json: True
2024-06-10 02:40:05,625 - INFO - Processing input: /tmp/input17048584742003229031.json
2024-06-10 02:40:05,626 - INFO - Parsing program file: /tmp/program11614070107740914765.lam
2024-06-10 02:40:05,626 - INFO - Event lam.run.start triggered, with properties: {'program_file': '/tmp/program11614070107740914765.lam', 'as_json': True, 'workspace_id': '49', 'flow_id': '71'}
2024-06-10 02:40:05,626 - INFO - Running jq script .step_1.data.users |
map(
if .totalSpent > 20 then
{
    "lam.workflowId": 49,
    "lam.payload": {
        "lam.queryParams": {
            "email": .email
        },
        "lam.body": .
    }
}
else
    empty
end
)
 with input data {"step_1":{"data":{"totalUsers":4,"averageAge":31.25,"totalItemsSold":81,"users":[{"name":"Alice","email":"alice@example.com","totalSpent":22},{"name":"Bob","email":"bob@example.com","totalSpent":20},{"name":"Charlie","email":"charlie@example.com","totalSpent":24}]},"response":{"message":"Received POST request","contents":{"totalUsers":4,"averageAge":31.25,"totalItemsSold":81,"users":[{"name":"Alice","email":"alice@example.com","totalSpent":22},{"name":"Bob","email":"bob@example.com","totalSpent":20},{"name":"Charlie","email":"charlie@example.com","totalSpent":24}]}}}}
2024-06-10 02:40:05,659 - INFO - Handling jq output: [{"lam.workflowId":49,"lam.payload":{"lam.queryParams":{"email":"alice@example.com"},"lam.body":{"name":"Alice","email":"alice@example.com","totalSpent":22}}},{"lam.workflowId":49,"lam.payload":{"lam.queryParams":{"email":"charlie@example.com"},"lam.body":{"name":"Charlie","email":"charlie@example.com","totalSpent":24}}}]

2024-06-10 02:40:05,660 - INFO - Event lam.run.warn triggered, with properties: {'error': 'Invalid JSON output', 'workspace_id': '49', 'flow_id': '71'}
2024-06-10 02:40:05,660 - INFO - Event lam.run.success triggered, with properties: {'workspace_id': '49', 'flow_id': '71'}
2024-06-10 02:40:05,661 - INFO - Run complete, waiting for event logger to finish

Last updated