How to Use Axios to Handle SSL/TLS Certificate Verification Errors in Node.js

Published on
  • avatar
    Binh Bui
How to Use Axios to Handle SSL/TLS Certificate Verification Errors in Node.js
Photo by Boxed Water Is Better

When making HTTPS requests in Node.js, you may encounter the error message "unable to verify the first certificate." This error usually occurs when your application is unable to verify the SSL/TLS certificate presented by the server, which can happen for a variety of reasons.

In this article, we'll look at how to use Axios to handle SSL/TLS certificate verification errors in Node.js.

One way to handle SSL/TLS certificate verification errors in Node.js is to use Axios, a popular HTTP client library. Axios provides an option to disable SSL/TLS certificate verification, which can be useful when working with self-signed or invalid certificates.

To disable SSL/TLS certificate verification with Axios, you can pass a custom httpsAgent option that includes a rejectUnauthorized property set to false. Here's an example code snippet that shows how to use Axios to make an HTTPS request with SSL/TLS certificate verification disabled:

const axios = require('axios')
const https = require('https')

const agent = new https.Agent({
  rejectUnauthorized: false,

  .get('', {
    httpsAgent: agent,
  .then((response) => {
  .catch((error) => {

In this example, we create a new https.Agent object with rejectUnauthorized set to false, and then pass it as the httpsAgent option when making an HTTPS request with Axios. This tells Axios to ignore SSL/TLS certificate verification errors and accept self-signed or invalid certificates.

It's important to note that disabling SSL/TLS certificate verification can make your application vulnerable to man-in-the-middle attacks, where an attacker could intercept and modify the data being transmitted. So it's generally recommended to only disable certificate verification for testing or development purposes, and to enable it in production environments.

In summary, when you encounter SSL/TLS certificate verification errors in Node.js, you can use Axios to handle them by disabling certificate verification using the httpsAgent option with a custom https.Agent object that has rejectUnauthorized set to false. However, it's important to use this option with caution and to enable certificate verification in production environments to ensure the security of your application's data.

That's all for now. Thanks for reading! If you enjoyed this article, please consider sharing it on Twitter or Reddit. You can also follow me on Twitter for more content like this. If you have any questions or feedback, feel free to leave a comment below or send me an email.