Skip to content

Environment variables

You can store secrets, keys, and API tokens as Environment Variables via the val’s left side bar.

Environment variables can be accessed via Deno.env or process.env within any file in your val.

  • The “key” and “value” of each environment variable can be any string
  • Vals can’t set environment variables programmatically from inside val code. Environment variables are set via the settings page, API, or SDK. Trying to update an environment variable, for example by using Deno.env.set, is a no-op.

Environment variables set via the API or SDK (for example, client.vals.environmentVariables.update or the add_env_var tool) are picked up by a val on the next HTTP request against the same warm isolate — no redeploy needed. Environment variables set through the Val Town UI or settings page may behave differently and can require a val edit or redeploy before a warm isolate sees the new value.

This uses the Deno-default Deno.env variable, which is available globally.

const secret = Deno.env.get("someSdkSecret");
export let sdk = new SomeSDK(secret);

This is the conventional way to access environment variables when you’re in a Node.js environment.

const secret = process.env.someSdkSecret;
export let sdk = new SomeSDK(secret);

It is safe to reference environment variables in public vals. Others can see that they’re being used, but not their values.

For example, in this public val, you can see that I’m using a Discord bot’s environment variable, but you cannot run this code or get the value of the environment variable.

View and run this example on Val Town

import { discordSendDM } from "https://esm.town/v/vtdocs/discordSendDM";
const userd = "420257368417239041";
const result = await discordSendDM(Deno.env.get("discordBot"), userd, "Hello from valtown!");
console.log(result);

If you expose an HTTP or Email trigger, the underlying code can be triggered by anyone who knows the URL or email address, but they still can’t access your environment variables directly. This is how server-side code normally works. If someone wants to run your code with their environment variables, they need to remix your val and run their copy.

Within organization accounts, you can create environment groups to share environment variables between multiple vals.