API:Tutorial

In this tutorial, you will learn about the MediaWiki Action API, how to use it, request format, a simple query, and examples of projects using Action API.

What is the MediaWiki Action API?
API:Main page is a good starting point for understanding the API. In a nutshell, MediaWiki Action API is a web service. There are many types of web services, and Action API’s type is REST, which is a more flexible and scalable protocol and mostly uses HTTP requests.

You can let your program talk to the API and get access to wiki features such as logging in to a wiki, creating and editing a page, searching for a title or text, parsing content, etc. This API is used by extension developers, wiki site administrators, and third-party developers.

On the right sidebar of this page, there are so many features listed in different categories that are supported by the API. Before exploring any of these features, read through the overview pages such as data formats, errors and warnings, the Action API FAQ, etc. listed on API:Main page to get an idea of the capabilities of API.

How to use it
You can request for data from client slide to the server over HTTP and get the response back in a standard format. A request consists of an endpoint and a set of parameters. There are two types of request that can be made: GET And POST. In case of a GET request, parameter would be a query string and in case of POST request, parameter would be data in JSON format.

Request format
All Wikimedia Wikis have endpoints in this format. To operate on MediaWiki or English Wikipedia, use the related endpoint. For testing, for example, to make a test edit or create an account, use the test wiki endpoint in your queries.

In the query string, one of the parameters would be. As the name is; it tells the API which action to perform. One of the most popular action is query (action=query) that allows fetching data from a wiki. Following the action parameter, the next could be from one of the three query module types listed below:


 * prop: get properties of pages


 * list: get list of pages matching a certain criterion


 * meta: get meta information about the wiki and user

Finally, include the  parameter telling the API in which format to get the results. Though the MediaWiki API has supported different parameters in the past, the most recommended format is JSON; always use JSON.

A simple query
Let’s take an example of a simple query to understand what a typical GET Request and Response would look like in the context of Action API.

Let's search for a title in a wiki! Doing so would require the use of the  module documented on API:Search.

GET Request
To search for page titles matching Craig Noone GET Request would be:It will contain:


 * endpoint which is of English Wikipedia in this case
 * to fetch data from wiki
 * to get list of pages matching a criteria
 * along with a page title to search for
 * recommended format for output

Response
As a result, you will get a list of pages titles that match Craig Noone in JSON format:

Examples of projects using Action API
Some example of projects in Wikimedia that use Action API:


 * Wikipedia IOS App, in which one of the features is to search for wiki pages near your location.


 * Bots that operate on a wiki. Pywikibot is a bot framework written in Python that interfaces with MediaWiki API which a lot of bot developers use.


 * API use in visualizing data obtained from a wiki. Such as this neat visualization that shows real time edits made on Wikipedia.

Additional notes

 * Several sites are using MediaWiki. They might all be running a different version of MediaWiki and in turn the API. Some module parameters supported in one version might be deprecated or removed in others. You could learn about that in each module's documentation via API help module.
 * If you want to make a lot of API calls, and perhaps run very busy and active bots, talk to wiki admins ahead of time, so they do not block you. See list of Administrators of Wikimedia projects. Read more about this topic on API:Etiquette.
 * All data modifying actions such as logging, editing or moving a page, and watching or patrolling changes would require tokens. You would need to obtain tokens via API:Tokens to proceed further.

Resources

 * Use Special:APISandbox to experiment with the Action API. You can play around with supported actions, specify a value for parameters, and send requests to the API to see the results on the same page.
 * Wikimedia dumps so you can work with them locally.
 * Read through tutorials: An Introduction to API's and What is REST — A Simple Explanation for Beginners.
 * MediaWiki API help provides auto-generated API documentation of all supported modules.
 * API:Main page provides an overview of the Action API.
 * Introduction to the MediaWiki API by User:Roan_Kattouw_(WMF) at the San Francisco Hackathon, 2012. This tutorial guide goes along with the video.