Wikidata Query Service/User Manual/MWAPI

Jump to navigation Jump to search

MediaWiki API Query Service[edit]

MediaWiki API Service allows to call out to MediaWiki API from SPARQL, and receive the results from inside the SPARQL query.


The query is initiated by SERVICE with URL wikibase:mwapi.


Query inputs are specified as service parameters, in clause looking like:

       bd:serviceParam mwapi:language "en" .

This defines parameter "language" in the query.

There are two special parameters, api and endpoint, which are always required and identify the endpoint host (wiki) and the specific API used. The API endpoints are pre-defined in configuration, see below. Currently, only pre-configured endpoints are allowed.


       bd:serviceParam wikibase:api "EntitySearch" .
       bd:serviceParam wikibase:endpoint "" .

The rest of the parameters use predicate starting with mwapi: and the name is the name of the parameter in the query, e.g. mwapi:srsearch. The value is the value that will be used in the query, it can be a constant or a variable. If the variable is not bound when the query is run, the parameter is skipped or, if it is required, as defined by the configuration, an error happens.

It is permissible to add input parameters not specified in the configuration, they will be passed to the service query. Please refer to the API documentation for the lists of parameters each service has. The configuration also defines some default parameters – if you override them in the service parameters, you may want to merge the default value with your desired value to ensure the output looks as expected by MWAPI (e. g. use mwapi:ppprop "wikibase_item|page_image_free" instead of just mwapi:ppprop "page_image_free").

Currently supported endpoints are: *,,,, If any other wikis need to be supported, please leave a comment to the developers and we will enable them.


By default, WDQS will try to fetch all the results of the query, with continuations. You can set the page size by using service-specific limit parameters, but the service will use continuation to fetch the following pages. This can be controlled by the wikibase:limit parameter, which will limit total number of rows fetched in one API request:

 bd:serviceParam wikibase:limit 50 .

This will fetch only 50 rows, regardless of pagination settings. Note that SPARQL LIMIT clause will cause the same effect, but will be applied only after the API call has finished - all the data has been fetched and then the extra data has been discarded. It is more efficient to use the internal limit.

A special value of "once" disables the continuation mechanism:

 bd:serviceParam wikibase:limit "once" .

This makes the service to stop after the first API call and not use continuations. The number of results depends on service parameters.

There is a hard-coded limit of 10000 rows which always applies.


Mediawiki API services are asked to return the results in XML, from which they are extracted to output parameters using XPath. There are two ways to specify output parameters: by referring to the configured parameter and by specifying XPath directly.

The configured output parameter would be used like this:

       ?title wikibase:apiOutput mwapi:title .

This results in the variable ?title being bound to the XPath result defined by the "title" output variable. Note that the configured output parameters expect certain input parameters to be present, and if you override the configured defaults the output parameters may break: for example, the configuration specifies a default parameter of prop=info|pageprops, and if you override it with mwapi:prop "revisions", then the mwapi:lastrevid output parameter will no longer be able to get the lastrevid from the response. (You would fix this by looking up the default prop in the configuration and then changing your query to use mwapi:prop "info|pageprops|revisions".)

The direct XPath expression is used like this:

     ?ns wikibase:apiOutput "@ns" .

In this case, the object of the triple is an XPath string and not mwapi: URI.

In both cases, XPath is evaluated relative to item path, specified in service configuration. There's no difference in XPath evaluation for both forms.

The predicates that can be used are wikibase:apiOutput, wikibase:apiOutputItem and wikibase:apiOutputURI. The first form results in a string taken literally; the second interprets the string as Item ID and constructs a full item URI; and the third interprets the string as an URI and constructs an URI value.

Predicate wikibase:apiOrdinal allows to define variable that would be set to the number of the result in the original API output. Note that due to the multithreaded nature of the SPARQL engine the results are not necessarily returned in the order in which they were received by the API, which may be useful for queries like search where result order is important. Using wikibase:apiOrdinal will allow to recover the original order. The object of the triple is currently ignored.

     ?num wikibase:apiOrdinal true.

Supported services[edit]

Currently the following services are supported:

