For the complete documentation index, see llms.txt. This page is also available as Markdown.

πŸ“‘Axios API Calls

Mock API calls

Set the default axios baseURL for calling the API

Open .env file and edit REACT_APP_API_URL.

.env

## Backend API URL
VITE_APP_API_URL=

You can configure the same for Next.js as well.

Axios has been configured in the folder src/utils/axios.ts

Example 1: With baseUrl

src/utils/axios.ts
import axios, { AxiosRequestConfig } from 'axios';

const axiosServices = axios.create({ baseURL: import.meta.env.VITE_APP_API_URL || 'http://localhost:3010/' });

// ==============================|| AXIOS - FOR MOCK SERVICES ||============================== //

axiosServices.interceptors.request.use(
  async (config) => {
    const accessToken = localStorage.getItem('serviceToken');
    if (accessToken) {
      config.headers['Authorization'] = `Bearer ${accessToken}`;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);
src/utils/axios.js
import axios, { AxiosRequestConfig } from 'axios';

const axiosServices = axios.create({ baseURL: import.meta.env.VITE_APP_API_URL || 'http://localhost:3010/' });

// ==============================|| AXIOS - FOR MOCK SERVICES ||============================== //

axiosServices.interceptors.request.use(
  async (config) => {
    const accessToken = localStorage.getItem('serviceToken');
    if (accessToken) {
      config.headers['Authorization'] = `Bearer ${accessToken}`;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

Example 2: Without baseUrl

You can set the entire URL in the Axios request. Do not use common Axios instances src\utils\axios.ts instead, use the Axios library.

Last updated