Skip to navigation

How to trigger a Workflow Builder workflow

This topic demonstrates how to trigger a Workflow Builder workflow.
View as Markdown

This topic demonstrates how to trigger a Workflow Builder workflow. This example will update participant information for a workflow and create and start a new instance of the workflow.

For a walkthrough of the process to trigger a workflow, see this video:

Prerequisites

This how-to requires a published workflow in your account. If you are running this how-to from our launcher projects, this workflow will be created for you. The workflow will include three steps:

  • Set up invite
  • Sign an envelope from a template with a signer and a cc recipient
  • Show confirmation screen

Run this code example from our Quickstart or launcher projects: Bash, C#, Java, Node.js, PowerShell.

Required data

Running the code in this how-to requires this data:

Data elementDescription
{API_ACCOUNT_ID}A GUID value that identifies your account. This value is automatically generated by Docusign for any account you create. Copy the value from the API Account ID field on the Apps and Keys page.
{BASE_PATH}A string value that forms the root of the URL required to make API calls. The base path value in the example code is set to target the developer environment at https://<domain> Where <domain> is https://api-d.docusign.com/v1 for the developer environment and https://api.docusign.com/v1 for production.
{INSTANCE_NAME}A string value for the name of the Workflow Builder workflow instance.
{CC_RECIPIENT_EMAIL}A string value for the email address of the carbon copy recipient.
{CC_RECIPIENT_NAME}A string value for the name of the carbon copy recipient.
{SIGNER_EMAIL}A string value for the email address where the signer will receive a notification of the signing request.
{SIGNER_NAME}A string value for the full name of the signer.
{WORKFLOW_ID}A GUID value that identifies your Workflow Builder workflow.

Step 1. Obtain your OAuth token

To make the API calls shown in this how-to, you need a valid OAuth access token. Docusign supports access tokens for three different recommended OAuth grant types: Public Authorization Code Grant, Confidential Authorization Code Grant, and JSON Web Token (JWT) Grant. Choose the OAuth grant type appropriate for your scenario.

See Confidential Authorization Code Grant for guidance on your options to get an Confidential Authorization Code Grant OAuth token.

Workflow Builder OAuth access tokens are the same ones used for our eSignature REST API, although you must request the signature scope and one or more Workflow Builder scopes to call the Workflow Builder API. The endpoint calls made in this how-to require that you request the Workflow Builder-specific aow_manage scope when obtaining your access token.

See Workflow Builder Authentication for details and the full list of Workflow Builder scopes.

Three icons that represent the three methods a user can use to obtain an OAuth token: JSON Web Token (JWT), Public Authorization Code Grant (ACG), and Confidential Authorization Code grant

Step 2. Construct your API headers

After you have a valid OAuth access token, you are ready to construct the API headers that you will pass to the API call. Construct your headers like this, but make sure to substitute your valid OAuth access token for {ACCESS_TOKEN}.

declare -a Headers=('--header' "Authorization: Bearer ${ACCESS_TOKEN}" \
'--header' "Accept: application/json" \
'--header' "Content-Type: application/json")

Step 3. Retrieve the triggerURL

Make a Workflows:getWorkflowTriggerRequirements request to retrieve the desired workflow definition and extract the url property from the response.

Here’s how you make the call, but make sure to substitute your API account ID for {API_ACCOUNT_ID}, and the desired workflow ID for {WORKFLOW_ID}.

