0

In the Appwrite console, I'm adding a test environment variable to pass into function...

enter image description here

In my function code (NodeJs) index.js, I'm logging out the value of above variable...

enter image description here

I save the code and use the Appwrite CLI (createTag) to push/publish the code.

Then in Appwrite console, I activate the new function then I execute it and I see this in log...

enter image description here

Clearly I'm missing something but I'm searching the Appwrite docs and I don't see it.

What am I doing incorrectly?

Thank you for helping :-)

Locohost
  • 1,682
  • 5
  • 25
  • 38

2 Answers2

0

Ok looks like as of this post, this is a bug in the Appwrite web UI code. You can do this 2 ways right now. You can set the environment vars in code or you can use the Appwrite CLI. I ended up putting the CLI commend in my NodeJs package.json scripts for quick easy access.

Here are both ways that worked for me...

appwrite functions create --functionId=regions_get_all --name=regions_get_all --execute=[] --runtime=node-16.0 --vars={ 'LT_API_ENDPOINT': 'https://appwrite.league-tracker.com/v1', 'LT_PROJECT_ID': '61eb...7e4ff', 'LT_FUNCTIONS_SECRET': '3b4b478e5a5576c1...ef84ba44e5fc2261cb8a8b3bfee' }

const sdk = require('node-appwrite');

const endpoint = 'https://appwrite.league-tracker.com/v1';
const projectId = '61eb3...7e4ff';
const funcionsSecret = '3b4b478e5a557ab8a...c121ff21977a';

const functionId = process.argv[2];
const name = process.argv[2];
const execute = [];
const runtime = 'node-16.0';
const env_vars = {
    "LT_API_ENDPOINT": endpoint,
    "LT_PROJECT_ID": projectId,
    "LT_FUNCTIONS_SECRET": funcionsSecret
};

// Init SDK
const client = new sdk.Client();
const functions = new sdk.Functions(client);
client
    .setEndpoint(endpoint) // Your API Endpoint
    .setProject(projectId) // Your project ID
    .setKey('33facd6c0d792e...359362efbc35d06bfaa'); // Your secret API key

functions.get(functionId)
    .then(
        func => {
            // Does this function already exist?
            if ((typeof (func) == 'object' && func['$id'] == functionId)) {
                throw `Function '${functionId}' already exists. Cannot 'create'.\n\n`;
            }
            // Create the function
            functions.create(functionId, name, execute, runtime, env_vars)
                .then(
                    response => console.log(response),
                    error => console.error(`>>> ERROR! ${error}`)
                );
        }).catch(
            error => console.error(`>>> ERROR! ${error}`)
        );
Locohost
  • 1,682
  • 5
  • 25
  • 38
  • Make sure you setup your API keys with proper functions.read and functions.write permissions or the code will error. – Locohost Jan 23 '22 at 13:47
  • Once you run one of above you will have access to the environment variables in your function code like... `process.env.LT_API_ENDPOINT` – Locohost Jan 23 '22 at 13:49
0

As of Appwrite 0.13.0, an Appwrite Function must expose a function that accepts a request and response. To return data, you would use the response object and either call response.json() or response.send(). The request object has an env object with all function variables. Here is an example NodeJS Function:

module.exports = async (req, res) => {
  const payload =
    req.payload ||
    'No payload provided. Add custom data when executing function.';

  const secretKey =
    req.env.SECRET_KEY ||
    'SECRET_KEY environment variable not found. You can set it in Function settings.';

  const randomNumber = Math.random();

  const trigger = req.env.APPWRITE_FUNCTION_TRIGGER;

  res.json({
    message: 'Hello from Appwrite!',
    payload,
    secretKey,
    randomNumber,
    trigger,
  });
};

In the example above, you can see req.env.SECRET_KEY being referenced. For more information, refer to the Appwrite Functions docs.

Steven Nguyen
  • 452
  • 4
  • 4