Categories: REST API Tutorials

How to Use the Visual Crossing Weather API with Java, Python, PHP, Ruby & JavaScript Examples

Weather plays an important role in all aspects of our lives. Not only do we rely on weather information for planning day-to-day activities like commute or other outdoor activities, but all major events like outdoor entertainment programs/sports matches heavily depend on the weather. Further, airlines, utility companies, fire stations also require accurate weather information. Thus, there is an ever-increasing need for precise weather information for locations all over the world. A number of REST APIs have emerged to satisfy this need. One such weather API is the Visual Crossing Weather API. In this article, we will be learning about this API and its features.

Connect to the Visual Crossing Weather API

What is the Visual Crossing Weather API?

The Visual Crossing Weather API is a forecast and historical weather API by Visual Crossing Corporation. It provides a 16-day global weather forecast. The weather forecast is available on a daily and hourly basis. It is available for all worldwide locations. Further, the API also provides historical weather information. The location can also be specified as a city, state, zip code, complete or partial address as well as a latitude/longitude combination. In addition to location, the endpoints accept a lot of other parameters which can be used to customize the output. The output consists of a number of weather-related fields like the minimum and maximum temperatures, humidity, heat index, wind speed, and direction, etc.

Who can use the Visual Crossing Weather API?

The Visual Crossing Weather API can be used by application developers in different types of web-based/mobile applications. Some of the types of applications that can be built using the API are listed below.
Weather applications – Since weather is so important to people in their day-to-day decision-making, there is a huge need for websites and mobile applications that provide the latest weather information. Thus, such weather applications can be built using the Visual Crossing Weather API. Users can use such applications to stay updated about the weather and plan their activities accordingly.

News applications – The weather is also a vital component of news, so most news websites include weather information. Thus, the Visual Crossing Weather API can be used to embed weather information within news websites. Providing weather data in addition to news can help such websites to increase their coverage.

Travel applications – Finally, the weather plays a crucial role while planning vacations. So, travel websites can use the Visual Crossing Weather API to provide weather information to their end-users. Providing weather information can help users to travel websites to plan their vacation accordingly.

How does the Visual Crossing Weather API work?

The Visual Crossing Weather API is a REST API. In brief, REST is a client-server architecture pattern. A REST service exposes some data on a server that can be retrieved by a client application and used as desired. REST services expose data in the form of HTTP URLs which are also known as REST endpoints. Most REST endpoints accept some input values (also known as REST parameters). They return an HTTP status code (indicating if the request was processed successfully or not) and optionally data corresponding to the request. The data is typically sent in JSON, XML, and CSV format. In addition, A REST endpoint can also be secured via authentication headers. Thus, a client application needs to send authentication information that is validated by the REST service and if valid, the request is processed.
The Visual Crossing Weather API works on the same principles. It exposes several endpoints that provide historical weather data as well as weather forecasts. An application can query an endpoint by supplying its URL, authentication headers (API key and API host provided by RapidAPI), and request parameters. The Visual Crossing Weather API then returns the requested information data.

How to use the Visual Crossing Weather API?

You can use the Visual Crossing Weather API via RapidAPI. RapidAPI not only lists the API endpoints but allows executing an endpoint and viewing its results. This feature makes it very easy for developers to understand an endpoint’s response without writing any code. Not only that, RapidAPI provides code snippets that can be used to invoke the API endpoints in various programming languages. Thus, developers can get started with the API with minimal effort.
To use RapidAPI to connect to the Visual Crossing Weather API, you need to follow the steps given below.

Step 1 – Signing Up

The first step is to create a RapidAPI account as described below.

