API:Edit

Requires the selected MediaWiki to have the following configuration settings set to :   and. See Manual:DefaultSettings.php.

Token
To edit a page, an edit token is required. This token is the same for all pages, but changes at every login. If you want to protect against edit conflicts (which is wise), you also need to get the timestamp of the last revision. You can obtain these as follows:

When passing this to the Edit API, always pass the token parameter last, or at least, after the text parameter.

Editing pages
Pages can be edited and created with.

Parameters

 * : Page to edit
 * : nth section to edit. Use 0 for the top section, 'new' for a new section. Omit if replacing the entire page
 * : (added in 1.19) Title to use if creating a new section. If not specified,  will be used instead
 * : New page (or section) content
 * : Edit token
 * : Edit comment
 * : If set to true, mark the edit as minor
 * : If set to true, 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 last revision, used to detect edit conflicts. Leave unset to ignore conflicts
 * : Timestamp when you obtained the page content and/or metadata. Used to detect page deletions and recreations. Leave unset and use  to ignore conflicts
 * : If set, suppress errors about the page having been deleted in the meantime and recreate it
 * : If set, throw an error if the page already exists
 * : If set, throw a  error if the page doesn't exist
 * : 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. If this parameter is set and the hashes don't match, the edit is rejected. This can be used to guard against data corruption
 * : CAPTCHA ID from the previous request
 * : Answer to the CAPTCHA
 * : Revision ID to undo. Overrides,   and
 * : Undo all revisions from  up to but not including this one. If not set, just undo one revision

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: 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 errors using either the format described above or the usual error format.

Possible errors
In addition to the usual stuff:
 * code: notitle
 * info: The title parameter must be set
 * code: notext
 * info: One of the text, appendtext, prependtext and undo parameters must be set
 * code: notoken
 * info: The token parameter must be set
 * code: invalidsection
 * info: The section parameter must be set to an integer or 'new'
 * code: protectedtitle
 * info: This title has been protected from creation
 * code: cantcreate
 * info: You don't have permission to create new pages
 * code: cantcreate-anon
 * info: Anonymous users can't create new pages
 * code: articleexists
 * info: The article you tried to create has been created already
 * code: noimageredirect-anon
 * info: Anonymous users can't create image redirects
 * code: noimageredirect
 * info: You don't have permission to create image redirects
 * code: spamdetected
 * info: Your edit was refused because it contained a spam fragment: ``wikitext ''
 * code: filtered
 * info: The filter callback function refused your edit
 * code: contenttoobig
 * info: The content you supplied exceeds the article size limit of bytes bytes
 * code: noedit-anon
 * info: Anonymous users can't edit pages
 * code: noedit
 * info: You don't have permission to edit pages
 * code: pagedeleted
 * info: The page has been deleted since you fetched its timestamp
 * code: emptypage
 * info: Creating new, empty pages is not allowed
 * code: emptynewsection
 * info: Creating empty new sections is not possible.
 * code: editconflict
 * info: Edit conflict detected
 * code: revwrongpage
 * info: rrevid is not a revision of ``pagename ''
 * Thrown if an invalid revid is given for  or
 * code: undo-failure
 * info: Undo failed due to conflicting intermediate edits
 * code: missingtitle
 * (see above nocreate parameter)

Editing via Ajax
Below is sample code for editing a page via an Ajax request:

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--