top of page
City Weather

City Weather

Get weather information according to city

Intermediate.png

Intermediate

2K

Published:

January 19, 2021

Girl Enjoying her Drink

by

Anchor 1
Hire a Developer

Example Description

In this example, we fetch data from a third-party weather service to create our own customized weather widget. We allow site visitors to query weather data based on city name.

Example Code

Tab 1

.

Tab 2

.

Tab 3

.

This code solution can be complicated.

How We Built It

We used OpenWeatherMap as our third-party service for retrieving weather information. In order to use this API you will first need to register for an API key on their site.


Secrets Manager


We added the API key that was provided by OpenWeatherMap to the Secrets Manager under the name OpenWeatherApi. The Secrets Manager can be accessed using the Settings section of your site's dashboard. In order to use this example in your account, make sure to update the Secrets Manager with your OpenWeatherMap API key.


npm Package


We used the ‘moment’ npm package in order to easily format the current time.



Page Elements


We added the following elements to our site:

  • Text input: for entering the city name. We will validate this input using pattern validation.

  • Button: for fetching the weather.

  • Preloader image: for indicating that we are waiting for the third party service response.

  • Five response text elements: Location (city, country), Temperature (celsius), Weather description, Time of executing the call, Error message

  • Image: for displaying an appropriate weather icon.


Backend code


In getWeather.jsw, we have one function that provides weather data based on city name. The function first gets the OpenWeatherMap API key’s value from the Secrets Manager. Then, we create a constant which includes the third party service API call. Using this constant, a fetch call is made to get weather informations, which is returned in JSON format.


Page Code


When a site visitor enters the site, the following occurs:

  1. We set a custom validation handler for the city text input. The handler uses RegExp to ensure the value exists and contains only letters and spaces.

  2. We focus on the input text element, and register an onClick event handler that is triggered when the site visitor clicks the execute button.

  3. The execute button’s onClick handler will first check if the input is valid, using the ‘valid’ attribute that triggers the custom validation handler:

  4. If the value is valid: the loader image will expand, and a call to the backend function getCityBasedWeather(city) will be made, using the input value as the city parameter. After the response from the function is recieved, we update the page elements with the appropriate data.

  5. If the value is not valid: handleError() will be called. handleError() gets the validationMessage value from the text input and assigns it to the expanded errorText element. We collapse the rest of text and image elements, to shift attention towards the error message.

Hire a Developer

Velo solutions are powerful tools, but building them on your own can be challenging. Let an experienced Velo development shop build it for you, so you can keep working on your site or business.

Related Examples

Did this help?

Yes

|

No

Thanks for your feedback!

Secrets Manager

Secrets Manager

Store API keys in a secure manner using Secrets Manager.

Intermediate.png

Intermediate

Create a Weather Widget

Create a Weather Widget

Fetch data from a third-party service to create a widget.

Intermediate.png

Advanced

Currency Converter

Currency Converter

Convert a specified amount of money from one currency to another

Intermediate.png

Intermediate

Anchor 2
bottom of page