action=edit creates new pages and modifies existing ones. It is implemented in ApiEditPage, which wraps the EditPage class. All edit requests must be sent as HTTP POST and must include a valid CSRF token.
Required parameters
Title of the page to edit. Provide either
title or pageid, not both.Page ID of the page to edit. Alternative to
title.CSRF token obtained from
action=query&meta=tokens&type=csrf. The token is tied to the session; obtain a fresh token for each bot session, not each edit.Content parameters
At least one oftext, appendtext, prependtext, or undo must be provided (enforced by ApiEditPage::execute).
Full replacement text for the page. Replaces the entire content. Incompatible with
appendtext and prependtext.Text to append to the end of the page (or section). Does not require knowledge of the current content.
Text to prepend to the beginning of the page (or section).
Section identifier.
0 for the lead section, a positive integer for a numbered section, or new to append a new section. When section=new, sectiontitle sets the new section heading.Title for a new section. Only used when
section=new.Edit metadata
Edit summary displayed in the page history and recent changes. Strongly recommended for all edits. If omitted when creating a new section, the section title is used as the summary.
Mark the edit as minor. Only meaningful for logged-in users; the wiki may restrict who can set this flag.
Explicitly mark the edit as non-minor, overriding the user preference to default to minor edits.
Mark the edit as a bot edit, hiding it from recent changes for users who filter bot edits. Requires the
bot user right.Pipe-separated list of change tags to apply to the edit (e.g.,
tags=bot|automated). Tags must be defined in the wiki’s tag registry.Conflict detection
To detect edit conflicts — where another editor changed the page between your read and your write — pass timestamps obtained before your edit:Timestamp of the revision your edit is based on (from
rvprop=timestamp). If the page was edited after this timestamp, the API returns an edit-conflict error.Timestamp of when you fetched the page content (your local read time). Used together with
basetimestamp to detect file upload conflicts.Revision ID of the base revision, as an alternative to
basetimestamp.Creation control
Fail with
articleexists if the page already exists. Useful for ensuring you are creating, not overwriting.Fail with
missingtitle if the page does not already exist.Suppress the error that would otherwise occur when editing a page over an existing deletion log entry.
Undo
Revision ID to undo. The API reconstructs the edit made by that revision and applies the inverse change.
Revision ID to undo back to (inclusive). Undoes all revisions from
undo back to undoafter. Omit to undo only the single revision specified by undo.Content model
Serialization format of the
text parameter (e.g., text/x-wiki for wikitext, text/plain, application/json). Defaults to the page’s current content model format.Content model for the page (e.g.,
wikitext, json, css, javascript). Changing this also changes the content model of the page.Complete edit workflow
Authenticate
Log in using a bot password. See Authentication for the full login flow. Store the session cookies for subsequent requests.
Fetch a CSRF token
Obtain a token with Response:
action=query&meta=tokens. Reuse this token for the entire session; do not fetch a new one per edit.Fetch current content (optional but recommended)
For conflict-safe edits, retrieve the current revision and its timestamp before editing.Record
revisions[0].revid as your baserevid and revisions[0].timestamp as your basetimestamp.Code examples
Successful response
"Success" on success. Any other value indicates failure.Revision ID of the newly created revision.
Present and
true when the submitted text is identical to the current revision; no revision was created.Appending and prepending
Useappendtext or prependtext to add content without knowing the full current page text. These parameters are especially useful for leaving messages on talk pages.
Section editing
To edit a specific section, pass its index insection. Section 0 is always the lead section. Positive integers correspond to the section order in the wikitext.
Undo a revision
Error handling
| Error code | Cause |
|---|---|
badtoken | The CSRF token is invalid or expired |
edit-conflict | Another edit occurred between your read and write; fetch fresh content and retry |
articleexists | createonly=1 was set but the page already exists |
missingtitle | nocreate=1 was set but the page does not exist |
noedit | The page is protected and you lack the required permission |
noedit-anon | Anonymous editing is not allowed on this wiki or page |
protectedpage | The page is protected (see inprop=protection from prop=info) |
blocked | Your account is blocked from editing |
ratelimited | You have exceeded the edit rate limit; back off and retry |
