wikiHow to Use the MediaWiki API

The MediaWiki API is a well-documented set of development tools that can do a majority of tasks in one script. The API is typically used for development of automated tools for use in the wiki. The MediaWiki API can be easily used by most languages, particularly PHP (The curl functions) as well as Perl and a few others..


  1. Image titled Use the MediaWiki API Step 1
    Read the MediaWiki API documentation. It is located at 'api.php' in the same directory as the usual index.php.
  2. Image titled Use the MediaWiki API Step 2
    Select an output format. The MediaWiki offers data output in the form of JSON, JSONFM, PHP (serialized format), PHPFM, WDDX, WDDXFM, XML, XMLFM, YAML, YAMLFM, and RAWFM. The formats suffixed "FM" are pretty-print in HTML.
  3. Image titled Use the MediaWiki API Step 3
    Choose an action. The MediaWiki API supports the following actions:
    • help - See the help screen
    • login - Login to MediaWiki
    • opensearch - Implement the opensearch protocol. (Not all MediaWiki installations have this).
    • feedwatchlist - Return a watchlist feed.
    • query - Has a variety of options. See the MediaWiki API documentation for them.
  4. Image titled Use the MediaWiki API Step 4
    Select a query or a list. Besides the standard actions, MediaWiki supports different queries and lists, such as listing pages that contain a link to another article, number of recent changes diffs, contributions, etc.
  5. Image titled Use the MediaWiki API Step 5
    Pass the posted data to the API through the use of CURL or another tool that allows you to use the POST method to the script. The GET method may also work.
  6. Image titled Use the MediaWiki API Step 6
    Make sure to keep cookies sent to an application (Some scripts/applications may wish to store it in a subfolder.
  7. Image titled Use the MediaWiki API Step 7
    When interacting with the MediaWiki user interface, mind your edit token kept in most edit pages. The edit token is used to determine edit collisions and avoid overriding another edit already done (This is used in rollback as well.). Keep in mind it is easier for things to go awry if you are acting with the user interface of the wiki. Be mindful of any regexes you use to make sure that they will only do the intended result.
  8. Image titled Use the MediaWiki API Step 8
    Read the results sent by the wiki. The results will be sent to you in an HTTP response, which can be read by many parsers. Take extra care of the format you choose. Make sure you use the appropriate parser for that format. It is usually a good practice to explicitly state the format to MediaWiki even if it has a default format that you want to use.


  • Make sure you have your local wiki's support of the script before using the API.
  • You can find more about MediaWiki by using its API.


  • Try to obtain only the data you need and test it on a private wiki to minimize the risk of something going wrong on a public wiki -- it's always a good idea to review your code for possible bugs. Slow the request rate down to a point where you can look out for errors and correct them if necessary. Even at full automation, keep the request rate moderate so as to allow real humans priority access to the wiki.

Things You'll Need

  • Knowledge of a computer programming language (ex: C++, C, PHP, Perl, Java, or similar).
  • Knowledge of the specific code libraries of your language of choice that deals with sending and receiving HTTP requests and responses. Also code libraries that deal with parsing the output format of choice might be a necessity. For example, in PHP, much of this can be done with CURL, which is available as a PHP extension.
  • Knowledge of one of the output formats supported by the MediaWiki API, and how to convert it to a usable form. (e.g in PHP, unserialize() changes the "php" data format to an array)
  • General knowledge of the HTTP protocol and the internet communication architecture (sending POSTs vs GETs, and header responses)

Article Info

Categories: Wikis | Web Programming