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


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 "{YourToken}&stids=WBB,MTMET"


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).

		// specify the request parameters here
	function (data)
		* do something with your returned data


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.


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.


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.

//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("{$apiString}");
$data = json_decode($response); 
//$data = json_decode($response,true); for associative array
//Do stuff with the PHP variable!


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.

#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(""+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