[Solved] Change the default base url for axios

I have configured my axios like this

const axiosConfig = {
  baseURL: '',
  timeout: 30000,

Vue.prototype.$axios = axios.create(axiosConfig)

Inside my component i make a call as


Now the above works but i would like to change the baseURL without affecting the global base URL so that in my component i can simply use without API endpoint so

I’ve tried

this.$axios.baseURL = "";
this.$axios.get().. //this is still in api endpoint

How do i go about this?

Enquirer: Geoff


Solution #1:

Instead of



this.$axios({ url: 'items', baseURL: 'http://new-url.com' })

If you don’t pass method: 'XXX' then by default, it will send via get method.

Request Config: https://github.com/axios/axios#request-config

Respondent: Vipin Kumar

Solution #2:

Putting my two cents here. I wanted to do the same without hardcoding the URL for my specific request. So i came up with this solution.

To append 'api' to my baseURL, I have my default baseURL set as,

axios.defaults.baseURL = '/api/';

Then in my specific request, after explicitly setting the method and url, i set the baseURL to '/'

    baseURL: '/',
   .then(response => {
   .catch(error => {
Respondent: shazyriver

Solution #3:

  1. Create .env.development, .env.production files if not exists and add there your API endpoint, for example: VUE_APP_API_ENDPOINT ='http://localtest.me:8000'
  2. In main.js file, add this line after imports: axios.defaults.baseURL = process.env.VUE_APP_API_ENDPOINT

And that’s it. Axios default base Url is replaced with build mode specific API endpoint.
If you need specific baseURL for specific request, do it like this:

this.$axios({ url: 'items', baseURL: 'http://new-url.com' })
Respondent: Andris

Solution #4:

From axios docs you have baseURL and url

baseURL will be prepended to url when making requests. So you can define baseURL as and make your requests to /url

 // `url` is the server URL that will be used for the request
 url: '/user',

 // `baseURL` will be prepended to `url` unless `url` is absolute.
 // It can be convenient to set `baseURL` for an instance of axios to pass relative URLs
 // to methods of that instance.
 baseURL: 'https://some-domain.com/api/',

Respondent: Jonathan Batista

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.