Here are some resources that you can use to develop applications with the data on Data.gov.sg. If you are using the APIs listed here, please review the Website Terms of Use, the API Terms of Service, and the Singapore Open Data Licence.

Real-time APIs

Explore our real-time APIs at our Developer's Portal.

CKAN APIs

The prefix for web resource endpoints is https://data.gov.sg/api/3/action and they return responses in JSON format. You can retrieve dataset and resource metadata through these functions:

Resource Functions Description
/package_metadata_show?id={package id}

Gets the metadata of a dataset and all of its data resources. All of the data resources' download link, last updated date, etc. can be retrieved here.

/resource_metadata_show?id={resource id}

Gets the metadata of a specific data resource. This function is a subset of package_metadata_show as it would have listed all of its data resources. The data resource download link, last updated date, etc. can be retrieved here.

The Full CKAN API documentation can be found here.

If you need some code examples to get started, please read on the section on examples.

To go deeper and query specific rows of data in a data resource without the need to download for the entire resource file, please read on the section on the Datastore

Code Examples

Example #1: Get the metadata of a Dataset (or Package):

  1. Identify the appropriate web resource method to use: package_metadata_show
  2. Identify the dataset you want and retrieve it's ID, for instance: dengue-cases
  3. Create the URL call: https://data.gov.sg/api/action/package_metadata_show?id=dengue-cases.
  4. Make a web request to the above URL and you will receive a response. After parsing the JSON response into an object, check if the request was successful by ensuring that the response-json-object.success value evaluates to true before proceeding.

The following example shows how you can use Python to retrieve dataset information from the site:

#!/usr/bin/env python
import urllib2
import urllib
import json
import pprint

# Use the json module to dump a dictionary to a string for posting.
data_string = urllib.quote(json.dumps({'id': 'data-explorer'}))

# Make the HTTP request.
response = urllib2.urlopen
('https://data.gov.sg/api/action/package_metadata_show?id=dengue-cases',data_string)
assert response.code == 200

# Use the json module to load CKAN's response into a dictionary.
response_dict = json.loads(response.read())

# Check the contents of the response.
assert response_dict['success'] is True
result = response_dict['result']
pprint.pprint(result)
    

The following example shows how you can use Javascript and jQuery to retrieve dataset information from the site:

var data = {
id: 'dengue-cases'
};

$.ajax({
url: 'https://data.gov.sg/api/action/package_metadata_show',
data: data,
dataType: 'jsonp',
success: function(data) {
alert('Response status: ' + data.success)

// do more with the response
}
});
    

Example #2: Get the metadata of a Resource:

Important: Note that retrieving the dataset metadata already includes metadata for all of its data resources.

  1. Identify the appropriate web resource method to use: resource_metadata_show
  2. Identify the data resource you want and retrieve it's ID, for instance: a4755f45-10d8-4386-b66c-ee02208f2076.
  3. Create the URL call: https://data.gov.sg/api/action/resource_metadata_show?id=a4755f45-10d8-4386-b66c-ee02208f2076.
  4. Make a web request to the above URL and you will receive a response. After parsing the JSON response into an object, check if the request was successful by ensuring that the response-json-object.success value evaluates to true before proceeding.

Refer to CKAN's API Documentation for more detailed i nformation on how to execute the API calls.

DataStore API

The Datastore API is a wonderful tool for you to query specific rows of data within a data resource. This is especially useful if a data resource has a million rows and you only just require the first ten rows for use. You can query for data using the datastore_search resource function. Each row in the datastore corresponds to a Primary Key, identified by the _id field.

Example: Using a generic search

  1. Identify the data resource you want and retrieve it's ID, for instance: a4755f45-10d8-4386-b66c-ee02208f2076.
  2. Create the URL call: https://data.gov.sg/api/action/datastore_search?q=2012&resource_id=a4755f45-10d8-4386-b66c-ee02208f2076 .
  3. Make a web request to the above URL and you will receive a response. After parsing the JSON response into an object, check if the request was successful by ensuring that the response-json-object.success value evaluates to true before proceeding.

Query parameter q can be either a string or a dictionary. If it is a string, it will search on all fields on each row. If it is a dictionary as {"key1": "a", "key2": "b"}, it will search on each specific field.