I've read through all the docs for Nuxt.js environment variables and the Axios module but I'm still quite confused on how to properly set them up for my use case.
I want to query 2 separate APIs:
My own backend serves as an "extension" of the public API so that I can store additional data for my users.
Now my question is how do I set up my environment variables so that I can safely send dynamic requests to the public API without exposing its private API key? Do I need to use my own backend as a "proxy" and forward client side requests to the public API from there? Or can I directly send requests inside asyncData and fetch when running in SSR mode?
I think I need a general explanation on how Nuxt publicRuntimeConfig
and privateRuntimeConfig
, and Axios baseURL
and browserBaseURL
all work together. The docs didn't explain them clearly enough for me.
This question is mixing a lot of stuff at the same time but in no specific order:
privateRuntimeConfig
which is available only on the serverfetch()
and asyncData()
will run both on server and client side (can be forced to be run only on client side with fetchOnServer: false
), and it's not a good idea to have those on client since everything there can be publicly seenaxios
, a quick search can be helpful to setup thisaxios
in serverMiddleware
you'll need to install and import a regular axios
since it will be out of the scope of NuxtpublicRuntimeConfig
), if it should remain secret, you'll need a backend to hide it in-betweenbaseURL
is pretty much the default value, browserBaseURL
as explained in the docs is mainly an override specific to client-side requests, use it if you need to have something different and that overrides the baseURL
oneedge-side rendering
of Nuxt3 may maybe help on this one