Back

[SOLVED] How to access global environment variable from function?

  • 0
  • Functions
  • Cloud
NickTheMusketeer
29 Nov, 2024, 12:39

I use Bun 1.1 runtime for Functions and I'd like to use the value of a global environment variable that I have set.

In the Starter example, I can see: Bun.env['APPWRITE_FUNCTION_PROJECT_ID']

When I replace the attribute with the global environment variable key I've set on the project, it returns undefined


SOLUTION

  1. Copy the Global variables key-value pairs from the Project Settings in Appwrite console by clicking Editor
  2. Paste the key-value pairs into an .env created at the root directory of your Appwrite function in your codebase. This .env file is not tracked by default - don't update this behaviour!.
  3. Type appwrite run function , choose your function and you will now observe that your variables are part of your local Dockerised runtime.
TL;DR
Developers were having trouble accessing global environment variables in a local function. The solution is to copy global variables from Appwrite console, paste them into an `.env` file in the project's root directory, and run `appwrite run function`. This allows the variables to be part of the local Dockerised runtime.
NickTheMusketeer
29 Nov, 2024, 12:42

Clarification: I am attempting to run this function locally. The global env var is hosted in Appwrite console.

NickTheMusketeer
29 Nov, 2024, 12:44

Alright, I figured this out. Solution to follow in the next reply.

NickTheMusketeer
29 Nov, 2024, 12:47

If you are trying to run a function locally and want to access your global environment variables, the way to do this is to perform the following steps:

  1. Copy the Global variables key-value pairs from the Project Settings in Appwrite console by clicking Editor
  2. Paste the key-value pairs into an .env created at the root directory of your Appwrite function in your codebase. This .env file is not tracked by default - don't update this behaviour!.
  3. Type appwrite run function , choose your function and you will now observe that your variables are part of your local Dockerised runtime.
NickTheMusketeer
29 Nov, 2024, 12:48

[SOLVED] How to access global environment variable from function?

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more