How to use MessageMedia Webhooks

Imagine this scenario – you’re living in the late 90s when Netflix was a myth and you’re eager to watch this mind-boggling movie called The Matrix. What do you do? You call the local VCR rental shop and ask them if they have a copy available. They say they don’t have a copy yet, but they will call you as soon as they have one available for rent. You’ve basically subscribed to be notified by the store so rather than calling them every day and enquiring about it, you’ve asked them to notify you instead. This is the core concept of Webhooks.

Fortunately, we’re not living in the 90s and we’ve all watched the Matrix (you have, haven’t you?). The purpose of that example was to give you an idea of what webhooks are and to make it even easier to understand and digest, we’re going to be writing some awesome code.

This tutorial will cover:

  • Creating a webhook
  • Retrieve a webhook
  • Updated a webhook
  • Delete a webhook

If you get stuck on any section at any time, you can look at the code in this repository to help you out.

 

Prerequisites

In order to work through this tutorial, you need:

 

Installing Webhooks Node.js SDK

For this part, you’ll need a code editor such as Sublime Text or Atom. We’ll be using Node.js to develop our app, so you’ll need to make sure you’ve installed it on your machine as well. Now create an empty folder for your project, let’s name it “testApp”. Now, open up your command prompt, cd to the directory and run the following command to install the Webhooks SDK:

npm install messagemedia-webhooks-sdk

 

Creating a webhook

Create an index.js file inside that folder and open it up in your editor. Write down the following code. I’ve commented it, so you can understand what’s going on with each line.

We’ve basically added code to create a webhook for the URL http://webhook.com that will be triggered when an SMS is received. The template attribute decides what will be returned via the response. For our example, the id and the status of the message will be sent back.

Don’t forget to replace the placeholders with your actual credentials and then call the function at the end of your file using:

createWebhook();

You can run the file using this command:

node index

This is what a successful response body should look like.

 

Retrieving all webhooks

We can find out all the details of the previously created webhooks by making a GET request to the same endpoint. The SDK has already done the heavy lifting for us so all we’ll need to do is to call the retrieveWebhook function.

And a successful response for this request should look like this.

 

Updating a webhook

To update a webhook, you’ll need the id of the webhook. You can retrieve this by looking at the response of the above example which should display the id of each webhook along with other details.

And the code for this section is:

Add this line to call the function:

updateWebhook();

And a successful run of the script should result in something similar being shown in the console.

 

Delete a webhook

Similar to the updateWebhook method, you’ll need an id to be able to delete a webhook. Once you’ve got it, replace it with the WEBHOOK_ID placeholder in the file and you’re ready to go.

And a successful response should output null in the command line:

 

Conclusion

Great job – you have learnt how to use the MessageMedia Webhooks.

 

Get the code

All the code for this tutorial is available in the Webhooks Node.js Tutorial Github repo

 

Resources