a. Enter the Visual Crossing Weather API RapidAPI URL (https://rapidapi.com/visual-crossing-corporation-visual-crossing-corporation-default/api/visual-crossing-weather/ ) in a browser. Click on the Log In/Sign Up button in the top right corner. This redirects to the following authentication page:

b. Authenticate yourself using any of the methods shown on the page like Google, Github, Facebook, or via email. Upon successful authentication, the following page is displayed:

Step 2 – Subscribing to the API

The next step is to subscribe to the API. Subscribing to the API allows executing the API endpoints. The Visual Crossing Weather API has both free as well as paid plans. You can subscribe to a plan that is appropriate for you.

To subscribe to the Visual Crossing Weather API, you need to do the following:

a. Click on the Pricing Tab. This displays the following screen:

b. Select the desired plan. I have selected the Basic (Free) plan which allows 500 requests per month. This displays the following screen:

Step 3 – Executing an endpoint

As explained earlier, RapidAPI allows executing an endpoint and viewing the response within the browser itself.
To execute an endpoint, you need to do the following:

a. Click on the EndPoints tab. All the available endpoints are displayed on the left:

b. Click the endpoint that you would like to execute. I have selected the Historical weather report endpoint. Click the Test Endpoint button:

c. View the Response in the Results tab:

Step 4 – Obtaining a code snippet

Finally, RapidAPI provides code snippets in most of the popular programming languages which can be used to execute the API endpoints. Further, for each programming language, it provides code in multiple HTTP clients supported by the language (Since a REST API exposes its endpoints as HTTP URLs, a programming language specific HTTP client needs to be used to invoke an API endpoint). Thus, developers can obtain code for their desired programming language and desired HTTP client from RapidAPI without having to write code from scratch.
For example, to obtain a code snippet in C# using the RestSharp HTTP client, you need to do the following:
a. Click on C# -> RestSharp in the Code Snippets tab:

b. Copy the C# code shown and use it as desired:

Visual Crossing Weather API Endpoints

Let us now take a detailed look at the Visual Crossing Weather API endpoints.

Historical Weather Record

The Historical Weather Record endpoint returns the historical weather information for a given period of time and a given location. It accepts the following parameters:

  • startDateTime – This is a mandatory parameter of type String. It specifies the date and time from which the historical weather information is required. It needs to be specified in the yyyy-MM-ddTHH:mm:ss with hours being specified in 24-hour format.
  • aggregateHours – This is a mandatory parameter of type number. This specifies the interval between weather history data in the output. Possible values are 1 and 24. 1 represents hourly records, 24 represents a daily forecast.
  • location – This is a mandatory parameter of type String. It specifies the address of a location. It can either be specified as a city, state, zip code, postal code, or in terms of latitude and longitude. While specifying the address in latitude/longitude format, it needs to be specified as latitude, longitude where both latitude and longitude are in decimal degrees. Latitude should run from -90 to 90 and longitude from -180 to 180.
  • endDateTime – This is a mandatory parameter of type String. It specifies the date and time up to which the historical weather information is required. It needs to be specified in the yyyy-MM-ddTHH:mm:ss with hours being specified in 24-hour format.
  • unitGroup – This is a mandatory parameter of type String. It specifies the system of units for the output data. Supported values are us, uk, metric.
  • dayStartTime – This is an optional parameter of type String. This specifies the time of the day from where the historical weather data is required. When this parameter is present and not set to the same value as the dayEndTime, the endpoint returns only weather records between the specified day times. It needs to be specified in h:m:ss format.
  • contentType – This is an optional parameter of type String. It specifies the output data format. Possible values are csv and json.
  • dayEndTime – This is an optional parameter of type String. This specifies the time of the day up to which where the historical weather data is required. When this parameter is present and not set to the same value as the dayStartTime, the endpoint returns only weather records between the specified day times. It needs to be specified in h:m:ss format.
  • shortColumnName – This is an optional parameter of type Boolean. It specifies whether short column names should be specified or not. When false, the returned dataset includes descriptive column names. When true, shorter, abbreviated column names with only alphanumeric characters are returned. The short names are useful for programmatic use of the data.

The response consists of various weather-related fields like minimum temperature, maximum temperature, heat index, wind speed, and much more. For example, the endpoint returns the following response for location=Washington DC, startDateTime=2019-01-01, endDateTime=2019-01-03, aggregateHours=24 and contentType=json:

Weather forecast data

The weather forecast data endpoint returns the weather forecast for the specified location. It accepts the following parameters:

  • location – This is a mandatory parameter of type String. It specifies the address of a location. It can either be specified as a city, state, zip code, postal code, or in terms of latitude and longitude. While specifying the address in latitude/longitude format, it needs to be specified as latitude, longitude where both latitude and longitude are in decimal degrees. Latitude should run from -90 to 90 and longitude from -180 to 180.
  • aggregateHours – This is a mandatory parameter of type number. This specifies the interval between weather forecast data in the output. Possible values are 1,12, and 24. 1 represents hourly records, 12 represents 12-hour records while 24 represents a daily forecast.
  • shortColumnName – This is an optional parameter of type Boolean. It specifies whether short column names should be specified or not. When false, the returned dataset includes descriptive column names. When true, shorter, abbreviated column names with only alphanumeric characters are returned. The short names are useful for programmatic use of the data.
  • unitGroup – This is a mandatory parameter of type String. It specifies the system of units for the output data. Supported values are us, uk, metric.
  • contentType – This is an optional parameter of type String. It specifies the output data format. Possible values are csv and json.

The response is quite similar to the Historical Weather Data endpoint and consists of various weather-related fields like minimum temperature, maximum temperature, heat index, wind speed, and much more. For example, the endpoint returns the following response for location=Washington DC, aggregateHours=24 and contentType=json:

How to use the Visual Crossing Weather API with various programming languages

As explained earlier, RapidAPI provides code snippets in various programming languages like Java, Python, PHP, Ruby, Javascript, NodeJS which can be used to execute the API endpoints. Let us take a look at a few code samples.

Using the Visual Crossing Weather API with Java

In order to use the Visual Crossing Weather API with Java, click the desired API endpoint on the left. In the code snippets tab, select Java and the desired HTTP client. Copy the code snippet and use it as required.
For example, RapidAPI provides the following Java code for the Historical Weather Report endpoint using the AsyncHttp client:

AsyncHttpClient client = new DefaultAsyncHttpClient();
client.prepare("GET", "https://visual-crossing-weather.p.rapidapi.com/history?startDateTime=2019-01-01T00%3A00%3A00&aggregateHours=24&location=Washington%2CDC%2CUSA&endDateTime=2019-01-03T00%3A00%3A00&unitGroup=us&dayStartTime=8%3A00%3A00&contentType=json&dayEndTime=17%3A00%3A00&shortColumnNames=0")
.setHeader("x-rapidapi-key", "<your key here>")
.setHeader("x-rapidapi-host", "<your host here>")
.execute()
.toCompletableFuture()
.thenAccept(System.out::println)
.join();

client.close();

Using the Visual Crossing Weather API with Python

In order to use the Visual Crossing Weather API with Python, click the desired API endpoint on the left. In the code snippets tab, select Python and the desired HTTP client. Copy the code snippet and use it as required.
For example, RapidAPI provides the following Python code for the Historical Weather Report endpoint using the Requests client:

import requests

url = "https://visual-crossing-weather.p.rapidapi.com/history"

querystring = {"startDateTime":"2019-01-01T00:00:00","aggregateHours":"24","location":"Washington,DC,USA","endDateTime":"2019-01-03T00:00:00","unitGroup":"us","dayStartTime":"8:00:00","contentType":"json","dayEndTime":"17:00:00","shortColumnNames":"0"}

headers = {
'x-rapidapi-key': "<your key here>",
'x-rapidapi-host': "<your host here>"
}

response = requests.request("GET", url, headers=headers, params=querystring)

print(response.text)

Using the Visual Crossing Weather API with PHP

In order to use the Visual Crossing Weather API with PHP, click the desired API endpoint on the left. In the code snippets tab, select PHP and the desired HTTP client. Copy the code snippet and use it as required.
For example, RapidAPI provides the following PHP code for the Historical Weather Report endpoint using the HTTP v1 client:

<?php

$request = new HttpRequest();
$request->setUrl('https://visual-crossing-weather.p.rapidapi.com/history');
$request->setMethod(HTTP_METH_GET);

$request->setQueryData([
'startDateTime' => '2019-01-01T00:00:00',
'aggregateHours' => '24',
'location' => 'Washington,DC,USA',
'endDateTime' => '2019-01-03T00:00:00',
'unitGroup' => 'us',
'dayStartTime' => '8:00:00',
'contentType' => 'json',
'dayEndTime' => '17:00:00',
'shortColumnNames' => '0'
]);

$request->setHeaders([
'x-rapidapi-key' => '<your key here>',
'x-rapidapi-host' => '<your host here>'
]);

try {
$response = $request->send();

echo $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}

 

Using the Visual Crossing Weather API with Ruby

In order to use the Visual Crossing Weather API with Ruby, click the desired API endpoint on the left. In the code snippets tab, select Ruby and the desired HTTP client. Copy the code snippet and use it as required.
For example, RapidAPI provides the following Ruby code for the Historical Weather Report endpoint using the net::http client:

require 'uri'
require 'net/http'
require 'openssl'

url = URI("https://visual-crossing-weather.p.rapidapi.com/history?startDateTime=2019-01-01T00%3A00%3A00&aggregateHours=24&location=Washington%2CDC%2CUSA&endDateTime=2019-01-03T00%3A00%3A00&unitGroup=us&dayStartTime=8%3A00%3A00&contentType=json&dayEndTime=17%3A00%3A00&shortColumnNames=0")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request = Net::HTTP::Get.new(url)
request["x-rapidapi-key"] = '<your key here>'
request["x-rapidapi-host"] = '<your host here>'

response = http.request(request)
puts response.read_body

Using the Visual Crossing Weather API with Javascript

In order to use the Visual Crossing Weather API with Javascript, click the desired API endpoint on the left. In the code snippets tab, select Javascript and the desired HTTP client. Copy the code snippet and use it as required.
For example, RapidAPI provides the following Javascript code for the Historical Weather Report endpoint using the Axios client:

import axios from "axios";

const options = {
method: 'GET',
url: 'https://visual-crossing-weather.p.rapidapi.com/history',
params: {
startDateTime: '2019-01-01T00:00:00',
aggregateHours: '24',
location: 'Washington,DC,USA',
endDateTime: '2019-01-03T00:00:00',
unitGroup: 'us',
dayStartTime: '8:00:00',
contentType: 'json',
dayEndTime: '17:00:00',
shortColumnNames: '0'
},
headers: {
'x-rapidapi-key': '<your key here>',
'x-rapidapi-host': '<your host here>'
}
};

axios.request(options).then(function (response) {
console.log(response.data);
}).catch(function (error) {
console.error(error);
});

In addition, RapidAPI can also be used to obtain code snippets in many other programming languages like Node.js, C, Kotlin, Swift, etc.

What are the benefits of the Visual Crossing Weather API?

Well-documented – The most important benefit of the Visual Crossing Weather API is that it is well-documented. The API endpoints include descriptions that explain what the endpoint does. Further, the parameters to each endpoint also include descriptions, sample values, and default values as appropriate. This enables developers to understand and use the API without a steep learning curve.

Cost-effective – Another important benefit of the Visual Crossing Weather API is that is it cost-effective. It has a free plan that offers 500 requests per month. Moreover, the Pro plan costs only $10 per month and offers 10000 requests per month. Thus, the API is a good option for developers looking for an economical weather API.

Minimalistic – Finally, the Visual Crossing Weather API is minimalistic. It provides only two endpoints that provide the historical weather information and weather forecast. Thus, developers that are only looking for these basic features can use the API without having to browse through tons of API methods.

Other Weather APIs

Just like the Visual Crossing Weather API, there are many other APIs available on RapidAPI that provide weather information. Some of these are as follows:

1. AccuWeather API – The AccuWeather API is a comprehensive weather API. It includes a large number of endpoints that provide daily and hourly weather forecasts, weather conditions for top cities, and much more.
2. Weather API – The Weather API is another weather API that provides the current weather data. It provides a 16-day, 5-Day, and 120-hours forecast. It even provides hourly and minutely forecasts.
3. Air Quality API – The Air Quality API provides current, forecasted, and historical air quality for any point in the world.
4. AerisWeather API – The AerisWeather API is an advanced weather API. It provides information about weather alerts issued to the public by the US/Canadian Government, weather forecasts, current sun/moon information, and much more.
5. Yahoo Weather API – The Yahoo Weather API is another weather API. It provides weather information by location and geolocation.

Summary

So, to summarize, The Visual Crossing Weather API is a weather-related REST API. It exposes endpoints that provide historical weather data as well as weather forecasts. In this article, we saw how to connect to the Visual Crossing Weather API using RapidAPI. We also took a look at all the API endpoints, the benefits of the API, and other alternative APIs that provide weather-related data. Finally, we saw how to write code that invokes the endpoints in various programming languages.

Connect to the Visual Crossing Weather API
5/5 - (1 vote)
Share
Published by

Recent Posts

Power Up Your Enterprise Hub: New March Release Boosts Admin Capabilities and Streamlines Integrations

We're thrilled to announce the latest update to the Rapid Enterprise API Hub (version 2024.3)!…

3 weeks ago

Unveiling User Intent: How Search Term Insights Can Empower Your Enterprise API Hub

Are you curious about what your API consumers are searching for? Is your Hub effectively…

3 weeks ago

Rapid Enterprise API Hub Levels Up Custom Branding, Monetization, and Management in February Release

The RapidAPI team is excited to announce the February 2024 update (version 2024.2) for the…

1 month ago

Supercharge Your Enterprise Hub with January’s Release: Search Insights, Login Flexibility, and More!

This January's release brings exciting features and improvements designed to empower you and your developers.…

3 months ago

Enhanced Functionality and Improved User Experience with the Rapid API Enterprise Hub November 2023 Release

Rapid API is committed to providing its users with the best possible experience, and the…

5 months ago

The Power of Supporting Multiple API Gateways in an API Marketplace Platform

In today's fast-paced digital world, APIs (Application Programming Interfaces) have become the backbone of modern…

6 months ago