Configure Custom SSL Ciphers in Axios
Axios relies on Node.js's native https module to handle
secure network traffic on the server side. To specify custom SSL/TLS
ciphers for Axios requests, you must instantiate a custom
https.Agent with your desired cipher list and attach it to
your Axios configuration. This guide walks through the exact steps and
code required to enforce custom SSL ciphers on global instances and
individual requests.
Step 1: Create a Custom HTTPS Agent
Node.js allows you to configure TLS options—including
ciphers, minVersion, and
maxVersion—using the https.Agent class.
Define your custom cipher list as a colon-separated string (following OpenSSL cipher list format):
const https = require('https');
// Define your cipher suite string
const customCiphers = [
'ECDHE-RSA-AES128-GCM-SHA256',
'ECDHE-RSA-AES256-GCM-SHA384',
'ECDHE-ECDSA-AES128-GCM-SHA256'
].join(':');
// Create the custom agent
const httpsAgent = new https.Agent({
ciphers: customCiphers,
honorCipherOrder: true,
minVersion: 'TLSv1.2'
});Step 2: Apply the Agent to Axios
You can apply your custom agent either to a specific request or across an entire Axios instance.
Option A: Applying to an Axios Instance (Recommended)
Attaching the agent to an Axios instance ensures that all outgoing requests through that client use the configured ciphers:
const axios = require('axios');
const apiClient = axios.create({
baseURL: 'https://api.example.com',
httpsAgent: httpsAgent
});
// All requests through apiClient now use the custom SSL ciphers
apiClient.get('/data')
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error('Request failed:', error.message);
});Option B: Applying to a Single Request
If you only need custom ciphers for a single endpoint, pass the
httpsAgent in the request config:
const axios = require('axios');
axios.get('https://api.example.com/secure-data', { httpsAgent })
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error('Request failed:', error.message);
});Important Considerations
- Environment Compatibility: Custom HTTPS agents only
work in Node.js runtime environments. In browser environments, the
browser directly manages TLS negotiation and ignores the
httpsAgentconfiguration. - TLS 1.3 Ciphers: For TLS 1.3, Node.js manages
cipher suites differently than TLS 1.2. If you need specific control
over TLS 1.3 suites, ensure your Node.js version supports configuring
ciphersfor TLS 1.3 or explicitly setmaxVersion: 'TLSv1.2'if you depend on older suites. - Verification: If a target server does not support
any of your configured ciphers, the handshake will fail with an
ERR_SSL_NO_CIPHER_MATCHorECONNRESETerror.