Create an app that uses openweather.org to grab the current weather in a location of your choice.
- Head over to openweather.org and sign up to use their free API.
- Find the API key page and generate a key to use. Paste it as a variable in your code as we will need it later.
- Read through the documentation about using the current weather data, and practice an API call in either Postman or your browser.
api.openweathermap.org/data/2.5/weather?q={city name}&appid={API key}
Replace the ={city name}
and ={API key}
with the city you want and your API key.
Remove the curly brackets
- Create a page using a text input, a submit button, and a space for the data to be displayed.
- Create a template card that will be used to display the data
Now is a good chance to test your page, open your index.html
on your favourite browser or run open index.html
from your terminal inside the weather-app folder. You may need to hardcode some expected data from the API.
- Target all the required elements using
document.querySelector()
- Add an eventListener with the argument of
click
to the submit button - Assign a variable that stores the value of the input field once the button has been clicked
Make use of
console.log()
in these steps!
- Outside of the event listener, create a new function
const createCardHtml = (text) =>
It will take in the parameters of the API data, but for now just pass the user submitted input as an argument. - In
createCardHtml
return the template card that was created in the html. Pass in the input value as one of the parts of your card to make sure it works!
You will need to return the string using template literals and the
return
keyword if you have not used the one line function style.
- In the event listener call the function passing in the input value.
const cardHtml = createCardHtml(cityInput.value);
- As the last line of the event listener, render the template card.
weatherContainer.innerHTML = cardHtml;
Read through the using Fetch guide on mdn, or the async await if you would prefer the newer methods.
- Create a new function that fetches the weather info from the API. Use either fetch or async await.
- Using template literals for the API url, insert the query parameters you have
fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${appId}&units=metric`)
- Format the response into a usable format with
.then(response => response.json());
console.log()
thejson
formatted response and make a note of what data you want to use. For example, the temperature is underdata.main.temp
It is important to find the correct data as not all API's will respond with the same format.- Call the function in the event listener, passing in the input from the text field.
- Instead of using
console.log()
for the data, pass the required information into thecreateCardHtml
function that was made earlier. - You may need to change the arguments for this function, and it should now look something like this:
const cardHtml = createCardHtml(name, emoji, temp, feelsLike, description);
- Fill in the template literals in the
createCardHtml
function so that the card is populated with the data from the API.
- It is important to keep up with the latest versions of the technology you use. Refactor the code to use Async Await
- Implement error handling with the API response
- Try using a geoLocation button that checks for the user's current location.
- Recreate the project using other technologies: Example
Stuck? Check out the provided example in the example/ folder!