API:Extensions

This document covers creating an API module in an extension for use with MediaWiki 1.30 and later.

Module creation and registration
All API modules are subclasses of , but some types of modules use a derived base class. The method of registration also depends on the module type.


 * action modules
 * Modules that provide a value for the main  parameter should subclass .  They should be registered in   using the   key.


 * format modules
 * Modules that provide a value for the main   parameter should subclass . They should be registered in   using the   key.  It's very uncommon for an extension to need to add a format module.


 * query submodules
 * Modules that provide a value for the <tvar|1> </>, <tvar|2> </>, or <tvar|3> </> parameters to <tvar|4> </> should subclass <tvar|5></> (if not usable as a generator) or <tvar|6></> (if usable as a generator). They should be registered in <tvar|json> </> using the <tvar|APIPropModules> </>, <tvar|APIListModules> </>, or <tvar|APIMetaModules> </> key.

In all cases, the value for the registration key is an object with the module name (i.e. the value for the parameter) as the key and the class name as the value. Modules may also be registered conditionally using the <tvar|hook1></> (for action and format modules) and <tvar|hook2></> (for query submodules) hooks.

Prefix
In the constructor of your API module, when you call <tvar|code1> </> you can specify an optional prefix for your module's parameters.

(In the generated documentation for a module this prefix, if any, appears in parentheses in the heading for the module.)

If your module is a query submodule then a prefix is required, since a client can invoke multiple submodules each with its own parameters in a single request. For action and format modules, the prefix is optional.

Parameters
Most modules require parameters. These are defined by implementing <tvar|getAllowedParams></>. The return value is an associative array where keys are the (unprefixed) parameter names and values are either the scalar default value for the parameter or an array defining the properties of the parameter using the <tvar|param> </> constants defined by <tvar|ApiBase></>.

The example illustrates the syntax and some of the more common <tvar|param> </> constants.

Parameters are documented using MediaWiki's i18n mechanism. See doc>#Documentation</>|#Documentation for details.

Execution and output
The code actually implementing the module goes in the <tvar|1></> method.

This code will generally use <tvar|1></> to get the input parameters, and will use <tvar|2></> to get the <tvar|3></> object to add any output to.

Query prop submodules should use <tvar|1></> to access the set of pages to operate on.

Query submodules that can be used as generators will also need to implement <tvar|1></> which is passed an <tvar|2></> that should be filled with the generated pages.

In this case, the <tvar|1> </> should generally not be used.

Caching
By default API responses are marked as not cacheable <tvar|1>('Cache-Control: private')</>!

For action modules, you can allow caching by calling <tvar|1></>.

This still requires clients pass the <tvar|1> </> or <tvar|2> </> parameters to actually enable caching.

You can force caching by also calling <tvar|1></>.

For query modules, do not call those methods.

You can allow caching by instead implementing <tvar|1></>.

In either case, be sure that private data is not exposed.

Token handling
If your action module changes the wiki in any way, it should require a 1>Special:MyLanguage/API:Token</>|token of some kind.

To have this handled automatically, implement the <tvar|1> </> method, returning the token that your module requires (probably the <tvar|2> </> 3>Special:MyLanguage/Manual:Edit token</>|edit token).

The API base code will then automatically validate the token that clients provide in API requests in a <tvar|1> </> parameter.

If you don't want to use a token that is part of core, but rather a custom token with your own permission checks, use <tvar|1></> hook to register your token.

Master database access
If your module accesses the master database, it should implement the <tvar|1> </> method to return <tvar|2> </>.

Returning errors
includes several methods for performing various checks, for example,


 * If you need to assert that exactly one of a set of parameters was supplied, use <tvar|1></>.


 * If you need to assert that at most one of a set of parameters was supplied, use <tvar|1></>.


 * If you need to assert that at least one of a set of parameters was supplied, use <tvar|1></>.


 * If you need to assert that the user has certain rights, use <tvar|1></>.


 * If you need to assert that the user can take an action on a particular page, use <tvar|1></>.


 * If the user is blocked (and that matters to your module), pass the <tvar|1> </> object to <tvar|2></>.

But you will often run into cases where you need to raise an error of your own.

The usual way to do that is to call <tvar|1></>, although if you have a <tvar|2> </> with the error information you could pass it to <tvar|3></> instead.

If you need to issue a warning rather than an error, use <tvar|1></> or <tvar|2></> if it's a deprecation warning.

Documentation
The API is documented using MediaWiki's i18n mechanism. Needed messages generally have default names based on the module's "path". For action and format modules, the path is the same as the module's name used during registration. For query submodules, it's the name prefixed with "query+".

Every module will need a <tvar|1> </> message, which should be a one-line description of the module.

If additional help text is needed, <tvar|1> </> may be created as well.

Each parameter will need a <tvar|1> </> message, and parameters using <tvar|2> </> will also need a <tvar|3> </> for each value.

More details on API documentation are available at <tvar|1></>.

Extensions may also maintain extra API documentation on Wikimedia.

This should be located on the extension's main page or, if more space is required, on pages named <tvar|1> </> or subpages thereof (e.g. <tvar|2></>, <tvar|3></>, or <tvar|4></>).

The API namespace is reserved for the API of MediaWiki core.

Extending core modules
Since MediaWiki 1.14, it's possible to extend core modules' functionality using the following hooks:


 * - to add or modify the module's parameter list
 * - to add or modify the module's parameter descriptions
 * - to do something after the module has been executed (but before the result has been output)


 * Use <tvar|1></> for <tvar|2> </>, <tvar|3> </> and <tvar|4> </> modules


 * If the module is run in generator mode, <tvar|1></> will be called instead

List of extensions with API functionality
See <tvar|1></> for examples of extensions that add to or extend the API.

Testing your extension

 * Visit <tvar|1>[/api.php api.php]</> and navigate to the generated help for your module or query submodule. Your extension's help information should be correct.


 * The example URLs you provided in <tvar|1> </> should appear under "Examples", try clicking them.


 * Omit and mangle URL parameters in the query string, check your extension's response.


 * Visit <tvar|ext>Special:ApiSandbox</> and interactively explore your API.


 * Visit <tvar|api></> to see additional information about your extension.