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.

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