SSL Pinning with Axios in Mobile and Hybrid Apps

Axios does not natively support SSL certificate pinning in mobile or hybrid application environments because it relies on high-level JavaScript networking APIs like XMLHttpRequest. In environments such as React Native, Ionic, Capacitor, or Apache Cordova, the underlying web runtime or JavaScript engine delegates TLS validation directly to the operating system or WebView. Consequently, achieving SSL certificate pinning with Axios requires intercepting requests using custom Axios adapters and routing them through native mobile networking layers that enforce certificate or public key validation.

Why Axios Cannot Pin Certificates Natively

Axios was originally designed for web browsers and Node.js environments:

How to Implement SSL Pinning with Axios

To enforce SSL pinning while maintaining the Axios API interface, developers use custom Axios adapters that redirect network calls to native HTTP clients.

1. Using Custom Axios Adapters

Axios allows overriding its default request transport mechanism through the adapter configuration option. A custom adapter intercepts the Axios request configuration, executes the request via a native plugin capable of SSL pinning, and returns a formatted Axios response object.

import axios from 'axios';
import NativePinningClient from 'some-native-pinning-library';

const pinnedAxios = axios.create({
  adapter: async (config) => {
    const response = await NativePinningClient.request({
      url: config.url,
      method: config.method,
      headers: config.headers,
      data: config.data,
      sslPinning: {
        certs: ['my_certificate']
      }
    });

    return {
      data: response.data,
      status: response.status,
      statusText: response.statusText,
      headers: response.headers,
      config: config,
      request: {}
    };
  }
});

2. SSL Pinning in React Native

In React Native, developers typically combine Axios with native networking modules:

3. SSL Pinning in Capacitor and Cordova

In WebView-based hybrid applications, standard Axios calls must completely bypass the WebView's network layer:

Types of Pinning Supported via Native Layers

When bridging Axios to native clients, two primary pinning strategies can be used:

Key Implementation Considerations