axios by itself comes with two useful "methods" the interceptors that are none but middlewares between the request and the response. so if on each request you want to send the token. Use the interceptor.request.

I made apackage that helps you out:

$ npm i axios-es6-class

Now you can use axios as class

export class UserApi extends Api {
    constructor (config) {

        // this middleware is been called right before the http request is made.
        this.interceptors.request.use(param => {
            return {
                defaults: {
                    headers: {
                        "Authorization": `Bearer ${this.getToken()}`

      this.login = this.login.bind(this);
      this.getSome = this.getSome.bind(this);

   login (credentials) {
      return"/end-point", {...credentials})
      .then(response => this.setToken(

   getSome () {
      return this.get("/end-point")

I mean the implementation of the middleware depends on you, or if you prefer to create your own axios-es6-class it is the medium post where it came from


there are a lot of good solution but I use this

let token=localStorage.getItem("token");

var myAxios=axios.create({
  baseURL: 'https://localhost:5001',
  timeout: 700,
  headers: {'Authorization': `bearer ${token}`}

export default myAxios;

then i import myaxios to my file and



// usetoken is hook i mad it

export const useToken = () => {
     return JSON.parse(localStorage.getItem('user')).token || ''
const token = useToken();

const axiosIntance = axios.create({
    baseURL: api,
    headers: {
        'Authorization':`Bearer ${token}`

axiosIntance.interceptors.request.use((req) => {
        req.headers.Authorization = `Bearer ${token}`;
    return req;


I use a separate file to init axios instance and at the same time, I add intercepters to it. Then in each call, the intercepter will add the token to the request header for me.

import axios from 'axios';
import { getToken } from '../hooks/useToken';

const axiosInstance = axios.create({
  baseURL: process.env.REACT_APP_BASE_URL,

  (config) => {
    const token = getToken();
    const auth = token ? `Bearer ${token}` : '';
    config.headers.common['Authorization'] = auth;
    return config;
  (error) => Promise.reject(error),

export default axiosInstance;

Here is how I use it in the service file.

import { CancelToken } from 'axios';
import { ToolResponse } from '../types/Tool';
import axiosInstance from './axios';

export const getTools = (cancelToken: CancelToken): Promise<ToolResponse> => {
  return axiosInstance.get('tool', { cancelToken });


Just in case someone faced the same issue.

The issue here is when passing the header without data, the header's configuration will be in the payload data, So I needed to pass null instead of data then set the header's configuration.

const config = {
         headers: {
             "Content-type": "application/json",
              "Authorization": `Bearer ${Cookies.get("jwt")}`,
axios.get(`${BASE_URL}`, null, config)


This works and I need to set the token only once in my app.js:

axios.defaults.headers.common = {
    'Authorization': 'Bearer ' + token

Then I can make requests in my components without setting the header again.

"axios": "^0.19.0",


If you want to some data after passing token in header so that try this code

const api = 'your api'; 
const token = JSON.parse(sessionStorage.getItem('data'));
const token =; /*take only token and save in token variable*/
axios.get(api , { headers: {"Authorization" : `Bearer ${token}`} })
.then(res => {
.catch((error) => {


By using Axios interceptor:

const service = axios.create({
  timeout: 20000 // request timeout

// request interceptor

  config => {
    // Do something before request is sent

    config.headers["Authorization"] = "bearer " + getToken();
    return config;
  error => {


The second parameter of is data (not config). config is the third parameter. Please see this for details:


You can create config once and use it everywhere.

const instance = axios.create({
  baseURL: '',
  timeout: 1000,
  headers: {'Authorization': 'Bearer '+token}

.then(response => {


Here is a unique way of setting Authorization token in axios. Setting configuration to every axios call is not a good idea and you can change the default Authorization token by:

import axios from 'axios';
axios.defaults.baseURL = 'http://localhost:1010/'
axios.defaults.headers.common = {'Authorization': `bearer ${token}`}
export default axios;

Some API require bearer to be written as Bearer, so you can do:

axios.defaults.headers.common = {'Authorization': `Bearer ${token}`}

Now you don't need to set configuration to every API call. Now Authorization token is set to every axios call.


const config = {
    headers: { Authorization: `Bearer ${token}` }

const bodyParameters = {
   key: "value"

The first parameter is the URL.
The second is the JSON body that will be sent along your request.
The third parameter are the headers (among other things). Which is JSON as well.

Related Query

More Query from same tag