DEV Community

Adam Smolenski
Adam Smolenski

Posted on

Need to test API webhooks on local host? ngrok is a great solution.

So recently I was trying to incorporate the mailgun api to keep track of user engagement with emails (did they just toss them out, like I usually do for certain mailing lists". So I wanted to add the built-in opened event from mailgun. But before deploying it into the real world I was trying to figure out how to get the webhook locally... You can't very well add localhost:3000 to the webhook, that just won't work.

Enter ngrok. It's a simple little tool that allows IP tunneling. So when you run the program, it will generate a secure url that will forward to your local host.

It's extremely simple and free for small use cases like mine was. It was limited to 40 connections a minute. So heck, I'd just keep my tests slow paced. The only thing I had to remember was in the end I was using a subdomain so that was just adjusting my routes on deploy. But on the $5/month plan it's quite simple.

To get started all you have to sign up. Download ngrok, on the mac I had to allow my terminal to open it. Mac has some fun security these days. But after you download you should unzip the file somewhere you will remember to access it. I put it in the base folder of the project I needed it, since it's a tool that will be commonly associated with these files.

When you finish that, ngrok will assign an auth token with your account. In the folder you threw ngrok file in you can just enter ./ngrok authtoken YOUR_TOKEN_HERE.
After that you are pretty much done. All you have to type in the terminal after that is ./ngrok http https://localhost:3000. You can throw in whatever port you want. I was using rails so 3000 was my backends default. After running that function, it will give you an informational display. It will give you the internet address in a form of https://SOMETHING.ngrok.io log request types and where they were headed. When I saw my POST request to /notification from mailgun popping in I was delightfully surprised at the ease of adding this new test tool to my arsenal. It also means I can also test outside network devices for other projects and just see how they load differently depending on networks.

For something so easy to implement it is vastly useful. Hopefully it's a useful program for you to use.

Top comments (0)