API:FAQ

NOTE: This page is work in progress

Also read the main>Special:MyLanguage/API:Main page|quick start guide. It answers some questions not answered here and points to other useful pages.

get help?

 * 1) Read this FAQ
 * 2) Try to find the answer to your question in the main>Special:MyLanguage/API:Main page|API documentation here or on the self-documenting API home page
 * 3) If you can't find the answer to your question on the web,
 * 4) * you can ask your question on the mediawiki-api mailing list.
 * 1) * Create a Wikimedia account (if you don't have one already) and add a new topic on the API discussion page.


 * 1) * Ask on IRC in the channel on the Freenode network.

file a bug or a feature request?
If you have found a bug in the API or have a feature request, you can report it on Phabricator. Be sure to search for existing bugs first (please don't file duplicate bugs) and choose "MediaWiki" for product and "API" for component when reporting a new bug against the API. If the functionality you're requesting or reporting a bug against is offered by an extension (e.g. AbuseFilter, FlaggedRevs), file it in that extension's component in the "MediaWiki extensions" product.

call the API?
Send HTTP requests to api.php. For example, on the English Wikipedia, the URL is http://en.wikipedia.org/w/api.php. Most wikis have api.php at a similar URL: just use api.php in place of index.php in page actions. From 1.17 onwards, MediaWiki supports Really Simple Discovery; the HTML source of every page has an RSD link pointing to an RSD descriptor which indicates where to find the API. If you can't figure out the URL of api.php on a third-party (non-Wikimedia-operated) wiki, contact its owner. The wiki may not enable the MediaWiki API, see.

To play with the API


 * use Special:ApiSandbox


 * enable your browser's developer console and watch net requests to api.php as you interact with the wiki

control the output format?
Pass   in the query string. See the list of output formats for more information. Note that effort is underway to remove all output formats except JSON, so try to use JSON whenever possible.

check if an API module is available?
If you know which extension implements the API submodule, you can query the siteinfo meta information for  and look for its name in the returned list.

More directly, you can use  to request information about the API modules and submodules (such as  ) that you want to invoke. The  array in the response must contain a   key for each module and submodule, anything missing is not available.

Even if a module appears to be available, you must always handle API errors.

detect errors?
See Errors and warnings. An error response from the API will set the <tvar|mwapierror> </> HTTP header and return an <tvar|error> </> structure. For an example error response, visit <tvar|url>http://en.wikipedia.org/w/api.php?action=blah</>.

get the content of a page (wikitext)?
If you just want the raw wikitext without any other information whatsoever, it's best to use index.php's action=raw mode instead of the API: <tvar|url>http://en.wikipedia.org/w/index.php?action=raw&title=Main_Page</>. Note that this will output plain wikitext without any formatting. See also the action=raw documentation

To get more information about the page and its latest version, use the API: <tvar|url1>http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=Main_Page</>. See also the documentation for the prop=revisions module.

You can retrieve 50 pages per API request: <tvar|url2>http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&titles=Main_Page|Articles</>. This also works with generators.

get the content of a page (HTML)?
If you just want the HTML, it's best to use index.php's  mode instead of the API: <tvar|url1>https://en.wikipedia.org/wiki/Main_Page?action=render</>. See the action=render documentation.

With the advent of RESTBase, on Wikimedia wikis you can instead request the cached HTML of a page, for example <tvar|url999>https://rest.wikimedia.org/en.wikipedia.org/v1/page/html/Main_Page</>. Unlike  this returns a complete HTML document (i.e.    </tt>various metadata  ...  ); you could use an HTML parsing library to get the inner HTML of the   tag (see the documentation).

To get more information distilled from the wikitext at parse time (links, categories, sections, etc.), you can
 * Query the property submodules that provide the information you need (links, categories, etc.).
 * Use the  API module. <tvar|url2>https://en.wikipedia.org/w/api.php?action=parse&page=Main_Page</>.

do I get HTTP 403 errors?
This could mean you're not passing a <tvar|ua> </> HTTP header or that your <tvar|ua> </> is empty or blacklisted User-Agent policy. See the quick start guide for more information. Also, it could mean that you're passing <tvar|amp> </> in the query string of a GET request: Wikimedia blocks all such requests, use POST for them instead.

do I get the readapidenied error?
The wiki you're querying contains private content and requires users to log in in order to be able to read all pages. This means that a client needs to be logged in to query any information at all through the API. See the quick start guide for more information. It's not currently possible to query the contents of whitelisted pages without logging in, even though they're available in the regular user interface.

do I get badtoken errors?
This is usually because you're either not passing a token at all (read about tokens in the documentation of the module you're using) or because you're having trouble staying logged in. It's also possible you're reusing a type of token that can't be reused (see module documentation for details) or that you're using a token that's associated with an expired session. In general, when using cached tokens, refetch the token (generally using action=tokens) and try again before giving up.

do I get warnings instead of tokens (Action 'edit' is not allowed for the current user)?
You either don't have the right to execute the action you requested, or you're having trouble staying logged in.

is X not available through the API?
Not all features available in the user interface are available through the API. Such features weren't implemented either because no one has gotten around to it yet or because no one has requested them. For information about filing feature requests, see above.

does my API call on Wikimedia wikis just return an HTML error?
If you use API calls with POST requests make sure that these requests don't use Content-Type: multipart/form-data. This happens for instance if you use CURL to access the API and you pass your POST parameters as an array. The Squid proxy servers which are used at frontend servers at the Wikimedia wiki farm don't handle that correctly, thus an error is returned.

Instead, use the "value1=key1&value2=key2..." notation to pass the parameters as a string, similar to GET requests.

On other wikis which you access directly it doesn't make a difference.

In addition, some software (such as cURL) send an <tvar|header> </> header for longer POST requests (>1024 bytes). The wikimedia wikis that go through Squid servers can't cope with this. If you are still getting HTML errors with post requests, and are not logged in, try setting a blank Expect header (e.g. using cURL on the command line, use the option <tvar|arg> </>).

do really long API urls not work?
There is a maximum limit of the url size that can be used with the API when making GET requests. This limit varies depending on the website. Wikimedia's limit is roughly around 8100 characters. To get around this limit use POST requests instead (you may also need to set the Expect header, as described above)