Level 1 · Lesson 3
Lock the key in the safe
Level 1 · Lesson 3Goal of this lesson
Store the API Ninjas key as an encrypted variable before any code uses it, so it never sits in a file, a commit or the browser.
Behind, or starting here? Download the code as it should be before this lesson, or in the course workspace run course-catch-up level-1/after-02-work-from-your-laptop in the folder above your app. It offers to bring your own app folder to this point and keeps its app id (the id: line in manifest.yml, which links the code to your app on the site), so it stays the same app. Your old files are kept in a copy next to it.
Lesson 4 calls API Ninjas, and every call needs your key. A workshop keeps keys in a key safe, not on the counter; your app keeps its key in an encrypted variable. It goes in first, before there is any code that could tempt you to paste it in. This lesson changes no files.
Step 1: Put the key in the key safe
Locked until the step before it is done.
Runs in the Atlassian cloud
Environment variablein the workshop: the key safe
An environment variable is a named value stored with the app, per environment. Functions read it as process.env.NAME. With --encrypt, the value is stored encrypted and forge variables list never shows it again. UI code in the browser cannot read variables at all, which is one more reason the call to API Ninjas happens in a function.
In the workshop Account keys and booking codes go in the key safe, never in the code.
Further reading, optional: Atlassian docs on forge variables (opens in a new tab)
See this on the Level 1 chart, in the step that reads the key from the encrypted variable.
In the app folder, run:
forge variables set --encrypt API_NINJAS_KEYThe CLI asks for the value and hides what you type: when you paste the key, nothing appears, not even dots. That is normal. Paste it once, press Enter, and don’t paste it anywhere else, such as an AI chat panel next to the terminal. Because the value is never part of the command, it stays out of your shell history too: the list of past commands your terminal saves to a file, which anyone using your computer could read.
…/Workshop-helper ❯ forge variables set --encrypt API_NINJAS_KEY
Setting the value of a variable in the development environment.
Press Ctrl+C to cancel.
? Enter the variable value: [hidden]
✔ Environment variable set.
Tip: Changes to environment variables will not apply to existing deployments. Please run forge deploy for your changes to take effect.
…/Workshop-helper ❯ forge variables list
Showing variables from the development environment:
Note, the values of encrypted variables are shown as ****.
Stuck?One common problem, and its fix
I set or changed a variable
Open in the FAQVariables reach your functions with the next deploy:
RunTerminal forge variables list forge deployVariables belong to one environment. Unless you add
-efollowed by an environment name (such as-e production), both commands use development.
Step 2: Check it is there, and hidden
Locked until the step before it is done.
forge variables listYou should see API_NINJAS_KEY with a tick under Encrypted? and its value shown as ****. Nobody, including you, can read it back from here. If you ever need to change it, set it again.
Step 3: Deploy so functions can read it
Locked until the step before it is done.
The CLI reminds you of this itself: a new or changed variable reaches functions only with the next deploy.
forge deployStuck?One common problem, and its fix
I set or changed a variable
Open in the FAQVariables reach your functions with the next deploy:
RunTerminal forge variables list forge deployVariables belong to one environment. Unless you add
-efollowed by an environment name (such as-e production), both commands use development.
Step 4: Optional: give the tunnel the key too
Locked until the step before it is done.
While forge tunnel runs, your laptop runs the code, and it reads variables from your terminal (its shell, the program that reads what you type), not from the key safe. Forge looks for the name with FORGE_USER_VAR_ in front.
In bash or zsh (the usual shells on macOS, Linux and in the course workspace), this asks for the key without showing it and keeps it for this terminal only. On Windows, use the tunnel from the course workspace in Docker, whose terminal runs bash:
read -s FORGE_USER_VAR_API_NINJAS_KEY && export FORGE_USER_VAR_API_NINJAS_KEYThe line is two commands joined by &&, which means “do the second only if the first worked”:
read -swaits for you to type or paste a value and stores it under the name that follows. The-sstands for silent: what you paste is not shown.exportmakes that value visible to programs started from this terminal, such asforge tunnel. Without it, only the terminal itself would know the value.
Close the terminal and the value is gone, which is what you want for a key.
After you press Enter, the cursor waits on an empty line with no message: it is waiting for the key. Paste the key (nothing appears) and press Enter again.
Start forge tunnel from the same terminal.