How to Override Global Timeout in Axios
Axios allows you to define a global timeout that applies to all outgoing HTTP requests, preventing calls from hanging indefinitely. However, specific operations—such as file uploads, large data exports, or long-running database queries—often require more time than your default threshold allows. This guide explains how to bypass your default configuration and set a custom timeout value (or disable it entirely) for individual Axios requests.
Setting Up a Global Timeout
A global timeout is typically established using
axios.defaults.timeout or by creating a custom Axios
instance via axios.create().
import axios from 'axios';
// Global instance with a 5-second timeout
const apiClient = axios.create({
baseURL: 'https://api.example.com',
timeout: 5000, // 5000ms = 5 seconds
});Overriding the Timeout on a Single GET Request
To override the global setting for a specific GET
request, pass a configuration object containing the new
timeout property (in milliseconds) as the second
argument.
// Override to 15 seconds for this specific call
apiClient.get('/quick-report', {
timeout: 15000
})
.then(response => console.log(response.data))
.catch(error => {
if (error.code === 'ECONNABORTED') {
console.error('Request timed out');
}
});Overriding the Timeout on POST, PUT, and PATCH Requests
For methods that accept payload data (such as POST,
PUT, or PATCH), the configuration object is
passed as the third argument, following the URL and the request
body.
const uploadData = { file: 'base64_encoded_string' };
// Override to 30 seconds for a data upload
apiClient.post('/upload', uploadData, {
timeout: 30000
})
.then(response => console.log('Upload successful:', response.data))
.catch(error => console.error('Upload failed:', error));Using the Axios Request Object Directly
If you are calling axios() directly as a function,
include the timeout key directly within the options
object.
apiClient({
method: 'delete',
url: '/bulk-delete',
timeout: 20000 // 20 seconds
});Disabling the Timeout Completely
To allow a request to run indefinitely without being aborted by
Axios, set the timeout value to 0.
apiClient.get('/stream-events', {
timeout: 0 // No timeout
});