How to get Digitalocean environment variable?

1k views Asked by At

I set an environment variable in Digitalocean with token, but I don't understand how to use it in the code. I scrolled through the docs, but didn't get anything. Can you please tell which path I should write or anything else what I should do?

2

There are 2 answers

0
JJ Fantini On BEST ANSWER

I would suggest using an environment function in your language of choice that searches the .env file or current global env and pull the variable that is available.

Example: for R, I can use Sys.getenv("MY_SECRET") to pull the variable from DO.

0
Jeff Monteiro On

To use variables in Digital Ocean using Functions over nginx runtime:

  1. Require dotenv into main function
  2. Process env into a const
  3. Now you can use the environment variable

EXAMPLE

function main(event, context) {

  require('dotenv').config();
  const SECRET = process.env.VARIABLE_NAME;
 
} 

Remember, is necessary to register the variable before access and use it.