response=$(mktemp /tmp/response-wftmp.XXXXXX)
Status=$(
curl -w '%{http_code}' --request GET "${base_path}/accounts/${account_id}/workflows" \
"${Headers[@]}" \
--output ${response}
)
# If the status code returned is greater than 201 (OK / Accepted), display an error message with the API response.
if [[ "$Status" -gt "201" ]]; then
echo ""
echo "Unable to retrieve a workflow definition"
echo ""
cat $response
exit 0
fi
echo "Workflows found:"
cat $response
echo ""
workflow_ids=$(grep -B 3 -E '"status": "active"' "$response" | grep -B 2 '"name": "Example workflow - send invite to signer"' | grep '"id":' | sed -n 's/.*"id": "\([^"]*\)".*/\1/p')
# Read the existing workflow ID from the config file
config_workflow_id=""
if [ -s config/WORKFLOW_ID ]; then
config_workflow_id=$(cat config/WORKFLOW_ID)
fi
# If there are multiple active workflows, checks if the config_workflow_id is in the workflow list and uses the config_workflow_id.
if [ -n "$config_workflow_id" ] && echo "$workflow_ids" | grep -q "$config_workflow_id"; then
workflow_id="$config_workflow_id"
else
workflow_id=$(echo "$workflow_ids" | head -n 1)
fi
# Error handling if no active workflow ID is found
if [ -z "$workflow_id" ]; then
echo "Error: No active workflow ID found in the response."
echo "Please create and publish a workflow before running this example."
exit 0
fi
# Get the trigger URL
# workflow_id comes from the response of the Workflows: getWorkflowsList endpoint
response=$(mktemp /tmp/response-wftmp.XXXXXX)
Status=$(curl -s -w "%{http_code}\n" -i --request GET "${base_path}/accounts/${account_id}/workflows/${workflow_id}/trigger-requirements" \
"${Headers[@]}" \
--output ${response})
# If the status code returned is greater than 201 (OK / Accepted), display an error message with the API response.
if [[ "$Status" -gt "201" ]]; then
echo ""
echo "Unable to trigger a new instance of the specified workflow ${workflow_id}"
echo ""
cat $response
exit 0
fi
cat $response
echo ""
trigger_url=$(grep '"url":' $response | sed -n 's/.*"url": "\([^"]*\)".*/\1/p')
decoded_trigger_url=$(echo $trigger_url | sed 's/\\u0026/\&/g')

Step 4. Construct the request body

Construct the trigger_inputs_object to include values for the variables defined in the workflow.

request_data=$(mktemp /tmp/request-wf-001.XXXXXX)
printf \
'{
"instance_name": "'"$instance_name"'",
"trigger_inputs": {
"signerEmail": "'"${signer_email}"'",
"signerName": "'"${signer_name}"'",
"ccEmail": "'"${cc_email}"'",
"ccName": "'"${cc_name}"'"
}
}' >$request_data

Step 5. Call the Workflow Builder API

Now that you have constructed your API headers and request body, make a POST request using the url property obtained in Step 3 to trigger a new instance of the workflow. The url property will also include two query parameters: mtid and mtsec. These values are generated by the Workflow Builder service when the workflow is published and are used to authenticate the request and identify a valid workflow.

View the source code for this how-to on GitHub: Bash, C#, Java, Node.js, or PowerShell.

response=$(mktemp /tmp/response-wftmp.XXXXXX)
# The ${decoded_trigger_url} variable is extracted from the response from a previous API call
# to the Workflows: getWorkflowTriggerRequirements endpoint.
Status=$(curl -s -w "%{http_code}\n" -i --request POST ${decoded_trigger_url} \
"${Headers[@]}" \
--data-binary @${request_data} \
--output ${response})

Expected response

If you run this code example from our Quickstart or launcher projects, you should see a JSON response similar to the code shown. You will also receive an email at the address supplied in Step 4 to complete the workflow.

Sample response
{
"instanceId": "5be2a8ad-xxxx-xxxx-xxxx-f4e928383eb3",
"workflowInstanceUrl": "https://demo.services.docusign.net/aow-auth/v1.0/accounts/1caf2cc9-xxxx-xxxx-xxxx-14d63c1ced6b/workflows/execution?mtid=cb6a48f3-xxxx-xxxx-xxxx-f2bf10fafc39&mtsec=tnrwWLmJH8-xxxxxxxx-Kpy5nGkeQdjU4G7yj9I6yQM"
}

Step 6. (Optional) Embed the instance in an iframe

To present the workflow to your users, embed the instanceUrl in an iframe in your application. This can be done in any front-end environment that supports HTML. This iframe provides a complete browser-based workflow experience for your end users.

-->
<div>
<iframe src=$instanceUrl width=800 height=600>
</iframe>
</div>
<!--

Expected response

If the workflow was triggered and embedded correctly, your application should display the Workflow Builder workflow in an iframe. The embedded experience will load the first step of the workflow and allow the user to begin interacting with it directly in the browser.

A screenshot showing an embedded agreement workflow

Next steps