Home  >  Q&A  >  body text

Accessing environment variables in composables in Nuxt3: a guide

In my nuxt 3 application, in order to get data, I want to set a base URL for all API calls. When I get this baseURL from environment variable. How to set baseURL?

I twist with composable useFetch but then I can't get the baseURL because useRuntimeConfig() is not accessible there.

// My composables function
const baseURL = "how to get baseURL from process.env";

export const myFetch = async (url: string) => {
  const options = {
    baseURL: baseURL,
  };

  return await useFetch(url, options);
}


P粉739942405P粉739942405289 days ago823

reply all(1)I'll reply

  • P粉763748806

    P粉7637488062023-11-24 10:19:06

    You can access the runtime configuration by doing this

    export default () => {
      const config = useRuntimeConfig()
    
      console.log(config)
    }
    

    As shown below: https://github.com/nuxt/framework /discussions/3215#discussioncomment-3088206

    reply
    0
  • Cancelreply