Build a Durable Workflow
- Time: 20 minutes
- What you'll build: A durable workflow that handles customer orders
The durable workflow reserves inventory for an order, suspends until a payment confirmation arrives, and then either emails the customer or cancels the order. Every activity result and data event is recorded, so a restart replays the record instead of reserving twice, and the wait for payment holds no thread and no connection while it lasts, whether that is seconds or days.
The finished flow has three steps:
- Reserve inventory as a recorded activity.
- Wait for a payment data event delivered from outside the workflow.
- Send the confirmation email when payment succeeded, or cancel the order when it did not.


Prerequisites
A working WSO2 Integrator environment.
-
WSO2 Cloud setup: Sign up and launch WSO2 Integrator in the browser-based cloud editor.
-
Temporal server: An internet-accessible Temporal server, such as Temporal Cloud or a publicly exposed self-hosted instance, with its endpoint URL, namespace, and credentials. See Temporal credentials.
Step 1: Create the integration
-
On the project home page, click Create on Cloud.

-
The WSO2 Integrator cloud editor opens in your browser.

The cloud editor creates a default integration for you, so you don't need to name it now. You can change its name when you deploy it. For more information, see Save and deploy.
Step 2: Add the durable workflow artifact
-
In the design view, click Add Artifact Manually.

-
On the Artifacts page, under Durable Workflow, click Durable Workflow.

-
The Create New Durable Workflow form opens.
-
Set Name to
orderWorkflow. -
Click the Workflow Input Data Type field, then click + Create New Type.

-
The Create New Type dialog opens with the correct defaults: the Create from scratch tab and Kind set to Record.
-
Change Name from
MyTypetoOrderInfo. -
Add each field with the + next to Fields, then set its name and type:
Field Type idstringcustomerIdstringcustomerEmailstringtotalint -
Click Save. The record is added to your project and appears under Types in the sidebar.

-
Click Create. The workflow is generated and its diagram opens with a single Start node, ready for the first step. The workflow receives the order as its
inputparameter.
+ Create New Type is only one way to fill Workflow Input Data Type. Pick any type already in the project straight from the list, or click Open Type Browser to search the wider set. Either way the result is an ordinary record you can edit later from Types in the sidebar. See Types.
Step 3: Reserve the inventory
Anything that touches the outside world belongs in an activity rather than in the workflow body. The runtime records each activity result, so a completed activity is never executed twice on replay.
The first step of the order process reserves stock. You create the activity and the call that uses it in one pass, straight from the workflow diagram.
-
On the workflow diagram, click +.

-
In the node panel, under Workflow > Steps, click Call Activity. The Activities panel opens, listing everything this workflow can call.

-
Under Current Integration, click + Create Activity.

-
In the Workflow Activity form, set Activity Name to
reserveInventory.
-
Under Parameters, click + Add Parameter, set Type to
OrderInfoand Name toorderInfo, then click Add.
-
Leave Return Type empty. This activity holds stock and returns nothing, so the designer declares it as
returns error?. Click Save.
-
In the Activities panel, under Current Integration, click
reserveInventoryto add the activity call to the workflow diagram.
-
Fill in the call form:
Field Value Order Info The order to reserve. Switch to Expression and set it to the workflow's input parameter. Retry Policy Leave it on No Retry, the default, for now. 
-
Click Save. The
reserveInventorynode appears on the diagram.
-
Give the activity something to do. Click the open icon on the node to open its own diagram. To keep it simple, mock the implementation with a log line.

-
On the activity diagram, click +.

-
In the node panel, under Logging, click Log Info.

-
Set Msg to
Inventory reservedand click Save.
+ Create Activity writes a new activity function in your integration. Create Activity from a Connection wraps an action on a connector you already have, and Prebuilt Activities holds the ones that ship with the runtime, for REST calls, SOAP calls, and email. See Activities.
Step 4: Wait for the payment confirmation
Payment is confirmed by something outside the workflow, such as a payment gateway callback or an operator action. Model that as a data event: a named slot the workflow waits on, and that anyone holding the workflow ID can fill.
-
Go back to orderWorkflow under Workflows in the sidebar, then click the + below the
reserveInventorystep.
-
In the node panel, under Workflow > Steps, click Await Data. The Await Data form opens.

-
Under Data Waits, fill in the entry:
Field Value Description Data Receive Variable Name paymentThe variable that receives the value once it arrives. Data Type booleanThe type of the value the workflow expects. To keep this guide simple, use boolean. If the payment is received, the value istrue.Data Name paymentThe name used when sending the data into this workflow. 
-
Click Add then click Save.

