Environment Variables
Use environment variables in your workers with .env file support and dashboard secrets.
Environment Variables
Environment variables are not declared anywhere. Every variable you set in the Ploy dashboard is available to your build and to your worker at runtime under env.vars, and during local development the same applies to every entry of your project's .env file.
ploy.yaml used to have an env section. It has been removed — frameworks
like Next.js inject variables through their own mechanisms, and libraries you
depend on can read variables you never wrote down. Declaring every variable up
front only got in the way.
Accessing Variables
All variables are available under env.vars in your worker:
export default {
async fetch(request, env) {
return Response.json({
appName: env.vars.APP_NAME,
apiUrl: env.vars.API_URL,
});
},
} satisfies Ploy;In a Next.js app you keep using process.env as usual — Ploy's Next.js adapter injects the variables for you:
export async function GET() {
return Response.json({ apiUrl: process.env.API_URL });
}ploy types generates a vars property typed as Record<string, string>. Because variables are never declared, the CLI cannot know their names:
declare global {
interface PloyEnv {
vars: Record<string, string>;
}
}Local Development with .env
Create a .env file in your project directory:
APP_NAME=my-app
SECRET_KEY=my-local-secret
API_URL=http://localhost:3000/apiWhen you run ploy dev, every entry of that file is loaded and exposed under env.vars.
Add .env to your .gitignore file. Never commit secrets to version control.
Production: Dashboard Variables
In production, env.vars is populated from the environment variables set on your project. The same values are also exported into the build environment, so they are available to next build and any other build step.
Setting Variables in the Dashboard
- Go to your project settings in the Ploy dashboard
- Navigate to the Environment Variables section
- Add the variables your project needs
Variables can be added one at a time, or pasted in bulk in KEY=value form — for example straight from your local .env file.
Secret vs Plaintext
When adding variables in the dashboard, you can choose between:
- Secret: The value is encrypted and never displayed after creation. Use for API keys, tokens, and passwords.
- Plaintext: The value is stored and displayed in plain text. Use for non-sensitive configuration.
Variables Ploy Sets For You
Ploy injects a few variables of its own into every deployment:
| Variable | Description |
|---|---|
PLOY_URL | The project's primary URL |
PLOY_SHA_URL | The immutable URL for this commit |
PLOY_BRANCH_URL | The URL for this branch |
PLOY_LIVE_URL | The URL of the current production deployment |
PLOY_BRANCH | The branch this deployment was built from |
PLOY_COMMIT | The commit this deployment was built from |
PLOY_DEPLOYMENT_ID | This deployment's id (see skew protection) |
Full Example
Project Structure
my-worker/
├── .env # Local values (gitignored)
├── ploy.yaml # Project configuration
├── env.d.ts # Generated types
├── package.json
├── tsconfig.json
└── src/
└── index.tsploy.yaml
kind: dynamic
build: pnpm build
out: dist.env
APP_NAME=my-app
SECRET_KEY=dev-secret-key-123
API_URL=https://api.dev.example.comsrc/index.ts
export default {
async fetch(request, env) {
const url = new URL(request.url);
if (url.pathname === "/config") {
return Response.json({
appName: env.vars.APP_NAME,
apiUrl: env.vars.API_URL,
// Never expose secrets in responses!
});
}
// Use SECRET_KEY for authentication
const authHeader = request.headers.get("Authorization");
if (authHeader !== `Bearer ${env.vars.SECRET_KEY}`) {
return new Response("Unauthorized", { status: 401 });
}
return Response.json({ message: "Authenticated" });
},
} satisfies Ploy;Next Steps
- Workers - Learn about Ploy workers
- Configuration - Full
ploy.yamlreference - Databases - Add persistent SQLite databases
How is this guide?
Last updated on