Forge repo instructions - 04-app-specific-env-variables-solution

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

  1. In your command line, type the following Forge CLI command while in your project directory.

    forge variables set --encrypt
    
    

    Follow the prompts to create the encrypted API_TOKEN variable. 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 --encrypt option protects the API token value from being displayed by forge variables list.

  2. 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

  1. Open the resolver file: src/resolvers/index.js

  2. Update the fetchDogInfo function to use the environment variable

    const fetchDogInfo = async () => {
    
      // Hardcoded for now
      const breedName = "chihuahua";
      const apiKey = process.env.API_TOKEN; // Updated value referencing the environment variable
    
    }
    
    

Redeploy the app

  1. Run the Forge deployment command on your CLI

    forge deploy
    
    
  2. Go to your work item and refresh the page. The app should still display the same details as the previous exercise.