API:Main page



This is an overview of the "action" API. See the menu bar on the right for more detailed sub-topics and other APIs. ''

The MediaWiki action API is a web service that provides convenient access to wiki features, data, and meta-data over HTTP, via a URL usually at dev>Special:MyLanguage/Manual:Api.php|.

Clients request particular "actions" by specifying an  parameter, mainly   to get information.

If you are instead looking for an "internal API" or "PHP API", see the dev>Special:MyLanguage/Manual:Developing extensions|extension interface, which allows PHP developers to add new functionality to a MediaWiki installation.

The MediaWiki action API can be used to monitor a MediaWiki installation, or create a bot to automatically maintain one.

It provides direct, high-level access to the data contained in MediaWiki databases.

client>Special:MyLanguage/API:Client code|Client programs can log in to a wiki, get data, and post changes automatically by making HTTP requests to the web service.

Supported clients include bots, thin web-based JavaScript clients such as Navigation popups and LiveRC, end-user applications such as Vandal Fighter, and other web sites ([//tools.wmflabs.org Tool Labs]' utilities).

On new MediaWiki installations, the web service is enabled by default, but an administrator can disable>Special:MyLanguage/API:Restricting API usage#Disabling the entire API|disable it.

MediaWiki has two other outward-facing interfaces:

Read the Export help for more information.
 * The Special:Export page, which provides bulk export of wiki content as XML.


 * The standard web-based interface (which you are likely using right now to view this page). Arthur Thomas/me

https://en.wikipedia.org/w/api.php?action=query&titles=Main%20Page&prop=revisions&rvprop=content&format=json

Use any programming language to make an HTTP GET request for that URL (or just visit that link in your browser), and you'll get a JSON document which includes the current wiki markup for the page titled "Main Page".

Changing the format to  will return a "pretty-printed" HTML result good for debugging.

Arthur Thomas/me

This is the endpoint.

It's like the home page of the MediaWiki web service API.

This URL is the base URL for English Wikipedia's API, just as   is the base URL for its web site.

If you're writing a program to use English Wikipedia, every URL you construct will begin with this base URL.

If you're using a different MediaWiki installation, you'll need to find its endpoint and use that instead. All wm>Special:MyLanguage/Wikimedia</>|Wikimedia wikis have endpoints that follow this pattern:

Since <tvar|rev></>, we have RSD discovery for the endpoint: look for the  in the HTML source of any page and extract the   URL; the actual link contains additional info.

For instance, on this wiki it's:

Otherwise, there's no safe way to locate the endpoint on any wiki. Arthur Thomas/me

This tells the API that we want data to be returned in JSON format.

You might also want to try  to get an HTML version of the result that is good for debugging.

The API supports other formats>Special:MyLanguage/API:Data formats</>|output formats such as XML and [<tvar|url>http://php.net/manual/en/function.serialize.php</> native PHP], but there are plans to remove less popular formats (T95715), so you might not want to use them.

The action
The MediaWiki web service API implements dozens of actions and extensions implement many more; the dynamically generated [<tvar|url>https://en.wikipedia.org/w/api.php</> API help] documents all available actions on a wiki.

In this case, we're using the "query" action to get some information.

The "query" action is one of the API's most important actions, and it has query>Special:MyLanguage/API:Query|extensive documentation of its own</>. Arthur Thomas/me

The rest of the example URL contains parameters used by the "query" action.

Here, we're telling the web service API that we want information about the Wiki page called "Main Page".

(The %20 comes from percent-encoding a space.)

If you need to query multiple pages, put them all in one request to optimize network and server resources: <tvar|code> </>.

See the query>Special:MyLanguage/API:Query</>|query documentation for details.

You can request many kinds of information, or properties, about a page. This parameter tells the web service API that we want information about a particular revision of the page.

Since we're not specifying any revision information, the API will give us information about the latest revision &mdash; the main page of Wikipedia as it stands right now.

Finally, this parameter tells the web service API that we want the content of the latest revision of the page.

If we passed in <tvar|rvprop> </> instead, we'd get the latest page content and the name of the user who made the most recent revision.

Again, this is just one example.

Queries are explained in more detail query>Special:MyLanguage/API:Query</>|here, and [<tvar|url>https://en.wikipedia.org/w/api.php</> the API reference] lists all the possible actions, all the possible values for <tvar|rvprop> </>, and so on.



Arthur Thomas/me


 * Any policies that apply to the wiki you want to access, such as Wikimedia Foundation wikis' tou>wmf:Terms of Use</>|terms of use, tmp>wmf:trademark policy</>|trademark policy. These terms apply to you when you access or edit using the API, just as they do when you use your web browser.

Beyond that point, what you need to read depends on what you want to do. The right-hand menu links to detailed, task-specific documentation, and some more general guidelines are given below.

Arthur Thomas/me MyCoolTool/1.1 (https://example.org/MyCoolTool/; MyCoolTool@example.org) BasedOnSuperLib/1.4

On Wikimedia wikis, if you don't supply a  header, or you supply an empty or generic one, your request will fail with an HTTP 403 error (cf. <tvar|m-ua-policy>User-Agent policy</>). Other MediaWiki installations may have similar policies.

If you are calling the API from browser-based JavaScript, you won't be able to influence the  header: the browser will use its own. To work around this, use the  header:

In PHP, you can identify your user-agent with code such as this:

Or if you use cURL:

Arthur Thomas/me sandbox>Special:ApiSandbox</>|The API sandbox available on all Wikimedia wikis makes it easy to try out different actions interactively. [<tvar|api-refs>//en.wikipedia.org/w/api.php</> The API reference] contains automatically-generated descriptions of all actions and parameters. [<tvar|php-wp>https://www.ibm.com/developerworks/xml/library/x-phpwikipedia/index.html</> Hook into Wikipedia information using PHP and the MediaWiki API] (IBM developerWorks article, 17 May 2011) [<tvar|java-wp>http://www.integratingstuff.com/2012/04/06/hook-into-wikipedia-using-java-and-the-mediawiki-api/</> Hook into Wikipedia using Java and the MediaWiki API] (6 April 2012) tutor>API:Tutorial</>|The API tutorial leads you through hands-on exercises and includes a training video. Mailing list for notifications and questions: mail>mail:mediawiki-api</>|API mailing list Low-traffic mailing list for announcements only (all posts to this list are posted to mediawiki-api as well): announce>mail:mediawiki-api-announce</>|mediawiki-api-announce View and report API bugs in the [<tvar|bugs>https://phabricator.wikimedia.org/tag/mediawiki-api/</> MediaWiki-API Phabricator project] (When [<tvar|bugreport>https://phabricator.wikimedia.org/maniphest/task/create/?projects=MediaWiki-API</> reporting new bugs], don't forget to add MediaWiki-API to Projects) The current MediaWiki database schema