Friday, December 26, 2014

How to Create an API Listing Nigerian States or Local Government Areas

How to Create an API for listing Nigerian States and Local Government Area using Linked Data from the Web of Data


Subjects: , , , ,

Situation Analysis:

Mfoniso Ossom ( posed the question on Silicon Africa Facebook group (, "Is there a public API that provides a listing of Nigerian states, Cities, LGA e.t.c?". Investigation has shown there was none as the time of this post.


How do I create an API out of a document that comprised of data representing States or Local Government Areas of ? We know that Wikipedia documents hosts these data so we can use DBPedia data as well as any other additional data source on the web.


  1. Use any server side programming language like PHP, Ruby, Python, Perl, ASP, etc to exploit SPARQL via REST based web service.
  2. Determine which SPARQL endpoint, a REST web service URI, you want to access e.g DBpedia ( or a local virtuoso instance (typically http://localhost:8890/sparql). SPARQL endpoints provides a REST API to exploit RDF documents. The URL we'll send will start with the SPARQL endpoint and then pass it some query string parameters, "query" and "output".
  3. Construct the SPARQL query to list the data
      ## SPARQL Query
      PREFIX rdfs: <>
      SELECT ?s1 as ?stateURI str(?s4) as ?stateName str(?s5) as ?stateDesc
      ?s1 a <> .
      ?s1 <> ?s3 .
      ?s1 rdfs:label ?s4 .
      ?s1 rdfs:comment ?s5 .
      FILTER ( ?s3 in ( <> ) ) 
      FILTER langMatches( lang(?s4), "en" )
      FILTER langMatches( lang(?s5), "en" ) 
  4. URL Encode the SPARQL query and add it to the "query" parameter in the HTTP URL (e.g[encoded sparql here]).
  5. Add the query output format to the "output" parameter (e.g[encoded sparql here]&output=json).
  6. This URI is usable anywhere on the Web.
  7. Make the HTTP request to the URL and return the response body as a string.
  8. Using the right content header, you can write to standard output.


  • Source codes for the Local Government Areas API - here.
  • Source codes for the States API - here.

Live Demo:

  • Local Government Areas API - here.
  • States API - here.
  • Note: Options for query parameter 'f' are json,csv,xml,ttl,rdf.

References - - - - -

Keywords: linkedopendatang, open data, linked data, nigeria, sparql, states, local government areas, api


  1. Emeka, this cool. It seems that some states are missing in the data returned. We have just 21 states. Is there some reliable data source you recommend?

  2. This comment has been removed by the author.