Skip to main content
Skip table of contents

Code Examples

We have put together a number of examples of using different languages to both get data from the API, and to read the resulting JSON.

Curl | Python | JavaScript | PHP | Ruby

cURL

cURL is a command available in the terminal of many Unix (Linux, MacOS) computers which can download something from the internet to your local server. All you have to do is be sure to enclose the API URL in quotes!

NONE
curl "https://api.synopticdata.com/v2/stations/metadata?&token={YourToken}&stids=WBB,MTMET"

This will leave a file on your computer (depending on the output settings for cURL you use). You’ll have to read the resulting JSON file using one of the reading methods shown below

Python

Our data services are almost all written in Python, so we would definitely recommend you to use python to get the data as well! This uses Python 3.

Getting data

Using the built-in urllib library, we can get API data, but you have to give the arguments to the API URL directly. Below this example we show one using the popular (it may already be available to you) requests library, which can take your API requirements in a dictionary.

PY
import urllib.request as req
import os.path
import json

API_ROOT = "https://api.synopticdata.com/v2/"
API_TOKEN = "[your api token]"
# let's get some latest data
api_request_url = os.path.join(API_ROOT, "stations/latest")
# the built-in library requires us to specify the URL parameters directly
api_request_url += "?token={}&stid={}".format(API_TOKEN, "KLAX")
# Note, .format is a python method available to put values into strings
# then we make the request
response = req.urlopen(api_request_url, api_arguments)
api_text_data = response.read()

Using the Requests package:

PY
import requests
import os
API_ROOT = "https://api.synopticdata.com/v2/"
api_request_url = os.path.join(API_ROOT, "stations/latest")
api_arguments = {"token":API_TOKEN,"stid":"KLAX"}
req = requests.get(api_request_url, params=api_arguments)
req.json()

Reading API data with Python

Though you can request CSV data format for a limited number of requests, Python is very good at understanding JSON, and turning it into data you can work with. To do this, you should use the json package, which comes standard.

PY
import json
use_data = json.loads(api_text_data)
# Now you can work with use_data because it is a dictionary of the data the API returned.
use_data['STATION'][0]

The .json() function you can use from the Requests package reads and decodes the JSON returned on its own, so you can skip this step.

JavaScript

There are a number of ways to make data requests with JavaScript. For simplicity we are going to use jQuery, which is a high-level framework that hides essentially all the work of making a request and decoding the JSON data. In JS, making a request means using a callback function. When you send the request to be made, your code will not stop and wait for the request to complete. Instead, you will tell the request a function to call when that request is done. This function is called a ‘callback’.

JS
var tkn = "[my public token]";
$.getJSON('https://api.synopticdata.com/v2/stations/latest',
	{
		// specify the request parameters here
		stid:stn,
		within:1440,
		token:tkn
	},
	function (data)
	{
		/*
		* do something with your returned data
		*/
	}
); 

Server-side JavaScript, usually in the form of NodeJS, is a popular and powerful programming language. Though jQuery will work in Node, you can also use the fetch method. Because that is a more technical concept, and because it works differently from the example above, we will not show an example here. Fetch also has built-in methods to read and understand JSON data.

If, for some reason, your request cannot decode the JSON itself, here is the line you would use to get structures from JSON data in JavaScript

JS
var data_dict = JSON.parse(json_string);

The following examples are just some of the languages we have built applications with that required using the API. Please contact us if you have any other language examples we could feature here!

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
<?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("https://api.synopticdata.com/v2/stations/nearesttime?{$apiString}");
$data = json_decode($response, true); 
// leave off the following true if you would like a PHP object instead of an 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.

RUBY
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("https://api.synopticdata.com/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

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.