Using ngrok to test local websites and APIs

Alex Hyett - May 28 '21 - - Dev Community

Often when I am creating a new website, I want to see how it is going to look on an actual device like my phone or tablet. You can use Chrome Web Tools for mimicing a device but it isn’t the same as an actual phone.

Chances are however that if you try and access the service using your IP Address on another device it will likely be blocked by your operating systems firewall. If you want to share the website or API with someone not on your network then you need to jump through lots of hoops such as forwarding the port on your router.

This is where ngrok comes in. It allows you to expose your local service or website over a secure tunnel.

Installing Ngrok

Ngrok couldn’t be simpler to install.

  1. Head over to ngrok.com and sign up for an account (it’s free).
  2. Download ngrok for your operating system. They support Windows, Mac (Intel and Arm), Linux (Intel and Arm) and FreeBSD.
  3. Unzip the downloaded zip file
  4. Connect ngrok to your account ./ngrok authtoken <auth token here>

Using Ngrok

Once Ngrok is installed it is simple to use.

  1. First start up your application so that it is running on your local machine. If I run this Gatsby website locally it runs on port 8000.
  2. Start a tunnel to your local port (8000 in this case) ./ngrok http 8000. You should see an output like this:

Ngrok WindowNgrok gives you the http and https address that you can connect to and you will see the incoming HTTP requests on the console.

From their you can access the service running locally on your phone, tablet or even give it to someone else to try out.

ngrok on iPhone

You can also access a Dashboard locally on port 4040 if you want to get a detailed look of the requests and responses going to your service.

ngrok dashboard

Ngrok is free for single use quick demos, if you need something more such as custom domains then you will need the paid account which starts at $5 per month.

Final Thoughts

Ngrok is one of those services that every developer should have in their toolbox. The dashboard is especially useful if you want to monitor the requests going to your service without another tool like fiddler. The free plan is good enough for most use cases, if you need more than the paid options are reasonable.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .