Sync/async execution of a specific workflow with an integration trigger
  • 20 Feb 2024
  • 6 Minutes to read
  • Dark
    Light

Sync/async execution of a specific workflow with an integration trigger

  • Dark
    Light

Article Summary

Webhooks are used in Torq to trigger workflows with events from third-party vendors. When you create a trigger integration (of a specific third-party vendor or a generic webhook), you get a webhook URL (endpoint) that you can provide to any third-party vendor to get events from them. The integration can be set as a trigger for any number of workflows.

You can select an extended version of an integration webhook URL (endpoint) to trigger a specific workflow. You can also select whether to execute it synchronously or asynchronously. For synchronous executions, you can use output parameters to modify the response status, body, and headers according to your needs.

Note

All three trigger options: Webhook URL, Asynchronous URL, and Synchronous URL, are available for all trigger integrations (vendor integrations and generic).

When to use

  • Synchronous executions enable external systems to trigger a specific workflow and get a fully customizable response based on the workflow logic and output: define the status, body, and headers of the response returned by Torq according to the third-party requirements.
  • Asynchronous executions can be used to test a new or modified workflow. Trigger only the workflow you want to test with a predetermined trigger event and check that the execution output matches the expected output.

Select a trigger type

Select a trigger execution type

  1. Open the workflow you would like to trigger.
  2. Select the workflow trigger. Click on Webhook URL to get all the trigger types.
  3. Select a trigger type:
    • Webhook URL: trigger all workflows that are using this trigger integration.
    • Asynchronous URL: trigger only this specific workflow. The response to the execution request will include the workflow execution ID, which can be used to periodically check whether the execution is complete and the execution output is available. The URL structure is: <integration webhook>/workflows/<workflow ID>. For example: https://hooks.torq.io/v1/webhooks/af6aab8d-****-****-****-74259a93b18f/workflows/710c5349-b617-****-****-e15671ca4ffb
    • Synchronous URL: trigger only this specific workflow. The response to the execution request will be returned when the execution is complete, and it will include the execution output. The HTTP response is customizable. The URL structure is: <asynchronous URL>/sync
  4. Click the copy icon to copy the selected URL.
Note
The workflow will be executed in production using the latest published version. If the workflow doesn't have a published version, it won't be executed.

Execute the workflow

Provide the URL you copied (synchronous or asynchronous) to a third-party vendor to trigger the specific workflow when receiving an event. Alternatively, you can use the URL to trigger the workflow manually, simulating the automatic trigger.

Below is an example of the cURL syntax to trigger the workflow.

Note

The cURL command accounts for authentication headers the webhook may have. We recommend using authentication headers to improve security and prevent unauthorized access to your data. 

curl <Synchronous URL or Asynchronous URL> -H "auth_header_name:secret" -d '{"body": [1,2,3]}'

The trigger event for this example is:

{
  "body": [
    1,
    2,
    3
  ]
}

Get the execution output

Getting the output differs according to whether the workflow execution is synchronous or asynchronous.

Synchronous execution

The response to the execution request from the previous section will contain the execution output and will be returned as soon as it's available. If the execution takes more than 50 seconds to complete, the Execution is in progress response and the execution ID will be returned after 50 seconds. Use the execution ID to retrieve the execution status as you would for asynchronous execution. Note that you can use output parameters to modify the HTTP response according to your needs.

Asynchronous execution

  1. The response to the execution request from the previous section will contain the execution ID: {"execution_id":"c9a0769f-b8a3-****-****-b57c138b5523","status":"ok"}
  2. Use the Torq public API retrieve an execution call to continuously check the execution status until the execution is complete and the output is available.
curl --request GET \
  --url https://api.torq.io/public/v1alpha/executions/<execution_id> \
  --header 'Authorization: Bearer <bearer_token>' \
  --header 'accept: application/json'

Synchronous execution: use output parameters to determine the HTTP response

By default, the workflow execution output will contain the output parameters and values or JSON object defined in the Exit operator.

There are three parameter names you can use to modify the HTTP response:

Note
The parameters below (body, status, headers) will only modify the HTTP response for synchronous executions.
Parameter nameDescriptionHow to use
bodyThe value will be returned as the response body.If this parameter exists, other parameter values will not be included in the execution output. Make sure the parameter type matches the data provided as the value.
statusThe value will be returned as the response status code.The status can be specified in either string or number formats. The value must be a valid HTTP status code.
headersThe value will be added to the HTTP response headers.The parameter type must be JSON. The value must consist of valid key-value pairs.

Below is an example of how these parameters can be added to an Exit operator.
Use output parameters to affect the HTTP response

The parameter values determine the HTTP response.
HTTP response

Content types

To have the HTTP response interpreted in a format other than the default application/json, you can override the Content-Type header and specify the desired format. Refer to the example above.

These are the supported content types:

  • application/json - the default format
  • text/csv
  • text/plain
  • text/html
  • text/xml
  • Other - for additional content types, contact Torq support.

Example use case: handling CORS requests with synchronous executions

Cross-Origin Resource Sharing (CORS) is an HTTP header mechanism allowing a server to specify origins other than its own from which a browser can load resources. It involves an initial request to the server to confirm permission for the actual request, with the browser sending headers indicating the intended HTTP method and headers for the request.

  1. Use a webhook integration that can accept raw HTTP requests as the workflow trigger.
  2. In order to handle OPTIONS requests differently than HTTP requests, add a condition checking the HTTP method: {{ $.event.http_method }}
  3. Use two Exit operators to adjust the response according to the HTTP method.

Use sync webhooks to respond to CORS calls

Sync/async execution of a shared workflow

For a workflow that's shared with your workspace, you won't have the option to copy the Webhook URL as explained above. Instead, you'll need to obtain the Webhook URL from the workflow source workspace, or you can ask an Owner from that workspace to send it to you. Be sure to update the workflow ID to match the one in your workspace.

Troubleshooting

Below are the responses you can get when executing the workflow with the extended webhook or when trying to retrieve the execution.

StatusCodeDescription
Success200
Execution is in progress201Synchronous execution didn't complete yet.
Invalid integration ID400The integration ID format is invalid.
Invalid workflow ID400The workflow ID format is invalid. If a workflow with the ID exists, ensure the workflow is triggered by the specified integration.
Invalid header400Incorrect header name or the header secret doesn't match the header name.
Invalid HMAC signature401
Execution not found404No execution to retrieve. Ensure no trigger condition prevents the workflow execution with the specified event. 
Integration not found404No integration with the specified integration ID.
Workflow not found404No workflow with the specified workflow ID. Make sure the workflow has a published version.
Payload is too large413The event body payload is too large.
Rate limit exceeded429Workspace execution limit is 25 per second. 
Internal error500Retry the request.

Was this article helpful?

What's Next
Changing your password will log you out immediately. Use the new password to log back in.
First name must have atleast 2 characters. Numbers and special characters are not allowed.
Last name must have atleast 1 characters. Numbers and special characters are not allowed.
Enter a valid email
Enter a valid password
Your profile has been successfully updated.