API:Edit

Requires the selected MediaWiki to have the following configuration settings set to :   and. See Manual:DefaultSettings.php. This module is implemented by ApiEditPage.php.

Token
To edit a page, an edit token is required. This token is the same for all pages, but changes at every login. Edit tokens can be obtained using action=tokens for MediaWiki versions 1.20 and later. Alternatively, you can obtain an edit token using the following method. If you want to protect against edit conflicts (which is wise), you also need to get the timestamp of the last revision, included in the example below:

When passing this to the Edit API, always pass the token parameter last (or at least after the text parameter). That way, if the edit gets interrupted, the token won't be passed and the edit will fail.

When running on a MediaWiki page, the token can be obtained by calling  (more information).

Editing pages
Pages can be edited and created with.

Parameters

 * Some of these parameters seem to be revised in v1.20; for the current list, see ApiEditPage::getAllowedParams. To see the parameter list currently in use at Wikipedia, see: ApiEx


 * : Title of the page you want to edit. Cannot be used together with.
 * Note: If  is set to false, 'Article' and 'article' will not be considered as the same page, as in Wiktionary, for example.
 * : Page ID of the page you want to edit. Cannot be used together with
 * : Section number. 0 for the top section, 'new' for a new section. Omit to act on the entire page
 * : Title to use if creating a new section. If not specified,   will be used instead
 * : New page (or section) content
 * : Edit token. Especially if you are not using the  parameter, the token should be sent as the last parameter, or at least, after the text parameter, to prevent a bad edit from getting committed if transmission of the body is interrupted for some reason. This parameter is required.
 * : Edit summary. Also section title when section=new and sectiontitle is not set
 * : If set, mark the edit as minor
 * : If set, don't mark the edit as minor, even if you have the "Mark all my edits minor by default" preference enabled
 * : If set, mark the edit as bot; even if you are using a bot account the edits will not be marked unless you set this flag
 * : Timestamp of the base revision (obtained through prop=revisions&rvprop=timestamp). Used to detect edit conflicts; leave unset to ignore conflicts
 * : Timestamp when you started editing the page (e.g., when you fetched the current revision's text to begin editing it or checked the (non-)existence of the page). Used to detect edit conflicts; leave unset to ignore conflicts
 * : Override any errors about the article having been deleted in the meantime
 * : Don't edit the page if it exists already
 * : Throw an error if the page doesn't exist
 * : Add the page to your watchlist. Deprecated. Use the watchlist argument
 * : Remove the page from your watchlist. Deprecated. Use the watchlist argument
 * : Specify how the watchlist is affected by this edit, set to one of "watch", "unwatch", "preferences", "nochange":
 * : add the page to the watchlist
 * : remove the page from the watchlist
 * : use the preference settings (Default)
 * : don't change the watchlist
 * : MD5 hash (hex) of the  parameter or the   and   parameters concatenated. If this parameter is set and the hashes don't match, the edit is rejected. This can be used to guard against data corruption
 * : Add this text to the beginning of the page. Overrides text
 * : Add this text to the end of the page. Overrides text. Use section=new to append a new section
 * : Revision ID to undo. Overrides,   and
 * : Undo all revisions from  up to but not including this one. If not set, just undo one revision
 * : Automatically resolve redirects
 * : Content serialization format used for the input text
 * for wikitext
 * for javascript
 * for css
 * for plaintext
 * for json
 * : Content model of the new content
 * : CAPTCHA ID from the previous request
 * : Answer to the CAPTCHA
 * : CAPTCHA ID from the previous request
 * : Answer to the CAPTCHA
 * : CAPTCHA ID from the previous request
 * : Answer to the CAPTCHA
 * : CAPTCHA ID from the previous request
 * : Answer to the CAPTCHA
 * : Answer to the CAPTCHA

header of your request to. The token that you received is terminated with, this needs to be urlencoded (so it will end with  ) before it is passed back.

CAPTCHAs and extension errors
If an edit requires a CAPTCHA, you'll get something like: The specific information you get will depend on the captcha in use. In most cases, you won't get a math CAPTCHA, but a URL to an image (in the  field). When you've solved the CAPTCHA, retry the request (in this case with ).

Other extensions that use the APIEditBeforeSave hook may return failure results using either the format described above or the usual error format.

Possible errors
In addition to the usual stuff:

Editing via Ajax
Below is sample code for editing a page via an Ajax request: You can also use the mw.Api object:

Large texts
When using texts over 8000 characters, use "multipart/form-data" as it does not require escaping and will be significantly smaller than its urlencoded counterpart. Multipart has roughly 160 bytes of overhead for each parameter, so for seven parameters that's ~1.1kb overhead. Example: POST http://en.wikipedia.org/w/api.php HTTP/1.1 User-Agent: Bot Framework Content-Type: multipart/form-data; boundary=---8ce61ec834cf268 Host: en.wikipedia.org Cookie: Content-Length: 1348 Accept-Encoding: gzip Connection: Keep-Alive

-8ce61ec834cf268 Content-Disposition: form-data; name="action"

edit -8ce61ec834cf268 Content-Disposition: form-data; name="title" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit

Title here -8ce61ec834cf268 Content-Disposition: form-data; name="text" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit

Text here -8ce61ec834cf268 Content-Disposition: form-data; name="summary" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit

Summary -8ce61ec834cf268 Content-Disposition: form-data; name="token"

1f287ba00a908e9622045e7b18ffa352+\ -8ce61ec834cf268 Content-Disposition: form-data; name="assert"

user -8ce61ec834cf268 Content-Disposition: form-data; name="format"

xml -8ce61ec834cf268--

1

1

1