Shashank Aggarwal
2 min readJun 19, 2020

An introduction to using Wikidata APIs

Wikidata is a very generic knowledge base, and contains information about (almost) everything under the sun. Think of it as a structured version of Wikipedia which can be accessed programmatically.

For example, if you want to display an input field on your website which allows people to pick their favourite musician, you could fire a Wikidata query (from your UI or backend) which gets a list of all musicians, and you can use this list to show a suggestion list. More interesting, however, is the additional logic you can apply while querying for musicians. For example, you can filter for folk rock musicians born after 1980 in the UK etc.

While exploring how to use WikiData, I found that there are two APIs which let us access Wikidata, each intended for different purposes.

1. The “wbsearchentities” API

This API is simple way of searching for entities using their labels or aliases. For example, if you would like to get all entities names ‘bond’, you can use the following GET query:

curl 'https://www.wikidata.org/w/api.php?action=wbsearchentities&search=bond&format=json&language=en&uselang=en&type=item&limit=20'

This returns a list of Wikidata entities whose label(s) OR alias(es) contain the word Bond. You can customize the output returned using different url parameters (see the docs). Some of the important ones are:

  • search : The text to search for
  • type : Type of entity to search for - item (default), property etc.
  • limit : Number of entities to fetch
  • continue : Offset to search from (useful for paginating the response)

This API is useful for building suggestion dropdowns (The official Wikidata page uses it too for its search bar). However, it does not let us apply logical constraints when fetching for data. For example, in the above query, we cannot apply a filter for entities which are instances of the Human entity if we wanted to fetch only entities which refer to a person.

2. The Wikidata SPARQL API

This API lets you access Wikidata intelligently. You can formulate complex queries by specifying constraints on the entities and properties. For example, if you wanted to fetch all entities named Bond but only if they refer to human beings, you can use the following query:

The query can be tested on the Wikidata Query Service page. Constraints are specified using the triples syntax:

<object> <relationship> <object>

We see 2 constraints in the above queries. Also, the Wikibase label service is used to get the names of the entities. For the official step-by-step Wikidata SPARQL tutorial, see this. Also, there is a really good introductory video on YouTube.

You can formulate much more complicated queries. Many interesting queries with explanations can be found here and here. These show us how to perform sorting, filtering, working with dates etc.

The API can be used by firing simple GET requests to the endpoint:

https://query.wikidata.org/sparql

For example, a simple Javascript program to perform the above query will look like this: