Secure your API token by using app-specific environment variables
Are you feeling uneasy with your API token casually written in your index.js file? Worry not! The Forge platform allows you to create app-specific environment variables. In this quick exercise, we’ll work together on how you can easily create these environment variables.
Create an encrypted environment variable for your API token
-
In your command line, type the following Forge CLI command while in your project directory.
forge variables set --encryptFollow the prompts to create the encrypted
API_TOKENvariable. The API token input is hidden while you type.Setting the value of a variable in the development environment. Press Ctrl+C to cancel. Use a new key to create a variable, or use an existing key to override a variable. ? Enter the variable key: API_TOKEN ? Enter the variable value: [input is hidden]When the variable value prompt is shown, paste your API key and press
Enter. The value will not be displayed in the terminal.The
--encryptoption protects the API token value from being displayed byforge variables list. -
Verify if your encrypted app-specific environment variable has been created successfully. The variable name should appear in the list, but its value will be hidden.
forge variables list
Update your resolver file to use the newly created environment variable
-
Open the resolver file:
src/resolvers/index.js -
Update the
fetchDogInfofunction to use the environment variableconst fetchDogInfo = async () => { // Hardcoded for now const breedName = "chihuahua"; const apiKey = process.env.API_TOKEN; // Updated value referencing the environment variable }
Redeploy the app
-
Run the Forge deployment command on your CLI
forge deploy -
Go to your work item and refresh the page. The app should still display the same details as the previous exercise.