• Updated
Implement mutual TLS (mTLS) at an API gateway
06-05-2026: We updated this blog post to reflect ngrok’s current architecture, including Cloud Endpoints, the
terminate-tlsTraffic Policy action, and Endpoint Pools for load balancing.
When developing services that require mutual TLS (mTLS) with an API gateway, your infrastructure and certificate management challenges start mounting straightaway. It’s hard to set up the proper local environment to test how your API responds to an mTLS implementation, and once you do, that environment rarely carries over to your production infrastructure.
You need to protect the data made available on your API, whether you’re dealing with Internet of Things (IoT) devices or sensitive financial data, but how can you get the job done without implementing an API gateway twice? At least?
Let’s explore how ngrok’s API gateway supports your mTLS-enabled service throughout the API lifecycle, from development to production. Whether you use an Agent Endpoint from the CLI for local development or a persistent Cloud Endpoint for production, the configuration stays the same—a single Traffic Policy YAML.
What is mutual TLS?
Mutual TLS is a bidirectional security protocol that builds on the standard Transport Layer Security (TLS). Unlike TLS, where only the client has to authenticate the server before communicating with the server, mTLS requires both the client and the server to authenticate each other—a basic implementation of a zero-trust policy. When your API trusts no one by default, even those already in the network, and they must prove who they claim to be, you have strong guarantees your data only moves toward the right consumers.
Before we discuss how mTLS works with an API gateway, let’s take a 101-level look at TLS’s building blocks.
Public and private keys
Private and public keys are crucial in cryptography, particularly in asymmetric encryption. A public key is a cryptographic key that you can use to encrypt messages or verify digital signatures. As the name suggests, you can share it widely with anyone.
A private key is one you keep secret. You can use a private key to make digital signatures and to decrypt messages encrypted with the matching public key. Anyone who gets a copy of it can sign as you, so exposing it lets an attacker impersonate you.
Certificate authority
A certificate authority (CA) is a widely trusted entity that verifies the identities of various entities and issues digital certificates to facilitate encrypted communications over networks. On top of verification and issuance, CAs also maintain and manage the lifecycles of digital certificates, including renewing and revoking them.
TLS certificate
A TLS certificate (X. 509) is a digital document that matches a public key to an entity’s identity using a digital signature from a certificate authority. It captures key information, such as the algorithm used to create the certificate’s signature, the entity (usually a CA) that issued and signed the certificate, the certificate’s validity, and the entity identified by the certificate.
Why use mTLS for your API?
With standard TLS and an API gateway, the API consumer starts a TLS handshake. The client and the gateway trade one-time key shares in their hellos and derive the session keys from them, not from the certificate, and those keys encrypt the rest of the handshake. The gateway then sends its certificate along with a signature made with its private key. The client checks that the certificate chains to a CA it already trusts, then checks the signature against the public key in the certificate.
That’s great, but it only protects the consumer. The API gateway, and your upstream API service, has no way of knowing who the consumer is or whether they should be allowed access to the data.
Mutual TLS starts the same way, and then the gateway asks for the client’s certificate too. The client sends its certificate and signs the handshake with its own private key. The gateway checks the certificate against the CA it trusts for clients and checks the signature, and it rejects the connection if either check fails.
When paired with an API gateway, mTLS also:
-
Creates a baseline mechanism for authentication that’s more durable than basic username/password.
-
Identifies and authenticates clients that can’t log in using other means, such as machine-to-machine (M2M) processes or IoT devices.
-
Prevents malicious requests to your API aiming to extract data you need to keep secure.
-
Stops attacks that rely on a stolen or guessed password or API key, like spoofing and brute force, because an API consumer must hold a CA-signed certificate and its private key to complete the mTLS handshake. A stolen certificate and private key still work until the certificate expires, because ngrok doesn’t check revocation. Issue short-lived client certificates, or deny a leaked one by its serial number.
-
Ties every connection to a specific client certificate, so your logs can record which consumer made each request.
mTLS with ngrok’s API gateway
ngrok is a full API gateway with built-in mTLS, so you can authenticate both ends of every connection without standing up your own reverse proxy or PKI tooling. It offers two types of endpoints for your API gateway:
- Agent Endpoints are created by the ngrok CLI, SDKs, or Kubernetes Operator. They’re ideal for development and testing, and they live as long as the agent runs.
- Cloud Endpoints are persistent, always-on endpoints managed entirely from the ngrok dashboard or API. They’re the recommended approach for production API gateways because they stay up independent of any single agent process.
Both endpoint types support the same Traffic Policy configuration for mTLS. The instructions below use the CLI (Agent Endpoints), but the Traffic Policy YAML works identically on Cloud Endpoints.
To get started, you’ll need just two things:
-
An ngrok account.
-
ngrok installed where your API service(s) run—see our quickstarts for the agent CLI, Go SDK, Rust SDK, or Kubernetes Operator, although the instructions from here on out are for the CLI.
Generate your certificates
If you don’t yet have a CA and certificates, you can create them using OpenSSL, starting with the CA private key (ca.key ).
Next, generate an x509 self-signed certificate for the CA.
With the CA keys established, you can generate a certificate for your client.
First, generate a new key with OpenSSL, but with a different name: client.key.
Then generate the client’s certificate signing request (CSR).
Sign the client’s request using the CA’s certificate and generate an x509 certificate.
Configure your ngrok API gateway for mTLS
You now configure features in ngrok like mTLS using our Traffic Policy engine, which lets you filter, take action on, and orchestrate traffic using the CEL configuration language.
The easiest way to use Traffic Policy on an API gateway and the ngrok agent is with a YAML file that specifies the phase on which you want to run specific actions.
Create a file called mtls.yaml on the system where you’ll run the ngrok agent and paste in the following, also pasting in the contents of ca.crt in place of ... certificate ... .
The min_version line accepts only TLS 1.3 connections, so client certificates always travel encrypted.
Start the ngrok agent to create an Agent Endpoint for your API service, referencing the Traffic Policy file you just created.
This creates an Agent Endpoint under an automatically-assigned domain. You can also bring your own domain to ngrok, or create a Cloud Endpoint with the same Traffic Policy for a persistent, always-on API gateway.
Verify mTLS is configured correctly
To ensure you’ve correctly configured mTLS, send a request via curl to the endpoint without passing your client’s certificate. You’ll get back not a HTTP error response or an ngrok error code, but a TLS error triggered before your request even reaches your ngrok endpoint.
When you pass the client certificate in your request, you can access your API—proof you’ve successfully implemented mTLS with ngrok’s API gateway!
Want a quick mTLS + API gateway demo using a sample repo?
We have a simple API service at ngrok-samples/api-demo on GitHub you can play around with to validate how enabling mTLS works on ngrok’s API gateway.
By default, this API runs locally on port 5000.
Next, fire up your ngrok agent to create a new Agent Endpoint, which acts as your gateway between this API and the public internet—you can use the same certificates and Traffic Policy file you created just above.
Finally, you can use curl to make a request entirely secured using mTLS.
What’s next after mutual TLS and your API gateway?
Deploy your first API gateway with mTLS in just a few minutes—start by grabbing yourself a free ngrok account to help you meet security best practices, whether you’re working in a highly regulated industry or want peace of mind for your API project.
While deploying an API gateway with ngrok, you’re also saving yourself the hassle of configuring web servers, deploying service meshes on Kubernetes, or rolling out custom DNS. ngrok removes all barriers to entry enabling mTLS on any API gateway project, getting you to production without complex reverse proxies or wrestling with your ops team for weeks.
Once you have mTLS enabled, there’s more you can do with ngrok’s API gateway:
- Scale with Endpoint Pools: Need to load balance across multiple instances of your mTLS-secured API? Endpoint Pools let multiple agents share the same URL with automatic load balancing—just add
--pooling-enabledto your ngrok command. - Go persistent with Cloud Endpoints: Move from Agent Endpoints to Cloud Endpoints for a production API gateway that stays up independent of any single agent process.
- Terminate TLS however (and wherever) you want with Traffic Policy
- Put your APIs online the easy and composable way
- Secure your API with JWTs and ngrok’s API Gateway
Have some strong thoughts about ngrok’s API gateway? We’d love to see them in our Community Repo—the home for all discussions, bug reports, and feedback about your mTLS and API gateway experience.