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:
Create a function in a web method to call the weather service and retrieve the data:
Import the functions needed to make https
requests and to get secrets from the Secrets Manager.
For example:
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:
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:
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:
Here is the complete code for this example, without comments: