It’s been a few weeks since the Kenyan government unveiled it’s new open data initiative, KODI, to the public – a first in Africa and a milestone for e-governance in Kenya and Africa. For the first time, this opens up doors to what was previously either inaccessible or hard to obtain data. The government swiftly followed up with the publication of the Hansards, that is, records of parliamentary proceedings on Google Books, a joint effort by the Kenya National Council for Law Reporting and Google Kenya.
One of the more interesting aspects of Open Data is the opportunity Open Data affords by allowing not only consuming and visualizing the data but also giving developers a chance to interact with the data programmatically and hence be able to use the data to build innovative applications and mash up the data in interesting ways.
To highlight just how cool data is and how significant it’s availability is to developers, consider the stunning talk given by Hans Rossling back in 2006 at the TED conference. Not only was the talk riveting and engaging but it highlighted previously stale data in a way that made sense and brought out patterns and insights that may not have been so easily come about with plain old data in rows and columns.
But just how easy is it to program the data that the Kenyan government has availed?
The Kenyan government chose to turn to Socrata, a company that offers turnkey open data solutions for governments. This choice has raised several concerns within the local tech community about why the government could not instead choose to use local developers and host the data locally.
Well, while there may be differing opinions on this matter, this article is not concerning itself with the politics or rightness or wrongness of this choice by the government. We’re looking into what KODI has to offer the developer and one thing is for sure, Socrata has a robust, proven platform and provides a very simple API for developers. Let’s look at this API a bit more and see what we can do with it. This is just a basic overview, you can get the full details of how to work with the Socrata API on the Socrata Developer site.
The RESTful API
Socrata provides a really simple to use Application Programming Interface that is largely RESTful. You can fetch data by making simple HTTP GET requests.
Datasets are generally referred to as ‘views‘ and each view has an id e.g. ‘rntu-q8pe‘ which is the Baringo Age Pyramid dataset.
For view API calls (the other type of API call is for users) you can basically query the data using a GET request in the form of baseurl/api/views/viewid.returnformat e.g. opendata.go.ke/api/views/rntu-q8pe.json. It’s that simple.
You can also query for row data: baseurl/api/views/viewid/rows.returnformate.g. opendata.go.ke/api/views/rntu-q8pe/rows.json
You can also query for column data: baseurl/api/views/viewid/rows.returnformat e.g. opendata.go.ke/api/views/rntu-q8pe/columns.json
To get data about all the available views just call /baseurl/api/views.json e.g opendata.go.ke/api/views.json
You can get data back in different formats including JSON, XML, CSV and RDF just by changing the returntype e.g. from …/rntu-q8pe.json to …/rntu-q8pe.xml
Furthermore you can append query strings to filter the data further. For example to filter out only views tagged ‘expenditure’ – http://opendata.go.ke/api/views/?tags=”expenditure”
Well, that’s great as far as API calls are concerned but how exactly would you go about programming an actual application? One has to first register their application to get an application token. There are two ways by which Socrata authenticates applications OAuth2 and HTTP Basic Authentication. Socrata provides some sample code in different languages for you to get started with. Socrata also have libraries for the major programming languages such as Java that allow you to manipulate the data in a much easier manner than handling raw JSON or XML e.g. with the Java API you can make a connection using a “Connection” class and manipulate data using ‘View’ class.