In Axios, axios.create() builds a separate instance with its own defaults, such as baseURL, headers and timeout, so you do not repeat them on every request. You export that instance from one file and use it like the global axios object: api.get('/users/1').
This guide shows how to create and configure an instance, make requests from a React component the right way, and add interceptors for tokens and errors.
Install the package in your project with npm:
Axios works in the browser and in Node.js, and it supports ES modules and TypeScript types out of the box. The browser built in fetch() is an alternative, but Axios adds automatic JSON handling, request and response interceptors, timeouts and consistent error objects.
Create a file such as src/api.js and export one configured instance.
Notes on the options:
baseURL is prefixed to relative URLs, so api.get('/users/1') calls https://www.example.com/api/users/1.timeout is in milliseconds, and the request is aborted if it takes longer. Axios has no timeout by default.Content-Type: application/json automatically when you send a plain object, so you do not need to set it. The original example used application/x-www-form-urlencoded, which is only right when you really send form encoded bodies.https in production. Plain http is acceptable only for local development.You can create several instances, for example one per backend service, each with its own settings.
An instance has the same shortcut methods as the global object.
The long form api({ method: 'get', url: '/users/1' }) also works, and is useful when the method is chosen at runtime. Each call returns a promise whose result has data, status, statusText and headers.
The original example called the request in the component body, which re-runs it on every render. Put it in useEffect, and cancel it with an AbortController when the component unmounts.
Add import axios from 'axios'; at the top if you use axios.isCancel. The signal option has been supported in Axios since version 0.22, and the old CancelToken API is deprecated. For larger apps, a data library such as TanStack Query handles caching, retries and loading state around the same Axios calls.
Interceptors run for every request or response of an instance, which is the main reason to create one.
If you can, prefer HttpOnly cookies over storing tokens in localStorage, since scripts on the page can read the latter.
Axios rejects the promise for any status outside 2xx. Check what kind of failure it was.
axios is the global default instance. axios.create() returns a new instance with its own defaults and interceptors, so settings do not leak between parts of your app.
Both work. Axios gives you interceptors, timeouts and automatic JSON parsing in one package, while fetch() needs no dependency but requires more manual handling.
Pass a headers object in the request config, for example api.get('/x', { headers: { 'X-Trace': '1' } }). It is merged over the instance defaults.
The server must send the correct Access-Control-Allow-Origin headers. Axios cannot bypass CORS from the browser.
Create one Axios instance with axios.create() and export it, setting baseURL, timeout and headers in a single place. Call it from useEffect with an abort signal in React, use interceptors for authentication and shared error handling, and inspect error.response to tell server errors from network failures.