Environment variables and secrets
When developing scripts or applications, handling secrets like API keys, passwords, and tokens securely is crucial. Environment variables and secrets offer a safe and flexible way to store and manage this sensitive data without hardcoding it into your codebase.
What are environment variables?
Environment variables are used to store information or configuration data that your script requires at runtime. They enable you to manage any values separate from the script code. This separation ensures that variable values are not exposed and stored in your scripts or version control systems but rather configured in the runtime.
Adding environment variables
To add environment variables to your script, follow these steps:
- Log in to the bunny.net dashboard.
- Select Edge Platform, click on Scripting, and select the script.
- Select Env Configuration and Environment Variables, and fill in the details of the variable you want to create.
- Click Save.
Using environment variables
You can access environment variable value in the script code using node process module or using Deno.env.
The following script example shows how to access an environment variable value using node process module:
import * as BunnySDK from "https://esm.sh/@bunny.net/[email protected]";
import process from "node:process";
/**
* Rewrites the url to fetch from origin.
* @param {Request} request - The Fetch API Request object.
* @return {Response} The HTTP response or string.
*/
BunnySDK.net.http.serve(async (request: Request): Response | Promise<Response> => {
// Load environment variable named OriginUrl
const originUrl = process.env.OriginUrl;
// Parse initial url
const url = new URL(request.url);
// Rewrite and fetch request from origin
return fetch(originUrl + url.pathname);
});
The following script example shows script code with same logic but using Deno.env to access variable value:
import * as BunnySDK from "https://esm.sh/@bunny.net/[email protected]";
/**
* Rewrites the url to fetch from origin.
* @param {Request} request - The Fetch API Request object.
* @return {Response} The HTTP response or string.
*/
BunnySDK.net.http.serve(async (request: Request): Response | Promise<Response> => {
// Load environment variable named OriginUrl
const originUrl = Deno.env.get("OriginUrl");
// Parse initial url
const url = new URL(request.url);
// Rewrite and fetch request from origin
return fetch(originUrl + url.pathname);
});
What are environment secrets?
Environment secrets are sensitive configuration settings, such as API keys, passwords, or tokens, that are securely stored and used by your script. They are kept hidden from source code to prevent unauthorized access and maintain the security of your system.
Adding environment secrets
To add environment secrets to your script, follow these steps:
- Log in to the bunny.net dashboard.
- Select Edge Platform, click on Scripting, and select the script.
- Select Env Configuration and Environment Secrets, and fill in the details of the secret you want to create.
- Click Save Secret.
Using environment secrets
Environment secrets can be accessed using the same approach as environment variables (using node process module or Deno.env). You can use examples above to also access secrets.
Note
Names of environment variables and secrets on the same scripts have to be unique (same name cannot be used for variable and secret on the same script).
Need help or encountered issues?
If you encounter any difficulties or have questions while following this guide, our support team is here to assist you. Please don't hesitate to contact us via the support request form for prompt assistance.
Our dedicated support team is ready to help you resolve any issues you might face during the deployment process, provide additional guidance, or answer your questions.
Updated about 2 months ago