Use JSON response values of a webhook trigger action as variables
Read Time: 20-60 Minutes
Skill Level: Advanced
Keywords: Integrations, Automations, API, Webhook, Codeless, Workflows, REST, Zapier
One of the advanced functionalities of triggers is the ability to push and pull data from webhooks, this allows for extremely powerful and dynamic integration with other platforms. Any platform with an industry standard CRUD or REST API - or any platform which you can interact with using HTTP requests can be communicated with via triggers.
You are able to send many types of data from a ticket into an external API using the webhooks action, you are also able to process the data these webhooks return.

This is a somewhat technical concept - so in this article, I will use a common use case example, of a process used to book flight tickets (or any other appointment booking example) to scope the feature out. In reality, the scope of this feature is infinite.
Pre-requisites: An outbound webhook which returns a response.
The basis of this functionality assumes you have a trigger, which has a single action to call a Webhook.
You are able to use this guide to send the end-users data to an API which accepts occur: https://support.deskpro.com/en/kb/articles/web-hook-action-data-format
When the webhook is called, its response is logged into the ticket log. This can be viewed either by clicking on the ticket History, or examining the ticket_log in the ticket debug file.

When referencing the above response, we use different variables - you could replace the value
part with booking_status
in these variables, to render the string 'success'.
In twig:
{{ context.user_vars.options.webhook.value }}
In a trigger variable, such as “Check Expression”:
context.getUserVars().webhook["value"]
In an outbound webhook (trigger action):
{{ user_vars.webhook.value}}
1. Passing a variable from a webhook response into an email template (twig)

You may then receive a response from the API, to confirm whether the booking has been successful. You are able to capture this response, and use it within an email template like so.
Twig variable: {{ context.user_vars.options.webhook.json_key_or_name }}
Rendering the JSON value directly into the email:
If you refer to diagram (d.2) above, you will see we can pull the status directly from the value of the JSON response, like so : {{ context.user_vars.options.webhook.booking_status }}
For example, in a email template, you could create a new trigger, lower down to send a custom email:
<p> Hello, your booking was a <b> {{ context.user_vars.options.webhook.booking_status }} </b> - this was confirmed at {{ context.user_vars.options.webhook.timestamp }} </p> copy
This would render for the user:
Hello, your booking was a success - this was confirmed at 2022-01-11 12:16:37
Taking this a step further with {% if %} statements:
You can make your email more personalised, and dynamic, if you know the types of responses your API will give.
<p> Hello, </p>
{% if {{ context.user_vars.options.webhook.booking_status }} == 'success' %}
<p>Your booking was a success! Bon Voyage!</p>
{% if {{ context.user_vars.options.webhook.booking_status }} == 'failed' %}
<p>Your booking has failed, as there were no seats available. Please look at another flight option.</p>
{% else %}
<p>There was a problem with your booking, do not attempt to rebook as you may be charged twice. Please contact support.</p>
{% endif %}
<p> This was confirmed at {{ context.user_vars.options.webhook.timestamp }} </p> copy
In this example, we have 3 scenarios. One, where we know the plane ticket was booked, and we email a customer to confirm. Two, where we know the booking has failed, and there are no seats available. And three, where there may have been some issue, therefore the customer is prompted to contact support.
2. Using a value from a JSON webhook response as a variable within a “Check Expression” (regex) criteria

failed
, we would like to notify a supervisor and leave a note on the ticket.To achieve this, we are going to use “Check expression [expert]” function in a trigger. This trigger should exist after the original trigger which pulled the data from the webhook.

context.getUserVars().webhook["booking_status"] matches "/failed/"
This can be constructed in many ways, for example, we could ask it to trigger upon any state which “is not a success” by changing the trigger structure like below.

3. Taking a value from a JSON webhook response, then POSTing it out to another webhook.

Further to the above two examples, the values in the response from a webhook, can be sent out to another external API via another webhook action. The variable for this is as follows: {{ user_vars.webhook.value }}
In practice, this can look something like this as a trigger action.

{
"entry_type": "new_entry"'
"tracking_status": "{{ user_vars.webhook.booking_status}}",
"ticket_id": "{{ ticket.id }}",
"customer_email": "{{ ticket.person.primary_email.email}}"
} copy
In the above example, a new entry will be made in the tracking/reporting system, with the status (failed, success, other etc.), the numerical ticket ID, and the customers email address, for reporting the later. You can see how we can use different variables to pull user values, ticket values as well as webhook variables.
The scope of this is extremely, you could post any data from the response, into any platform with an API, for example, this could be tweaked to post a Calender Booking into the Google Calender API.
Please log in or register to submit a comment.