The diagram gains a Wait for payment node, drawn with an incoming arrow from outside the flow, because that is where the value comes from. The workflow now suspends at this line, and only at this line. It holds no thread and no connection while it waits, and it survives a restart of the runtime.
Use a data event when a system or a person submits content the workflow processes, as here. Use a human task when a person makes a decision that the cloud-managed control plane should render as a form in their inbox.
Step 5: Branch on the payment result
The value that arrived decides what happens next, so split the flow in two.
-
Click + below the wait.

-
In the node panel, under Control, click If.

-
Condition is prefilled with
true. Replace it with the value the wait produced: click the field and pickpaymentunder Variables in the value helper. -
Click Add Else Block.
-
Click Save.

The diagram splits into a payment path and an Else path, each with its own +.
+ Add Else If Block adds another condition to the same node, and Remove Else Block drops the else path when a branch is all you need.
Step 6: Send the confirmation email
The payment path tells the customer the order is confirmed. Create that activity and call it in one pass, the same way as reserveInventory.
-
Click + on the
paymentpath, then under Workflow > Steps, click Call Activity.

-
The Activities panel already lists
reserveInventory, so click the + on the Current Integration header to add another activity.
-
Set Activity Name to
sendEmail. Click + Add Parameter, set Type toOrderInfoand Name toorderInfo, click Add, then click Save.

-
Click
sendEmailin the Activities panel under Current Integration.
-
In the Order Info field, switch from Record to Expression, then select the workflow's input, and click Save.

-
Click the open icon on the
sendEmailnode to open its diagram. To keep it simple, mock the implementation with a log line. -
Click +, then click Log Info under Logging.


-
Leave Msg on Text and type
Email sent to. Click on the text box to open the field's value helper, then click Inputs >orderInfo>customerEmail. It lands in the text as an expression.
-
Click Save.

Keep Msg on Text whenever the message is words with values dropped into it. Text takes what you type as the message and turns each inserted value into an interpolation, which is what produces the string template above. Expression treats the whole field as a single Ballerina expression, so bare words like Email sent to are a syntax error there. The same toggle appears on any field that accepts either, such as Order Info on a call.
Step 7: Cancel the order
The Else path releases the hold instead.
-
Go back to orderWorkflow under Workflows in the sidebar. Click + on the Else path, then under Workflow > Steps, click Call Activity.


-
Click the + on the Current Integration header.

-
Set Activity Name to
cancelOrder. Click + Add Parameter, set Type toOrderInfoand Name toorderInfo, click Add, then click Save.

-
Click
cancelOrderin the Activities panel under Current Integration.
-
In the Order Info field, switch from Record to Expression, then select the workflow's input, and click Save.

Both branches now end in an activity, and the workflow is complete.
Step 8: Start the workflow from a service
A workflow does not start itself. It is launched from an entry point such as a service or an automation, so give the order process an HTTP resource that starts a run for each order that arrives.
-
In the sidebar, click the overview icon next to the integration name to return to the design view.

-
Click + Add Artifact, then under Integration as API, click HTTP Service.

-
On the Create HTTP Service form, keep Service Contract on Design From Scratch, set Service Base Path to
/orderProcess, and click Create.
-
The service opens with no resources. Click + Add Resource, then click POST in the Select HTTP Method to Add list.

-
In New Resource Configuration, set Resource Path to
..
-
Click + Define Payload, open the Browse Existing Types tab, click
OrderInfounder the current integration, and click Save.
-
Click Save to create the resource. Its own diagram opens.

-
Click +, then under Workflow, click Run Workflow.

-
Select the
orderWorkflowunder Current Integration.
-
Under Input, switch from Record to Expression and set Input to the request payload. Leave Workflow ID Variable Name as
workflowId, then click Save.
-
Click + below the Run Workflow node, then click Return under Control. In the Expression field, select Variables, then select
workflowId, and click Save.


The resource now starts a run for every order it receives and answers with that run's workflow ID.
Run Workflow starts an instance and returns immediately with its ID. It does not wait for the order to finish, which is the point: the run may sit on the payment event for days. Return the ID to the caller, because it is what ties a later payment confirmation to this instance.
Step 9: Deliver the payment confirmation
The run is now waiting on the payment data event, and it will wait forever until something fills it. Add a second resource that does.
-
Return to the service: click HTTP Service - /orderProcess under Entry Points in the sidebar.

-
Click + Resource, then click POST.

-
The path needs a segment that carries the workflow ID, so click + Path Param and fill in the Path Parameter form:

Field Value Description Name orderIdThe variable the segment binds to, and the name you reference it by inside the resource. Type stringUnder Advanced Configurations. Defaults to string, which is what a workflow ID is. -
Click Save. The segment lands in Resource Path as
[string orderId]. Complete the path so it reads[string orderId]/payment.
-
Click Save to create the resource. Its diagram opens.
![New Resource Configuration with Resource Path set to [string orderId]/payment and Save highlighted](/documentation/devant/cloud/assets/images/set-payment-path-699ddf36ed0f31b896d525bd795c7b0b.png)
-
Click +, then under Workflow, click Send Data Event.

