How do I read environment variables from a SvelteKit website?
Hiya,
I can't find any resources on how to set up SvelteKit to read environment variables from cloudflare pages environment variables set in the cloudflare dashboard. If anyone knows how to do this, it would be greatly appreciated :-)
4 Replies
Sounds like you've already done half of the work. Once it's defined in the dashboard then use dynamic private for runtime. That's all there is to it.
https://svelte.dev/docs/kit/$env-dynamic-private
$env/dynamic/private • Docs • Svelte
$env/dynamic/private • Svelte documentation
heya, thanks for the help! it now builds successfully, but I don't believe my app can access my api key as the page relying on it won't work. everything works fine in a local server. is there something specific I need to do with cloudflare secrets?
reading the value of WorkersAPIKey at runtime with a console.log spits out null
nvm, tried switching the secret to a plaintext env variable just to test, and still nothing :(
oh
my god
environment variables only apply to prodðŸ˜
I was testing on a preview branch
Lol. Sounds about right. Just gotta add the variables to the preview env. So any updates to variables should be done both on preview and prod.
Hello, @Soly
I think you are developing sveltekit project
I am a full stack sveltekit developer.
May I DM to you?