How to access Environment Variables? #1737
-
Simple question with (hopefully) a simple answer - how can I access environment variables? Alternatively, is there another way to suss out my environment locally and do a ternary based on that value? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
for now, I can use |
Beta Was this translation helpful? Give feedback.
-
Environment variables are available in data loaders and page loaders using the standard language features. For example, in a JavaScript data loader you can access environment variables with If you want to have the kind of configuration you are looking for, one option would be to generate the page with a page loader. That could read the environment variables and embed them in the generated page. Another option would be to have a data loader that reads all the environment variables you want to be publicly available in the data app (like your redirect URL), and saves them to its output. For example: const configuration = {
oauthRedirectUri = process.env["OAUTH_REDIRECT_URI"];
}
process.stdout.write(JSON.stringify(configuration)); |
Beta Was this translation helpful? Give feedback.
-
It doesn’t sound like you’re asking about environment variables in the Unix sense but whether your site is being served on If you want to generate different pages/content locally during preview than what you deploy to production, you could also consider using page loaders or data loaders and setting an environment variable differently during build (e.g., |
Beta Was this translation helpful? Give feedback.
Environment variables are available in data loaders and page loaders using the standard language features. For example, in a JavaScript data loader you can access environment variables with
process.env
, and in Python you could useos.environ
. Environment variables are not directly accessible on Markdown files, or the JS files they import the run in the browser, since browsers don't have environment variables.If you want to have the kind of configuration you are looking for, one option would be to generate the page with a page loader. That could read the environment variables and embed them in the generated page.
Another option would be to have a data loader that reads all the environment…