Webhooks exercise

Learn how to create, trigger, and manage webhook-initiated scenarios.

Exercise overview

The purpose of this scenario is to create an app to sell to convenience stores so they can easily determine whether or not a customer is old enough to purchase alcohol. The cashier simply needs to post the name and birthdate of the customer to a URL they have been provided. That post will trigger the scenario which will calculate the answer and return it to the requestor.

  1. The scenario consists of three webhooks.

  2. The trigger module is a custom webhook that listens for a post.

  3. When it receives a post it will output it to one of the next modules.

  4. The next module returns a response to the requestor.

    Webhooks Image 1

Steps to follow

Set up the trigger webhook.

  1. Create a new scenario and name it “Using webhooks.”

  2. For the trigger, add the Custom webhook module from the Webhooks app.

  3. Click on Add to create a new Webhook.

  4. Enter the Webhook name of “Drinking age app.”

  5. Leave IP restrictions blank, which means that anyone can send data into it.

  6. Click Save.

    Webhooks Image 2

  7. Back in the Webhooks mapping panel a URL has been created for this specific webhook. Click on “Copy address to clipboard” to copy that URL.

  8. Click OK.

  9. Click Run once.

  10. Use the URL in Postman to send a name and birthdate to your custom webhook. For instructions on setting up Postman, see the Webhooks walkthrough tutorial.

    The Webhooks module panel should look like this:

    Webhooks Image 3

    The webhook is now in a state where it’s listening for data to determine the data structure.

  11. You can define the data structure of the payload that you expect to get (data structures will be discussed later). If you don’t define a data structure Fusion will determine the data structure automatically when the post is sent.

  12. On the Postman side you want to send to the copied URL. The post should include basic form data. For this example you need three fields: Name, Birthdate, and clientToken.

    Webhooks Image 4

  13. After you click Send from Postman you should get an indication that the post has been accepted.

  14. This is the point where your scenario will show that the data structure has been successfully determined.

  15. You can see that the data has been received by opening the execution inspector.

    Webhooks Image 5

    Set up routing for client tokens.

  16. Add a router to the trigger module.

  17. In the upper path, add a Webhook response module. This will be our path for when the client token does not match.

  18. Set the status to 401.

  19. Set the Body to {“error”: “Failed to authenticate request. Please check your clientToken”}.

    Webhooks Image 6

  20. Create a filter between the router and the Webhook response module. Name it “Client token does not match.”

  21. For the Condition, use the clientToken field from the trigger module and do a numeric “Not equal to” comparison to the number 5121933.

    Webhooks Image 7

  22. In the bottom path, add another Webhook response module. This will be our path for when the client token does match.

  23. Set the status to 200.

  24. In setting up the Body, use the mapping panel functions to test to see if the person is 21 or older. If they are, return “You are old enough to drink!”, otherwise return “You are out of luck…”

    Webhooks Image 9

  25. Create a filter between the router and the Webhook response module on the lower path. Name it “Client token does match.”

  26. For the Condition, use the clientToken field from the trigger module and do a numeric “Equal to” comparison to the number 5121933.

    Webhooks Image 8

  27. Click on the Scheduling button under Run once to activate your scenario so that any time there is a new post it will be received, go down either path, and generate a response.

recommendation-more-help
c9fbcf61-6d19-481e-a9ab-f54a0ae0ee8a