Creating and Using Custom Instances

You can create custom Axios instances with different configurations for different parts of your application. This can help manage different base URLs, headers, and other settings.

// Create an instance with a custom configuration
const apiClient = axios.create({
  baseURL: 'https://jsonplaceholder.typicode.com',
  timeout: 1000,
  headers: {'Authorization': 'Bearer YOUR_TOKEN'}
});

// Use the custom instance to make requests
apiClient.get('/posts')
  .then(response => {
    console.log('Data:', response.data);
  })
  .catch(error => {
    console.error('Error:', error);
  });

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *