Axios Custom Instance Configuration Options
Creating a custom instance in Axios using axios.create()
allows developers to define reusable, centralized HTTP configurations
for specific APIs or microservices. This guide provides a detailed
overview of the core configuration options available when instantiating
a custom Axios client, covering base settings, request behavior,
security, performance limits, and data transformation properties.
Core Connection Settings
baseURL: A string representing the base URL prepended to any relative URL passed to request methods.baseURL: 'https://api.example.com/v1/'timeout: The number of milliseconds before the request times out and aborts. If set to0(the default), no timeout is applied.timeout: 5000headers: An object containing default custom headers sent with every request made by the instance.headers: { 'Authorization': 'Bearer token', 'Content-Type': 'application/json' }
Request and Query Parameters
params: Default URL query parameters to be appended to every request.params: { api_key: '12345' }paramsSerializer: A custom function or configuration object used to serializeparams(e.g., handling array formats).paramsSerializer: { indexes: false }
Authentication and Credentials
auth: Automatically sets an HTTP Basic Authorization header using provided credentials.auth: { username: 'admin', password: 'secretpassword' }withCredentials: A boolean indicating whether cross-siteAccess-Controlrequests should include credentials such as cookies, authorization headers, or TLS client certificates.withCredentials: true
Data Transformations and Encodings
transformRequest: An array of functions allowing modifications to the request data before it is sent to the server.transformResponse: An array of functions allowing modifications to the response data before it is passed tothenorcatch.responseType: Defines the format of the data returned by the server. Options include'json','text','blob','arraybuffer','document', and'stream'.responseEncoding: Sets the encoding used for decoding response data (e.g.,'utf8').
Request Control and Validation
validateStatus: A function determining whether to resolve or reject the returned promise based on the HTTP status code.validateStatus: (status) => status >= 200 && status < 300signal: AnAbortSignalinstance generated byAbortControllerto cancel active requests.proxy: Defines the host, port, and optional credentials for an HTTP proxy server.proxy: { host: '127.0.0.1', port: 9000 }maxContentLength: The maximum allowed size (in bytes) of the HTTP response content.maxBodyLength: The maximum allowed size (in bytes) of the HTTP request body.maxRedirects: Defines the maximum number of redirects to follow in Node.js (defaults to5).
Example Implementation
import axios from 'axios';
const apiClient = axios.create({
baseURL: 'https://api.example.com/v1',
timeout: 10000,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
withCredentials: true,
validateStatus: (status) => status >= 200 && status < 400
});