Awesome Open Source
Awesome Open Source

ngrok example workflow file path TypeScript compatible npm npm



npm install ngrok
const ngrok = require('ngrok');
(async function() {
  const url = await ngrok.connect();


npm install ngrok -g
ngrok http 8080

This module uses node>=8.3.0 with async-await. For callback-based version use 2.3.0

For global install on Linux, you might need to run sudo npm install --unsafe-perm -g ngrok due to the nature of npm postinstall script.


You can create basic http-https-tcp tunnel without authtoken. For custom subdomains and more you should obtain authtoken by signing up at Once you set it, it's stored in ngrok config and used for all tunnels. Few ways:

await ngrok.authtoken(token);
await ngrok.connect({authtoken: token, ...});


const url = await ngrok.connect(); // -> http://localhost:80
const url = await ngrok.connect(9090); // -> http://localhost:9090
const url = await ngrok.connect({proto: 'tcp', addr: 22}); // tcp://
const url = await ngrok.connect(opts);


const url = await ngrok.connect({
	proto: 'http', // http|tcp|tls, defaults to http
	addr: 8080, // port or network address, defaults to 80
	auth: 'user:pwd', // http basic authentication for tunnel
	subdomain: 'alex', // reserved tunnel name
	authtoken: '12345', // your authtoken from
	region: 'us', // one of ngrok regions (us, eu, au, ap, sa, jp, in), defaults to us
	configPath: '~/git/project/ngrok.yml', // custom path for ngrok config file
	binPath: path => path.replace('app.asar', 'app.asar.unpacked'), // custom binary path, eg for prod in electron
	onStatusChange: status => {}, // 'closed' - connection is lost, 'connected' - reconnected
	onLogEvent: data => {}, // returns stdout messages from ngrok process

Other options: name, inspect, host_header, bind_tls, hostname, crt, key, client_cas, remote_addr - read here

Note on regions: region used in first tunnel will be used for all next tunnels too.


The ngrok and all tunnels will be killed when node process is done. To stop the tunnels use

await ngrok.disconnect(url); // stops one
await ngrok.disconnect(); // stops all
await ngrok.kill(); // kills ngrok process

Note on http tunnels: by default bind_tls is true, so whenever you use http proto two tunnels are created - http and https. If you disconnect https tunnel, http tunnel remains open. You might want to close them both by passing http-version url, or simply by disconnecting all in one go ngrok.disconnect().


You can use ngrok's configurations files, and just pass name option when making a tunnel. Configuration files allow to store tunnel options. Ngrok looks for them here:

OS X	/Users/example/.ngrok2/ngrok.yml
Linux	/home/example/.ngrok2/ngrok.yml
Windows	C:\Users\example\.ngrok2\ngrok.yml

You can specify a custom configPath when making a tunnel.


When tunnel is established you can use the ngrok interface to inspect the webhooks done via ngrok. Same url hosts internal client api. You can get it as wrapped request and manage tunnels yourself.

const url = await ngrok.connect();
const api = ngrok.getApi();
const tunnels = await api.get('api/tunnels');

You can also get it as string url

const url = await ngrok.connect();
const apiUrl = ngrok.getUrl();


  • If you are behind a corporate proxy an have issues installing ngrok, you can set HTTPS_PROXY env var to fix it. Ngrok's postinstall use the got module to fetch the binary and the hpagent module to support HTTPS proxies. You will need to install the hpagent module as a dependency
  • If you are using a CA file, set the path in the environment variable NGROK_ROOT_CA_PATH. The path is needed for downloading the ngrok binary in the postinstall script.

how it works

npm install downloads ngrok binary for your platform from official ngrok hosting. To host binaries yourself set NGROK_CDN_URL env var before installing ngrok. To force specific platform set NGROK_ARCH, eg NGROK_ARCH=freebsdia32

First time you create tunnel ngrok process is spawned and runs until you disconnect or when parent process killed. All further tunnels are created or stopped by using internal ngrok api which usually runs on

ngrok binary update

If you would like to force an update of the ngrok binary directly from your software, you can require the ngrok/download module and call the downloadNgrok function directly:

const downloadNgrok = require('ngrok/download');
downloadNgrok(myCallbackFunc, { ignoreCache: true });


Please run git update-index --assume-unchanged bin/ngrok to not override ngrok stub in your pr. Unfortunately it can't be gitignored.

Test suite covers basics usage without authtoken, as well as features available for free and paid authtokens. You can supply your own tokens into env vars, otherwise warning given and some specs are ignored (locally and in PR builds). Travis supplies real tokens to master branch and runs all specs always.

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
javascript (67,992
tunnel (82
webhook (61
ngrok (30
localhost (17

Find Open Source By Browsing 7,000 Topics Across 59 Categories