Demystifying Axios instance

Abhijeet kumar
3 min readJun 18, 2023

--

Axios is a popular JavaScript library used for making HTTP requests from a web browser or Node.js. It provides an easy-to-use API for sending and receiving data over the network. Axios allows you to create instances, which can be configured with default settings and reused across your application. This is particularly useful when you need to make multiple requests to the same API or when you want to apply consistent configuration options.

Here’s an explanation of how to create an Axios instance and some examples of how to use it:

Import Axios: Before using Axios, you need to import it into your project. In a browser environment, you can include the Axios library by adding the following script tag to your HTML file:

<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

In a Node.js environment, you can install Axios using npm or yarn:

npm install axios // cmd

Create an Axios instance: Once you have imported Axios, you can create an instance using the axios.create() method. This method returns a new Axios instance with its own configuration options:

const instance = axios.create({
baseURL: 'https://api.example.com', // Set a base URL for all requests
timeout: 5000, // Set a default timeout for all requests (in milliseconds)
headers: {
'Content-Type': 'application/json', // Set default headers for all requests
},
});

Making requests with the Axios instance: Now that you have an instance, you can use its methods to make HTTP requests. The instance inherits all the methods from the Axios library, such as get, post, put, delete, etc. Here's an example of making a GET request using the instance:

instance.get('/users')
.then(response => {
console.log(response.data); // Process the response data
})
.catch(error => {
console.error(error); // Handle any errors
});

Configuring specific requests: You can override the default configuration options for specific requests by passing an additional configuration object to the instance method. This allows you to set specific headers, timeouts, or other options for a particular request:

instance.get('/posts', {
params: {
userId: 1,
},
headers: {
'Authorization': 'Bearer <token>',
},
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});

Interceptors: Axios instances also support interceptors, which allow you to intercept and modify the request or response before they are handled. You can add interceptors to the instance using the interceptors property:

instance.interceptors.request.use(config => {
// Modify the request config before sending
config.headers['X-Request-Id'] = '123456';
return config;
});

instance.interceptors.response.use(response => {
// Modify the response data before resolving the promise
response.data = response.data.results;
return response;
});

Additional instance methods: Besides the HTTP methods (get, post, etc.), Axios instances have additional methods like all, spread, and cancel that can be useful in certain scenarios. Refer to the Axios documentation for more details.

Creating an Axios instance allows you to configure default settings, reuse the instance across your application, and simplify the process of making HTTP requests. It helps maintain consistency and improves code readability.

Conclusion : Axios instances provide a powerful and convenient way to manage and customize HTTP requests in JavaScript applications. By creating an Axios instance, you can set default configuration options such as base URLs, timeouts, and headers, making it easier to reuse and maintain consistent settings across multiple requests. The instance inherits all the methods from the Axios library, allowing you to make various types of HTTP requests. Additionally, Axios instances support interceptors, which enable you to intercept and modify requests or responses, further enhancing flexibility and control. Overall, by leveraging Axios instances, developers can streamline their network requests, improve code organization, and enhance the overall efficiency of their applications.

--

--

No responses yet