
Real-time weather data is very important. The information allows you to make informed decisions. So, you can avoid abrupt weather changes. In this post, you will learn the way of adding real-time weather data to your dashboard using Weatherstack. Now, let’s dive in.
Table of Contents
What is Weatherstack?
Weatherstack is a REST API that provides historical weather. It has become very popular for user-friendliness, reliability, and as an open weather api with it’s free tier. The data accuracy level is very high. It is trusted by over 75,000 companies worldwide, including Microsoft and Warner Brothers.

Why should you use Weatherstack for building real-time weather dashboards?
- Provides highly accurate weather data in real-time
- Delivers super-fast performance by providing the response data in lightweight JSON format
- Supports a variety of location identifies, like name, ZIP code, and IP address
- Rock-solid uptime rate, which is close to 100%
- Offers bank-level security by utilizing industry-standard 256-bit HTTPS (SSL) encryption
How to Add Real-Time Weather Info to Your Dashboard with Weatherstack
To add the real-time weather info, you need to get the Weatherstack API access key. You can get it for free from here.
How to Implement Weatherstack with Python
1. First, you have to import the requests library.
1 |
import requests |
2. Then you have to include the access key and the query.
1 2 3 4 |
params = { 'access_key': 'YOUR_ACCESS_KEY', 'query': 'New York' } |
3. Now, you have to send a GET request to the Weatherstack API.
1 |
api_result = requests.get('https://api.weatherstack.com/current', params) |
4. Then, you have to store the response data in JSON format.
1 |
api_response = api_result.json() |
5. Finally, you can print the current temperature of the specified location, which is New York.
1 |
print(u'Current temperature in %s is %d℃' % (api_response['location']['name'], api_response['current']['temperature'])) |
Overall, the codes look like this:
1 2 3 4 5 6 7 8 9 10 11 12 |
import requests params = { 'access_key': 'YOUR_ACCESS_KEY', 'query': 'New York' } api_result = requests.get('https://api.weatherstack.com/current', params) api_response = api_result.json() print(u'Current temperature in %s is %d℃' % (api_response['location']['name'], api_response['current']['temperature'])) |
How to Implement Weatherstack with PHP
1. First, you have to declare a variable, called $location. It will hold a string of your preferred location, like New York.
1 |
$location = 'New York'; |
2. Now, you have to generate an URL encoded query string from the API key.
1 2 3 4 |
$queryString = http_build_query([ 'access_key' => 'YOUR_ACCESS_KEY', 'query' => $location, ]); |
3. Then you have to call curl_init function to write a formatted string to $ch variable.
1 |
$ch = curl_init(sprintf('%s?%s', 'https://api.weatherstack.com/current', $queryString)); |
4. Next, you have to call curl_setopt function.
1 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
5. Then you have to execute the cURL session.
1 |
$json = curl_exec($ch); |
6. Now, close the cURL session.
1 |
curl_close($ch); |
7. Next, you have to decode the JSON string.
1 |
$api_result = json_decode($json, true); |
8. Finally, you have to print out the temperature of the specified location.
1 |
echo "Current temperature in $location is {$api_result['current']['temperature']}℃", PHP_EOL; |
The overall codes look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$location = 'New York'; $queryString = http_build_query([ 'access_key' => 'YOUR_ACCESS_KEY', 'query' => $location, ]); $ch = curl_init(sprintf('%s?%s', 'https://api.weatherstack.com/current', $queryString)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $json = curl_exec($ch); curl_close($ch); $api_result = json_decode($json, true); echo "Current temperature in $location is {$api_result['current']['temperature']}℃", PHP_EOL; |
How to Implement Weatherstack with Node.js
1. First, you have to declare a constant, called axios.
1 |
const axios = require('axios'); |
2. Next, you have to include the Weatherstack API key and query in the params constant.
1 2 3 4 |
const params = { access_key: 'YOUR_ACCESS_KEY', query: 'New York' } |
3. Now, you have to create a GET request to the Weatherstack API URL. Then you have to hold response data in the apiResponse constant. Finally, you can print out the temperature of the specified location.
1 2 3 4 5 6 7 |
axios.get('https://api.weatherstack.com/current', {params}) .then(response => { const apiResponse = response.data; console.log(`Current temperature in ${apiResponse.location.name} is ${apiResponse.current.temperature}℃`); }).catch(error => { console.log(error); }); |
The overall codes look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
const axios = require('axios'); const params = { access_key: 'YOUR_ACCESS_KEY', query: 'New York' } axios.get('https://api.weatherstack.com/current', {params}) .then(response => { const apiResponse = response.data; console.log(`Current temperature in ${apiResponse.location.name} is ${apiResponse.current.temperature}℃`); }).catch(error => { console.log(error); }); |
How can I get started with Weatherstack?
Weatherstack is an outstanding weather API. It’s lightweight and super-fast. Also, its data are highly accurate. Besides, Weatherstack is more cost-effective than the other similar APIs. So, you should definitely consider using it.