API:Presenting Wikidata knowledge

Introduction
This shows how to retrieve and present facts from Wikidata about elements of your application.

Persistent language-independent facts
You can use Wikidata items and properties to provide language-independent information in your own application. This is more direct and consistent than presenting descriptions and snippets from Wikipedia encyclopedia articles, as API:Page info in search results explains.

Example


Inventory lets you create an inventory of your books and share with others. It displays certain properties from Wikidata about books, such as P364 "original language of work" and P50 "author". To do so it uses Wikidata's 'Q' numbers internally to identify books, for example its URL https://inventaire.io/entity/wd:Q180736 shows certain properties from the Wikidata entity http://www.wikidata.org/entity/Q180736 – the book "Les Misérables".

The Wikidata glossary explains entities and properties in more detail.

Recipe

 * 1) Find existing interesting wiki pages in the domain of your application, e.g. creative works, places, events, people, species.
 * 2) View the Wikidata information for those pages, choose interesting properties.
 * 3) Associate Wikidata entity IDs with elements of your application.
 * 4) Display their Wikidata information in the user's language
 * 5) Use the Wikidata "sitelinks" information about the item to provide links to the full Wikipedia (and Wikiquote, Wikivoyage, etc.) article about the element in the user's language

Get Wikidata entity IDs
To get an article's entity ID in Wikidata, you can:
 * copy the link "Wikidata item" ('wikibase-dataitem' message key) in the sidebar in most skins. It ends with 'Q ' NNNN'.
 * access the  variable in client-side JavaScript with
 * query the page for the page property :

Choosing interesting properties
If you view https://www.wikidata.org/wiki/Q180736, you can see
 * Some localized information:
 * "label"
 * "description"
 * "aliases" (displayed as "Also known as")
 * Many Statements about the item, that give values for its properties such as "author" and "publication date".
 * Many sitelinks about the item, providing the titles of pages about the item in various Wikipedias, also Wikibooks, Wikiquote, etc.

upright=2.0|right| This diagram shows you the most important terms you will hear around Wikidata.

Clicking the title of a statement takes you to a page about that property, for example the "author" property is Property:P50. Property pages in turn have labels, descriptions, aliases, and further statements, much like the Wikidata pages for real-world items.

The set of properties in Wikidata is steadily growing, there are thousands of them. See Wikidata:List of properties. Not all items in Wikidata have properties, and not all property values have been translated into all languages (for example Victor Hugo's occupation as a "author" has been translated into nearly all languages, but "draughtsperson" has fewer translations"). So you need to consider how to fallback if a property or value isn't translated into a language you are supporting, and you shouldn't build your application around a property that only appears in a few statements. (Of course you can help by contributing missing facts and translations to Wikidata.)

Querying wikibase
The extensions Wikibase Repository and Wikibase Client power Wikidata, together with related components. Most Wikimedia sites run Wikibase Client (check with Special:Version), while only wikidata.org itself runs Wikibase Repository. Wikidata Repository implements several modules for MediaWiki's Action API, all prefixed. The main workhorse API module in Wikibase Repository is  (see its generated API help). This returns the dataset Wikidata has about items (in QNNNNN entities) or properties.

Retrieve and display Wikidata information
Say you have associated Wikidata items with your application, and you want to display
 * the label and description of the item
 * author (property P50)
 * publication date property P577)
 * genre (property P136)
 * a link to the Wikipedia article for more information

You use  to get this information from Wikidata (see its generated help), which can return the same information that you see on an item's Wikidata page: labels, descriptions, aliases, "claims" (like statements), and sitelinks.
 * You can give  page titles on a wiki; but in this scenario, we provide it with the entity   of the Wikidata items for elements of our application.
 * You can specify the  you want for the information, and it will only return the description, labels, and aliases in that language (if they are available).
 * You can also specify  so that values and properties without a translation in your requested languages fall back to some value.
 * has no means to specify which properties you want, instead you request all claims about the entity. So in this scenario, we request.
 * You can specify a  for the wiki site links you want; in this scenario we only want the Wikipedia page (if any) on the wiki for the same language.

Let's ask for this information about Les Misérables in a less popular language, Azerbaijani, to see how  and   work.

You can see from the response that, the label for Les Misérables is available in Azerbaijani ("Səfillər"), but the description  falls back to the English description. And there is a wiki page for it on azwiki, az:Səfillər (roman).

Choosing sitelinks
The API help for  includes all the possible values for   and You can use Special:SiteMatrix to get a list of Wikimedia wikis with explanations. Wiki names are pretty standardized except for some edge cases, so it's safe to hardcode wiki names in .If you want to e.g. ask for links to Wikiquote sites that may not exist yet, you can also query its API module   and look through this output for relevant sites.

Parsing claims
Claims are unavoidably complex: there can be more than one and they may disagree, they differ in, they are (ideally) backed up by references, they may be qualified (for example the date range in which a claim applies).

As a result, for each property value you want, you must walk through an array of claims for it. In this example, for "author" and "genre" of Les Misérables, you would expect the value of the statement to be another item in Wikidata (rather than a simple number or date). To get the IDs for the genre, we are looking for (in the syntax for JSON elements used by jq): In pseudocode, you would locate  in the JSON response, then for each element in the array, you would check that its   exists and has the value "item", then you can safely access the. The result of all this is a set of numbers of items, in this case 8261 and 192239.

You then need to request the labels of items Q8261 and Q192239 in the user's language, using the same technique perhaps only requesting ), you can instead invoke the API module  . It returns similar information:

Alternatives
You can associate an element of your application with a page in a particular language's Wikipedia. Then as Page info in search results shows, you can query for and display useful information from that article such as a lead image thumbnail, opening text, and description (, |pageimages|pageterms&format=jsonfm&exsentences=2&exintro=&piprop=thumbnail&wbptterms=description&titles=Albert%20Einstein&formatversion=2 try it). A downside of this is page titles change so you may have to deal with redirects. Another is it's not multilingual: you have to know the page's title in other wikis (for example, the article in Greek Wikipedia about Les Misérables is Οι Άθλιοι), or track down a "sitelink" to the page in another language. That's why that article talks about page info in the context of search – if your user is searching for articles from a wiki, you know her language and the wiki to query.

Over time, this common information about articles in individual wikis is moving to Wikidata: So querying for Wikidata information is aligned with future developments.
 * is already returning the description of the page from Wikidata
 * The "Wikivoyage banner" image for places on Wikivoyage sites is now a property (P948) of the Wikidata item for that place (example of San Francisco).
 * The sitelinks to the same article in other languages and in other kinds of wikis are all maintained in Wikidata (example of San Francisco).