Skip to content

Environment Variables

Freestyle allows you to set environment variables independently for your cloudstate and web server environments. This is useful for isolating secrets and only exposing them to the necessary parts of your application.

Setting Environment Variables

To set environment variables for your freestyle project, you can use the env key in your freestyle.config.ts file. This key should be an object where the keys are the environment variable names and the values are the environment variable values.

freestyle.config.ts
import { defineConfig } from "freestyle-sh";
export default defineConfig({
deploy: {
web: {
envVars: {
...
}
},
cloudstate: {
envVars: {
...
}
}
},
});

Using .env Files

Freestyle does not automatically load .env files. If you want to use .env files, you can use a package like dotenv to load the .env file and set the environment variables in your freestyle.config.ts file.

  1. First, install the dotenv package:
    Terminal window
    npm install dotenv
  2. Add the following code to the top of your freestyle.config.ts file:
    import { config } from 'dotenv';
    config();
  3. Now you can use the environment variables in your freestyle.config.ts file.
    export default defineConfig({
    deploy: {
    web: {
    envVars: {
    PORT: process.env.PORT ?? "3000",
    OPENAI_API_KEY: process.env.OPENAI_API_KEY ?? "",
    }
    }
    },
    });