TLS client connections
A backend must call an HTTPS-compatible service directly over an encrypted transport.
Node.js · Module
The node:tls module provides OpenSSL-backed TLS and SSL networking for Node.js clients and servers. It includes encrypted sockets, TLS servers, secure contexts, certificate handling, and protocol inspection.
The node:tls module builds encrypted client and server communication on OpenSSL. It exposes secure sockets, servers, contexts, certificate and cipher inspection, and connection helpers; a client connection negotiates TLS before application data is exchanged.
import tls from 'node:tls';
const tls = require('node:tls');The module exports TLS client and server APIs, secure-context utilities, TLS socket and server classes, constants, and inspection functions.
node:tls in ESM or load it with require('node:tls') in CommonJS.node:crypto support cannot import or require node:tls.import tls from 'node:tls';
const socket = tls.connect({ host: 'example.com', port: 443, servername: 'example.com' }, () => {
console.log('TLS version:', socket.getProtocol());
socket.end();
});
socket.on('error', console.error);Opens a TLS connection to an HTTPS endpoint, reports the negotiated protocol after the handshake, and closes the socket.
A backend must call an HTTPS-compatible service directly over an encrypted transport.
An application accepts encrypted inbound TCP connections with server certificates.
Several connections share certificate, key, CA, or protocol configuration.
Operations or diagnostics need negotiated protocol, cipher, or peer-certificate information.
tls.connect() creates a client-side TLS connection and returns a tls.TLSSocket. It can receive host, port, certificate, authentication, and protocol options, and its callback runs when the connection is ready.
Follow-up: How can the peer certificate be inspected? Use the connected socket's certificate inspection APIs, such as getPeerCertificate(), after the connection is established.
tls.createSecureContext() builds reusable TLS credentials and configuration. A context can be supplied to server or client APIs so certificate, key, CA, and related settings are centralized.
Follow-up: What happens if a required certificate key is absent? Certificate-based cipher configurations require a key, supplied directly or through a PFX.
tls.TLSSocket extends net.Socket while adding TLS negotiation and transparent encryption. It therefore participates in stream-style I/O while exposing TLS-specific state and inspection methods.
Follow-up: How can negotiated protocol details be checked? Call connection inspection methods such as getProtocol() after the handshake has completed.
tls.connect()tls.createServer()tls.createSecureContext()tls.TLSSockettls.Servertls.getCiphers()tls.DEFAULT_MIN_VERSIONtls.DEFAULT_MAX_VERSION