Make weather app with VueJS and available API

Tram Ho

Preamble

Hello everyone, I am new to a free weather forecast api with 60 calls per minute or 1 million calls per month, it has weather data for more than 200,000 cities and is collected and processed. managed in a variety of ways from satellites, radars or weather stations. It also has a lot of return formats like JSON, XML, HTML. So I try to make an application to see the weather from the api.

Conduct

First, if you want to have an API, you need to access the page OpenWheather, If you do not have an account, please register an account, next you choose Pricing then choose Get API key then choose next API keys Here there will be a default key you can use always or you can create a new key according to you. Female key This will be passed in when we call the api to get weather information. A note is that when registering for an account, it will send a verification email to your account registration email, so you need to verify to be able to use this key (when you have registered but have not verified your email). it still has a default key but when calling the api using that key, it will get a 401 error). Next is about Api, you choose API then choose API doc of “Current Weather Data” because this is free. We will have an api URL of the type https://api.openweathermap.org/data/2.5/weather?q={cityname}&appid={APIkey}. With 2 Params is the cityname APi key corresponding to the city name you want to see the weather and the key that we just created above. There are also some other Parameters such as:

  • mode: Specifies the return type. There are xml and html values. If you don’t use it, the default is json
  • units: Units of measurement include standardmetricimperial. If you don’t use it, the default will be standard. For unit imperial will return the temperature in degrees F, metric then it will return the temperature in degrees Celsius, and by default, the temperature will return in degrees Kelvin.
  • lang: Data return language

You can see details about API return fields at this. Because it is quite long and the above doc has also explained in detail, I will not include it in the post. OK now let’s test to see if the api works, I will use 2 more Parameters that are units = metric and lang=vi so that the returned result has the temperature in degrees Celsius and the language is Vietnamese. Here is the result

So we already have an API, next we need to build an interface to display data from the api. My interface will look like this

When the user enters the city name, it will make an api call to return the result. First of all, you have to create a Vue project first, and remember to install them all axios with momentjs because I will need to use it later. The interface style you can see in the source code, I will leave the github link below, Here I will focus on how to process and display data. When the user enters the city, he will make an api call to get the data, so I have an event in the search tag getAPI when the data changes as follows

OKi so got the data. Next is the date and time part and now this I will take the current time and format according to the style I want to display + language.

The location part, I will get it from the name field in the data returned by the api. At first, if you don’t call the api, it will be - -

Next is the weather icon part OpenWheather give us the icon URL of the form http://openweathermap.org/img/wn/10d@2x.png. In data returns the part weather[0].icon will return the corresponding icon of the weather so we just need to change 10d with icon return that is okay. I have an icon like this

If there is no data, I also display an icon for beauty.

The temperature part is just taken out from the magnetic field temp of data and then display it. If you don’t have data, you can also display a value in it

The sunrise and sunset parts are located at the school, respectively sunrise and sunset need to reformat before displaying, do the following:

Finally, the humidity and wind speed are located in the field humidity and speed cWe just need to display it, the default wind speed is m/s you can change it back to km/h by multiplying by 3.6.

That’s it this is the result

Conclude

So, I have introduced to you a weather API so that you can make your own weather viewing application, you can go to the homepage of the website. OpenWheather to find out more details. My article ends here, thank you for watching, if you find the article useful, please give me an upvote, click follow to update the latest posts from me. Thank you

Source code: https://github.com/hoangbh-0986/open-weather

Share the news now