get started!

Mesonet API | Reference | Accepted Variables | Code Examples | Release Notes

Code Examples

Here are some examples using various popular programming languages to get you up and running with Mesonet API

Curl

Because we use simple GET requests, you can use ordinary commands like curl or wget to download data from our API into your servers and processes.


curl "http://api.mesowest.net/v2/stations/metadata?&token={YourToken}&stids=WBB,MTMET"
            

Javascript

Using jQuery, we utilize a JSONP request, which is simply a getJSON query with a callback argument in the URL. These kinds of queries are allowed across domains (whereas regular JSON/AJAX queries must fulfill the same-origin policy).


$.getJSON('http://api.mesowest.net/v2/stations/nearesttime?callback=?',
	{
		// specify the request parameters here
		stid:stn,
		within:1440,
		token:tkn
	},
	function (data)
	{
		/*
		* do something with your returned data
		*/
	}
); 
            

Python

With Python, JSON objects translate effectively to dictionaries. We just use the urllib2 library to grab the API query output, and the JSON library to convert the output to the dictionary object.

Python can also use the '&output=csv' parameter from the API to save a CSV file from a URL into memory and convert it to nested lists.


Matlab

With Matlab, JSON objects can be converted to cell structures by using the JSONLab package.

The same Matlab code can be modified to use a CSV format API return as well without the need for extra packages.


PHP

With PHP, the file_get_contents function grabs the contents of the API query in a straightforward manner, and then json_decode converts the response into a PHP object or associative array.

<?php
//Specify Request Parameters
$stid = "WBB";
$within = "1440";
$token = "YourToken";
//Construct the query string
$apiString = "stid={$stid}&within={$within}&token={$token}";
//Get the raw JSON object and convert to a PHP variable
$response = file_get_contents("http://api.mesowest.net/v2/stations/nearesttime?{$apiString}");
$data = json_decode($response); 
//$data = json_decode($response,true); for associative array
//Do stuff with the PHP variable!
?>

Ruby

In Ruby, the "net/http" and "uri" libraries are needed to call the API and get back the response. Note that this response is treated as a string in Ruby, so to convert the JSON into a more useful hash, you will need one of the JSON conversion utilities, the most common gem being shown in the comments here.

#!/usr/bin/ruby
#Require these two libraries
require "net/http"
require "uri"
#To easily convert the returned JSON string to a Ruby hash
#also require "rubygems" and "json"

#Specify request parameters
stid = "WBB"
within = "1440"
token = "YourToken"

#Construct the query string
apiString = "stid="+stid+"&within="+within+"&token="+token

#Parse the API URL and get the body of the response (the JSON)
uri = URI.parse("http://api.mesowest.net/v2/stations/nearesttime?"+apiString)
response = Net::HTTP.get_response(uri)
dataString = response.body #JSON comes in as a string
#data = JSON.parse(dataString) Converts JSON string to a Ruby hash