Using ngrok to test local websites and APIs
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 mimicking a device but it isn’t the same as an actual phone.
However, chances are 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, 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.
Ngrok couldn’t be simpler to install.
- Head over to ngrok.com and sign up for an account (it’s free).
- Download ngrok for your operating system. They support Windows, Mac (Intel and Arm), Linux (Intel and Arm) and FreeBSD.
- Unzip the downloaded zip file
- Connect ngrok to your account
./ngrok authtoken <auth token here>
Once Ngrok is installed it is simple to use.
- 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.
- Start a tunnel to your local port (8000 in this case)
./ngrok http 8000. You should see an output like this:
Ngrok gives you the HTTP and HTTPS address that you can connect to and you will see the incoming HTTP requests on the console.
From there you can access the service running locally on your phone, tablet or even give it to someone else to try out.
You can also access a Dashboard locally on port
4040 if you want to get a detailed look at the requests and responses going to your service.
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.
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.
Originally published at https://www.alexhyett.com on May 7, 2021.