Service Inputs Outputs Description
Generator generator, prop, pprop title, item, pageid, lastrevid, timestamp Call any generator API. Use "generator" parameter to specify, and specific generator parameters to further amend the search (see the example below).
Categories titles,cllimit category, title Get a list of categories on the page.
Search srsearch,srwhat,srlimit title Full-text search in wiki.
EntitySearch search,language,type,limit item,label Wikibase entity search, by title.

Required parameters are in bold. Please refer to the service documentation (linked in Service column) for the meaning of input parameters.

The full list of services can be seen in the production config file.


The service is configured by a JSON file listing particular API configurations and a list of endpoints. Please see the example configuration.

On the top level, there is a list of endpoint hostnames under "endpoints" and API configs under "services", with the keys being service names for use with wikibase:api.

Only endpoints listed in endpoints are allowed. The list can also contain prefixes - i.e. allows any hostname that ends with

Individual service configuration[edit]

The service configuration has two required elements - params for input parameters and output for output parameters. All the rest are currently ignored by the code, but @note and @docs are used to store description and link to documentation about particular API. It is safe to assume that config entries starting with @ will be always ignored by the code.


Input parameters are keyed by parameter name, and the value can be either constant - in which case the parameter is fixed, always present and its value can not be changed, or object, which contains description of variable parameter.

Variable parameter is bound by the query template or variable. The configuration specifies its type, which is currently ignored but may be enforced later. The configuration can also specify the default, which will be used in case the parameter is not specified or not bound. If the default is "",the parameter will be omitted in case it is not specified or not bound. If no default is specified, the parameter is treated as required and omitting it will result in an error.


Output is defined by items , which specifies the XPath for extracting specific result elements for the query, and set of output variables under vars. Each key in vars is variable name (to be used with wikibase:apiOutput) and the value is XPath to this particular value, calculated relative to the items path. Note that it is possible to also specify absolute paths for specific variables, but in that case you will get copies of that value for each result set.


Find all entities with labels "cheese" and get their types[edit]
  SERVICE wikibase:mwapi {
      bd:serviceParam wikibase:endpoint "";
                      wikibase:api "EntitySearch";
                      mwapi:search "cheese";
                      mwapi:language "en".
      ?item wikibase:apiOutputItem mwapi:item.
      ?num wikibase:apiOrdinal true.
  ?item (wdt:P279|wdt:P31) ?type

Try it!

Find articles in Wikipedia[edit]
  SERVICE wikibase:mwapi {
      bd:serviceParam wikibase:endpoint "";
                      wikibase:api "Search";
                      mwapi:srsearch "cheese".
      ?title wikibase:apiOutput mwapi:title.
} LIMIT 20

Try it!

Find articles in Wikipedia speaking about cheese and see which Wikibase items they correspond to[edit]
SELECT ?item ?type ?itemLabel ?typeLabel WHERE {
   SELECT ?item WHERE {
    SERVICE wikibase:mwapi {
      bd:serviceParam wikibase:endpoint "";
                      wikibase:api "Generator";
                      mwapi:generator "search";
                      mwapi:gsrsearch "cheese";
                      mwapi:gsrlimit "max".
      ?item wikibase:apiOutputItem mwapi:item .
  } LIMIT 100
 hint:Prior hint:runFirst "true".
 ?item wdt:P31|wdt:P279 ?type.
 SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
} LIMIT 100

Try it!

Find category members[edit]
  wd:Q6501349 wdt:P910 ?category .
  ?link schema:about ?category; schema:isPartOf <>; schema:name ?title .
  SERVICE wikibase:mwapi {
     # in
     bd:serviceParam wikibase:endpoint "";
                     wikibase:api "Generator";
                     mwapi:generator "categorymembers";
                     mwapi:gcmtitle ?title;
                     mwapi:gcmprop "ids|title|type";
                     mwapi:gcmlimit "max".
     # out
     ?member wikibase:apiOutput mwapi:title.
     ?ns wikibase:apiOutput "@ns".
     ?item wikibase:apiOutputItem mwapi:item.
# FILTER (?ns = "14")

Try it!