Using Environment Variables
This how-to explains using environment variables in CRANQ through a simple example.
Environment variables provide a safe way to store secrets that we don't want to bundle into our CRANQ project.
It's also a great way to share variables between projects.
You find a couple of nodes in the CRANQ repo that deal with environment variables based on whether you're looking to read one variable at a time or more, or whether you want to provide default values, or just want to get the ones that have assigned values in the environment.
List of environment-related nodes
The Environment variables getter with fallback node is a good choice in most situations.
In the following example, we'll focus on two environment variables: "API_KEY" and "PRIVATE_KEY".
system/Environment variables getter with fallbackin the repo, drag it onto the canvas, then click on the
default valuesport and set an empty dictionary.
data/Storein the repo and drag it onto the canvas. This will hold the list of variable names. Then, click on the
datainput port and set the value to the list of environment variable names:
First connect the
store. This will cause the
storenode to send out the list of variable names we stored in it via its
Then, connect the
variable namesinput of
environment variable getter...node.
Finally, connect the
resolved variablesoutput of the
environment variable getter...node to the
datainput of the
loggernode. This will allow us to see the environment variables' values in the output window.
Upon first run, you'll likely see something like this on the output:
The values are empty, because there are no such environment variables set up yet. And even if there were, the CRANQ app doesn't actually read from the real environment. It reads environment variables from a .env file stored in the CRANQ folder. This way you have more control over these variables during the development process. (Once the program is compiled to NodeJS it will read the actual environment, but more on that later.)
To create a .env file, first open the CRANQ folder via the Help menu.
Make note of the absolute path, and using a text or code editor, save the .env file there with the following content:
API_KEY=My API key
PRIVATE_KEY=My private key
Once saved, head back to CRANQ, and re-run the program. This time we'll see the values we just added to the .env file.
CRANQ's .env file is a regular dotenv (https://www.npmjs.com/package/dotenv) file, so other projects that use the dotenv library can read from it.
CRANQ uses the same .env file across projects, so you don't have to do anything extra to share environment variables between CRANQ projects. Just switch to another project and the variables will be accessible form there too.
After compiling this program, unzipping, installing dependencies, and running it through node, you'll likely see the same output as when we ran it from CRANQ for the first time.
Running compiled program
It's because, again, these variables are not set in the environment. Once we set them, they'll be read out correctly. (Screenshot below shows setting environment variables in macOS. Other OSes have similar commands to set environment variables.)
Setting environment variables and re-running program