Using Ngrok for Webhooks and Mobile Development Testing

by Michael Szul on

No ads, no tracking, and no data collection. Enjoy this article? Buy us a ☕.

I've been working on a Bot Framework adapter for Twitter chatbots for a while now, and Twitter is loaded with a handful of (ever-changing) APIs and ways to interact with their data. One of these APIs is the Twitter Account Activity API, and this is the one you need to use for receiving notifications when an action occurs against a particular account. For example, if you want to know when somebody tweets at your account, you would register a webhook with this API.

Unfortunately, Twitter has multiple layers of complexity with their Account Activity API. You need to register a live webhook, and then you need to subscribe your account to it. With the free tier, you can only have one registered webhook, so you need to be able to remove a webhook when necessary. The problem is that none of these API interactions have a web interface. You have to register the webhook from the webhook URL, and you have to manage your subscriptions through it as well.

You can't do this from localhost… or can you?

Ngrok is a fantastic service that creates a tunnel between the Ngrok servers and your local machine, allowing you to expose your various local services to external access. This is incredibly convenient for mobile development, IoT hardware development, and of course, webhooks, such as the one needed for the Twitter Account Activity API.

It's also really easy to use. Once you sign up for an account, you download the Ngrok executable. From a command-line, you then type:

ngrok http 80
      

That's it. Ngrok will print to your screen a session status, the URL to a local web interface for inspecting traffic, and two forwarding addresses: One for HTTP and one for HTTPS.

Note: You can use Ngrok for more than just local web applications, and the output will be slightly different depending on what you want. This example is for a webhook.

Once you have the Ngrok service spun up, the forwarding URL will push traffic to your local service, and in the case of the Twitter Account Activity API, you can use this URL as the webhook URL, and Twitter will then ping this URL when an activity is performed against your subscribed Twitter account.

The biggest caveat to this is that when you stop the Ngrok service and spin it up again, it'll create a new, different subdomain URL for you to use. In the case of the Twitter Activity Account API, this means that you will have to re-register the webhook; however, if you upgrade to a paid plan, you can reserve a subdomain.