Appwrite Functions can read environment variables at build and runtime. Use them to pass constants and secrets such as API keys, connection strings, and feature flags without hardcoding them in your source.
A function reads from three sources, in this order of precedence:
- Project variables are shared across every function and site in your project. Set them once and every function inherits them automatically. See project variables for the platform-wide reference.
- Function variables are scoped to a single function. Override a project variable for one function by setting the same key on the function itself.
- Appwrite-injected variables are set by Appwrite at execution time (for example,
APPWRITE_FUNCTION_PROJECT_ID). These take final precedence and cannot be overridden.
Redeployment required
Variable changes only take effect on the next deployment. Redeploy your function after creating, updating, or deleting variables.
Manage in the Console
- Navigate to your function in the Appwrite Console.
- Open the Settings tab > Environment variables section.
- Click Create variable and enter a key and value.
- Optionally select the Secret checkbox to prevent any team member from reading the value after creation.
- Click Create, then redeploy the function for the change to take effect.
You can also configure global variables that apply to all your functions from your project's Settings page. See project variables for details.
Manage with a Server SDK
You can also manage function variables programmatically using a Server SDK. Each call requires an API key with the functions.write scope to create, update, or delete variables, or the functions.read scope to list and read them.
Create a variable
List variables
Get a variable
Update a variable
You can change a variable's key, value, or secret flag. Marking a variable as secret is one-way. Once set, the value is no longer readable from the Console or API.
Delete a variable
Read variables in your function
Once a variable is set, you can read it inside your function using your runtime language's standard environment lookup.
Appwrite-injected variables
Appwrite passes the following environment variables into every function deployment by default. They take precedence over your own variables, so do not set keys with the APPWRITE_ prefix.
| Variable | Description | Available at Build and/or Run Time |
APPWRITE_FUNCTION_API_ENDPOINT | The API endpoint of the running function | Both |
APPWRITE_VERSION | The Appwrite version used to run the function | Both |
APPWRITE_REGION | The region where the function will run from | Both |
APPWRITE_FUNCTION_API_KEY | The function API key used for server authentication | Build time |
APPWRITE_FUNCTION_ID | The ID of the running function | Both |
APPWRITE_FUNCTION_NAME | The name of the running function | Both |
APPWRITE_FUNCTION_DEPLOYMENT | The deployment ID of the running function | Both |
APPWRITE_FUNCTION_PROJECT_ID | The project ID of the running function | Both |
APPWRITE_FUNCTION_RUNTIME_NAME | The runtime of the running function | Both |
APPWRITE_FUNCTION_RUNTIME_VERSION | The runtime version of the running function | Both |
APPWRITE_FUNCTION_CPUS | The CPU (runtime) specification of the running function | Both |
APPWRITE_FUNCTION_MEMORY | The memory (runtime) specification of the running function | Both |
During the build process, dynamic API keys are automatically provided as the environment variable APPWRITE_FUNCTION_API_KEY. This environment variable does not need to be initialized.
Secret variables
Mark a variable as Secret to hide its value from the Console and API after creation. Only the function runtime can read the value at build and runtime. Team members and external integrations cannot retrieve it after creation.
You can mark a variable as secret either when you create it or by updating an existing variable. Marking a variable as secret cannot be reversed. To replace a secret value, delete the variable and create a new one with the same key.
Limits
| Field | Limit |
Variable ID | 36 characters, a-z A-Z 0-9 . - _ |
Key | 255 characters |
Value | 8192 characters |