Tutorial: Use External API Keys Stored in the Secrets Manager to Call the OpenWeatherMap API

In this tutorial, you'll learn how to securely use external API keys stored in the Secrets Manager to call the OpenWeatherMap API. By keeping your API keys safe and confidential with the Secrets Manager, you can make secure API requests without exposing sensitive information in your code.

We'll use the following steps to call the OpenWeatherMap API with external API keys:

  1. Save your API key in the Secrets Manager.
  2. Configure your code to retrieve the API key and call OpenWeatherMap.

Step 1 | Save your API key in the Secrets Manager

  1. Access the Secrets Manager. There are 2 ways to navigate to the Secrets Manager:
  • Select Developer Tools from the code panel. Under the Security section, select Secrets Manager.
  • Select Developer Tools in your site's dashboard, and then select Secrets Manager.
  1. Add and save the API key for the OpenWeatherMap API. For puposes of this tutorial we have named this key "WeatherApiKey".

Step 2 | Write code to retrieve the API key and call OpenWeatherMap

Create a function in a web method to call the weather service and retrieve the data:

  1. Import the functions needed to make https requests and to get secrets from the Secrets Manager.
    For example:

    Copy
  2. Create a new function and do the following:
    a. Takes in a city whose weather you want to look up.
    b. Defines the service's address.
    c. Retrieves the API key from the Secrets Manager.
    For example:

    Copy
  3. Continue the function by constructing the full URL for the fetch request. The URL is made up of the service's address and an API key.
    For example:

    Copy
  4. Finish the function by receiving a response from the API call. When you receive the response, pull out the temperature data and return it.
    For example:

    Copy

Example Code

Here is the complete code for this example, without comments:

Copy

See also

Was this helpful?
Yes
No