You can use Environment Variables to store configuration options, or sensitive information such as API keys, passwords, and tokens.
Environment variables are injected to process.env
when your Function is executed. You can access them the same way you would access them in Node.js:
export function handler(request: Request): Response {
if (request.headers.get('Authorization') !== process.env.AUTH_TOKEN) {
return new Response('Unauthorized', { status: 401 });
}
return new Response('Welcome!');
}
Development
During development, you can use the --env
flag of the dev
command to specify a .env
file to load.
Adding environment variables
Head over to the settings tab of your Function, and scroll to the "Environment variables" section. Here, you can see all your Environment Variables, which are empty by default.
Enter a key and value, then click on "Add". You can keep adding Environment Variables until you're satisfied, and then click on "Submit" to save your changes.
When submitting, your Function's current production Deployment will automatically be re-deployed!
Removing environment variables
To remove an Environment Variable, click on the "Remove" button next to it. Click on "Submit" to save your changes.