Back

Access Appwrite_Function_APIKeys from process.env into req.variables of JSON file using typescript

  • 0
  • Web
alpha#trinity1
18 Jul, 2023, 14:39

web one was closed. This is building from web one. In my case, appwrite api keys were loaded into nodejs module which can only be accessed by process.env['APPWRITE_FUNCTION_PROJECT_ID']. So i need to load the result of process.env['APPWRITE_FUNCTION_PROJECT_ID'] into req.variables which is used to reques data from JSON file.

TL;DR
The user is trying to access the Appwrite function API keys stored in `process.env` and pass them into `req.variables` of a JSON file using TypeScript. They are having trouble getting the expected output when running their code. There is a suggestion to check the Appwrite documentation and a link to a GitHub repository for a local server script. The user also mentions changing the settings of the Appwrite keys and asks why `console.log()` is not outputting the expected value. Another user suggests correcting the syntax inside an object and provides an example. The original user mentions that the suggested solution did not work, and there is a discussion about
alpha#trinity1
18 Jul, 2023, 14:42

and remember im using typewrite

alpha#trinity1
18 Jul, 2023, 14:42

sorry, typescript

Binyamin
18 Jul, 2023, 14:42

When using Appwrite function you don't use process.env What you mean by that πŸ‘‡

appwrite api keys were loaded into nodejs module which can only be accessed by process.env

alpha#trinity1
18 Jul, 2023, 14:48

Sir, iam connecting to appwrite using nodejs and nodejs is running on replit online platform. On that platform, there is secret key menu where I can configure the keys I need to use (were loaded means were configured).. and in this case i use node-appwrite in place of appwrite...

Binyamin
18 Jul, 2023, 14:49

Okay So you have a node backend that has nothing to do with Appwrite functions. Yes?

Binyamin
18 Jul, 2023, 14:50

Then why you need req.variables for

alpha#trinity1
18 Jul, 2023, 14:57

No, first appwrite keys were configured in appwrite server using the appwrite console. Then from nodejs platform running on replit, appwrite keys were configured in the secrete menu to be used for communicating to appwrite. Once these keys are confugured in secrete menu, they go the module of nodejs which can only be accessed by process.env. remember in this case i am interacting with the server. so i need to retrieve saver data to the JSON file for browser interaction. thats why i need to parse them to req.variables....

Binyamin
18 Jul, 2023, 14:58

I understand all except the req.variables what you need those for?

alpha#trinity1
18 Jul, 2023, 15:03

JSON file stores data in value pairs, one of the parameters it stores are variables. so i need to parse the variable retrieved from process.env into req.variables of the JSON so that i can now start interacting with JSON file in order to display the data on the browser..

Binyamin
18 Jul, 2023, 15:09

Gotcha

Binyamin
18 Jul, 2023, 15:09

What is the error you're getting?

alpha#trinity1
18 Jul, 2023, 15:26

thats what i was trying to use but dont output anything

Binyamin
18 Jul, 2023, 15:28

Inside an object you can't use ; or the const keyword It should be something like this

TypeScript
req.variables = {
  apiKey: process.env['SOME_VAR'],
  eventData: process.env['SOME_VAR']
}
...
console.log("apiKEY_case2:" + apiKey);
...
alpha#trinity1
18 Jul, 2023, 15:30

okay sir, let me look at that, will get back to you....

alpha#trinity1
25 Jul, 2023, 11:02

Hey sir, hope you're okay. Decided to change the setting of the APPWRITE KEYS by first testing when I have declared them within the code. Now imagining why console.log() cant output the respective APPWRITE_FUNCTION_ENDPOINT. As per attached file.

alpha#trinity1
25 Jul, 2023, 11:03

Hey sir, hope you're okay. Decided to change the setting of the APPWRITE KEYS by first testing when I have declared them within the code. Now imagining why console.log() cant output the respective APPWRITE_FUNCTION_ENDPOINT. As per attached file.

Binyamin
25 Jul, 2023, 12:51

What do you get in the log?

alpha#trinity1
25 Jul, 2023, 15:12

Sir, there nothing i see even in the log..

Binyamin
25 Jul, 2023, 15:12

Then the problem is before How did you ran this function?

alpha#trinity1
25 Jul, 2023, 15:15

The code is a typescript file, so i do tsc test4.ts. This generates a .js file. Then after I do node test4.js

Binyamin
25 Jul, 2023, 15:17

Mmm. This won't output anything.

To execute this file you either need to deploy it your Appwrite instance. Or You can try this local-server.js that will let you run your test4.js locally https://github.com/appwrite/appwrite/issues/5425 Example https://github.com/byawitz/roadmapper/blob/main/functions/getGitFeatures/local-server.js

alpha#trinity1
25 Jul, 2023, 15:20

Thank you sir, some idea on how to deploy it as appwrite instance in nodejs

Binyamin
25 Jul, 2023, 15:21
alpha#trinity1
25 Jul, 2023, 15:22

thank you

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