posts/en/instance.md
You can create a new instance of axios with a custom config.
const instance = axios.create({
baseURL: 'https://some-domain.com/api/',
timeout: 1000,
headers: {'X-Custom-Header': 'foobar'}
});
The available instance methods are listed below. The specified config will be merged with the instance config.
In addition to using convenience methods like instance.get() or instance.post(), you can also call an Axios instance directly with a config object. This is functionally equivalent to axios(config), and is particularly useful when retrying a request using the original configuration.
const instance = axios.create({ baseURL: '/api' });
// Works just like axios(config)
instance({
url: '/users',
method: 'get'
});
This approach enables clean retry logic when handling authentication errors:
instance.interceptors.response.use(undefined, async (error) => {
if (error.response?.status === 401) {
await refreshToken();
return instance(error.config); // Retry original request
}
throw error;
});