-
Fill in the Send Data form:
Field Value Description Workflow Name orderWorkflowThe workflow to send the data to. The dropdown lists all the available workflows. Target Workflow Id orderIdThe instance to resume, taken from the request path. Data Name paymentThe event to fill. The dropdown lists the events the chosen workflow declares. Data trueThe value to deliver. 
-
Click Save.
The node reads Send to payment and is drawn with a dashed arrow across to orderWorkflow, because it hands a value to a run rather than calling something.
Anyone holding the workflow ID can deliver the value, so in a real integration this resource is what the payment gateway's callback hits. This walkthrough leaves it unprotected to keep the steps short. A real callback needs authentication and authorization on this resource, because the workflow ID on its own is not an authorization check. See Await data events.
Step 10: Deploy to WSO2 Cloud
The cloud editor runs in WSO2 Cloud and cannot connect to a Temporal server on your computer, so you run the workflow by deploying it. A deployed workflow connects to the Temporal server set in its environment's Temporal credential.
-
Add a Temporal credential for your Temporal server, and assign it to the Development environment. See Temporal credentials.
-
In the cloud editor, open the integration overview and click Save and Deploy in the Deploy to WSO2 Cloud panel. Follow the steps in Save and deploy. WSO2 Cloud builds the integration and deploys it to the Development environment.
-
In the WSO2 Cloud console, open the integration. Wait until the Development environment shows Active and the Workflow Definitions panel lists
orderWorkflow.
Step 11: Test the workflow
-
In the Development environment, click Test.

-
Expand POST /, click Try it out, and replace the request body with the following:
{"customerId": "CUS-9","id": "ORD-1","total": 4500}
-
Click Execute.

-
Confirm that the response code is
201, and copy the workflow ID from the response body. The workflow reserves the inventory and waits for thepaymentevent.
-
Expand POST /{orderId}/payment, click Try it out, and set orderId to the workflow ID.
-
Click Execute.

-
Confirm that the response code is
202. The workflow resumes and sends the confirmation email.
-
Click Back to Overview. In the Development environment, confirm that COMPLETED (24H) shows
1.
-
Click View Workflows and confirm that the run shows Completed.

-
In the Development environment, click View Logs and confirm that the logs show
Inventory reservedandEmail sent to [email protected].
To test the cancellation path, change the payment resource's Data value to false, deploy the integration again, and start a new workflow.
Source code
View the generated Ballerina code
The steps above generate the following complete, runnable Ballerina program: the types, the workflow and its activities, and the HTTP service that starts and resumes it.
Types (types.bal)
type OrderInfo record {|
string id;
string customerId;
string customerEmail;
int total;
|};
# Data record for workflow function
type OrderWorkflowData record {|
future<boolean> payment;
|};
Workflow and activities
import ballerina/log;
import ballerina/workflow;
@workflow:Workflow
function orderWorkflow(workflow:Context ctx, OrderInfo input, OrderWorkflowData data) returns json|error {
() _ = check ctx->callActivity(reserveInventory, {orderInfo: input});
boolean payment = check wait data.payment;
if payment {
() _ = check ctx->callActivity(sendEmail, {orderInfo: input});
} else {
() _ = check ctx->callActivity(cancelOrder, {orderInfo: input});
}
}
@workflow:Activity
function reserveInventory(OrderInfo orderInfo) returns error? {
log:printInfo("Inventory reserved");
}
@workflow:Activity
function sendEmail(OrderInfo orderInfo) returns error? {
log:printInfo(string `Email sent to ${orderInfo.customerEmail}`);
}
@workflow:Activity
function cancelOrder(OrderInfo orderInfo) returns error? {
}
HTTP service
import ballerina/http;
import ballerina/workflow;
listener http:Listener httpDefaultListener = http:getDefaultListener();
service /orderProcess on httpDefaultListener {
resource function post .(OrderInfo payload) returns json|error {
string workflowId = check workflow:run(orderWorkflow, payload);
return workflowId;
}
resource function post [string orderId]/payment() returns json|error {
check workflow:sendData(orderWorkflow, orderId, "payment", true);
}
}
Watch it run
Once the integration is connected to the cloud-managed control plane, every instance is visible there, running or completed. The execution graph shows the reservation as a completed activity and the pending payment as a DATA node with status WAITING, so anyone can see exactly what an order is blocked on instead of guessing that it is stuck.
Next steps
- Activities — retry policies, activity inputs, and how results are recorded.
- Await data events — several events, timeouts, and delivering data from other systems.
- Await human task — pause the order for a person's decision instead of a system's data.
- Durable timers — add a payment deadline that survives restarts.
Was this page helpful?