Mutual TLS (mTLS) authentication requires the client to present a certificate alongside the server’s certificate verification, allowing both ends of the connection to authenticate each other. Undici supports client certificate authentication through theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/nodejs/undici/llms.txt
Use this file to discover all available pages before exploring further.
connect option on Client and Agent. The certificates must be signed by a certificate authority (CA) that the server trusts, and you configure the CA, client certificate, and private key as file paths or PEM strings.
How client certificate auth works
Server requests a certificate
The HTTPS server is configured with
requestCert: true, which instructs it to ask the client for a certificate during the TLS handshake.Client presents its certificate
Undici sends the
cert and key specified in the connect option during the TLS handshake.Server validates the certificate
The server checks the client certificate against its configured
ca. If rejectUnauthorized: false is set, the server handles validation errors itself rather than dropping the connection immediately.Configuring a Client with a client certificate
Pass a connect options object to the Client constructor containing the TLS material. The example below shows a complete setup including both a server (for illustration) and the undici Client:
Client certificate authentication
connect option reference
The connect object passed to Client or Agent supports all of Node.js’s tls.connect options. The most commonly used are:
| Option | Type | Description |
|---|---|---|
cert | string | Buffer | string[] | Client certificate in PEM format |
key | string | Buffer | string[] | Private key for the client certificate |
ca | string | Buffer | string[] | CA certificate(s) to trust |
passphrase | string | Passphrase for an encrypted private key |
rejectUnauthorized | boolean | Whether to reject servers with invalid certificates (default true) |
servername | string | SNI (Server Name Indication) hostname override |
timeout | number | TLS handshake timeout in milliseconds |
Using Agent for multiple hosts
If your application communicates with multiple servers that all require client certificates, configure an Agent instead of a Client. The connect option on Agent applies to every connection it manages.
Agent with client certificate
Encrypted private keys with a passphrase
If your private key is passphrase-protected, include thepassphrase option alongside the key:
Passphrase-protected private key
Custom TLS with buildConnector
For advanced use cases — such as dynamically choosing a certificate based on the target host, or combining client certificates with other custom TLS logic — use buildConnector from undici to create a custom connector function and pass it to connect:
Custom connector with buildConnector
Self-signed certificates in development
During local development with a self-signed CA, setrejectUnauthorized: false and provide your local CA so that undici can still verify the chain (even though it is not trusted by the system):
Development self-signed CA
Generating certificates for testing
A typical test setup usesopenssl to generate a self-signed CA, server certificate, and client certificate. Here is the minimal command sequence:
Generating test certificates with openssl