Skip to main content

Download & Install

The fastest way to put anything on the internet.

JavaSDK

1

Get started with Java

Clone the ngrok Java SDK example:

git clone git@github.com:ngrok/java-sdk-example.git && cd java-sdk-example

Add the ngrok Java SDK to your pom.xml:

pom.xml

<dependency>		<groupId>com.ngrok</groupId>		<artifactId>ngrok-java</artifactId>		<version>1.1.1</version></dependency><dependency>		<groupId>com.ngrok</groupId>		<artifactId>ngrok-java-native</artifactId>		<version>1.1.1</version>		<classifier>${os.detected.classifier}</classifier>		<scope>runtime</scope></dependency>

Add ngrok to your app:

App.java

import com.ngrok.Session;import java.net.URL;  void connectNgrok() throws Exception {		var session = Session.withAuthtokenFromEnv().connect();		var forwarder = session.httpEndpoint()				.forward(new URL("http://localhost:8085"));		System.out.println("Available at: " + forwarder.getUrl());}
2

Run your app

NGROK_AUTHTOKEN="<YOUR_AUTHTOKEN>" mvn compile exec:java

Don’t have an authtoken? for a free account.

Open your ngrok URL in a browser to see it working!

You’re all set. What’s next?

String tp = """on_http_request:	# redirect users to Google to log in	- actions:		- type: oauth			config:				provider: google 	# allow logins *only* from acme.com	- expressions:		- "!actions.ngrok.oauth.identity.email.endsWith('@acme.com')"		actions:		- type: deny"""; var forwarder = session.httpEndpoint().trafficPolicy(tp).forward(new URL("http://localhost:8085"));
String tp = """on_http_request:	# verify webhook signatures from your provider	- actions:		- type: verify-webhook			config:				provider: github				# access your signing key from a secure ngrok vault				# no cleartext secrets in your policy, friend				secret: ${secrets.get('webhook-vault', 'github-secret')}"""; var forwarder = session.httpEndpoint().trafficPolicy(tp).forward(new URL("http://localhost:8085"));
String tp = """on_http_request:	# redirect users to your OpenID provider to log in	- actions:		- type: openid-connect			config:				issuer_url: https://accounts.google.com				# access your OIDC details from a secure ngrok vault				client_id: ${secrets.get('auth-vault', 'oidc-client-id')}				client_secret: ${secrets.get('auth-vault', 'oidc-client-secret')}				scopes:					- openid					- email"""; var forwarder = session.httpEndpoint().trafficPolicy(tp).forward(new URL("http://localhost:8085"));
String tp = """on_http_request:	- actions:		- type: basic-auth			config:				credentials:					# add up to 10 username:password pairs, all stored safely in a vault					- user01:${secrets.get('basic-auth-vault', 'password01')}					- user02:${secrets.get('basic-auth-vault', 'password02')}"""; var forwarder = session.httpEndpoint().trafficPolicy(tp).forward(new URL("http://localhost:8085"));

Inspect every detail of your traffic

Watch the flow in real time, then dig into the headers, body, latency, response, and more for every request.

Peruse the Java SDK quickstart

Follow along to embed ngrok into your app and use Traffic Policy for auth and traffic transformation.

Bring your own domain

Paid feature

Create a DNS CNAME record to use your own domain name for your endpoint URL.

static void connectNgrok() throws IOException, InterruptedException {		try (var session = Session.withAuthtokenFromEnv().connect()) {				var forwarder = session.httpEndpoint()						.domain("app.acme.com")						.forward(new URL("http://localhost:8085"));		}}

Add load balancing

Endpoint Pooling lets you automatically distribute traffic among any number of replicas of your app. It isn't currently supported by the Java SDK — enable it with the ngrok agent CLI or config file.