clientlogin flow backed by AuthManager. Both approaches ultimately produce a session cookie that must be included with subsequent requests.
Authentication methods
Bot passwords
Purpose-built credentials for API bots. Scoped to specific permissions. Created at
Special:BotPasswords. Use with action=login.OAuth
Industry-standard delegated authorization. Suitable for third-party applications acting on behalf of users. Requires the OAuth extension.
clientlogin
Interactive multi-step login via AuthManager. Supports MFA, CAPTCHA, and external identity providers. Use for user-facing applications.
CSRF tokens
Not an authentication method itself, but required for all write operations. Obtained after authenticating via
action=query&meta=tokens.Bot passwords (recommended for bots)
Bot passwords are separate credentials tied to a main account. They support fine-grained permission scopes (e.g., allow only editing, not blocking users) and do not expose your main account password.Creating a bot password
Navigate to Special:BotPasswords
Go to
https://YOUR_WIKI/wiki/Special:BotPasswords while logged in to your main account.Enter a bot name
Choose a descriptive name for the bot application (e.g.,
my-archiving-bot). This becomes part of the login username: MainAccountName@BotName.Select permission grants
Check only the permissions your bot actually needs. Available grants include: edit existing pages, create new pages, upload files, delete pages, patrol changes, send email, and others.
Logging in with a bot password
Bot password authentication usesaction=login in a two-step process:
- Make an initial request without a token to obtain a login token.
- Submit credentials with the token.
action=login requires POST for both steps. The password and token parameters must be posted (enforced via requirePostedParameters in ApiLogin::execute).Login response
The outcome of the login attempt. Possible values:
Success, Failed, NeedToken, WrongToken, Aborted.The user ID of the authenticated account. Only present on
Success.The username of the authenticated account. Only present on
Success.Human-readable failure reason. Only present on
Failed or Aborted.action=login parameters
Username. For bot passwords, use the format
MainAccountName@BotName.Password (marked sensitive; must be POSTed).
Login token from
action=query&meta=tokens&type=login. Required in the second step.Authentication domain, for wikis using external authentication systems (LDAP, etc.).
clientlogin (interactive/AuthManager flow)
action=clientlogin implements the full AuthManager login protocol, which supports multi-factor authentication, CAPTCHAs, and external identity providers. It is implemented in ApiClientLogin and is the correct choice for user-facing applications.
clientlogin is a multi-step flow; the server may return UI or REDIRECT responses requesting additional data (e.g., a TOTP code or OAuth redirect).
returnurl parameter is required for redirect-based flows (e.g., OpenID Connect). For simple password-only logins it can be set to any valid URL.
On success:
continue=1 and the fields requested in requests.
CSRF tokens
Every write operation (edit, delete, move, protect, block, etc.) requires a CSRF token. The token is tied to the authenticated session and changes if the session is lost or the user logs out.Fetching a CSRF token
+\. For anonymous users (or users without a valid session), the token value is +\ (a LoggedOutEditToken) and will be rejected by write operations.
Available token types
FromApiQueryTokens::getTokenTypeSalts, these token types are built in:
| Type | Used by |
|---|---|
csrf | edit, delete, move, protect, block, upload, and all other write actions |
watch | action=watch |
patrol | action=patrol |
rollback | action=rollback |
userrights | action=userrights |
login | action=login (step 2) |
createaccount | action=createaccount |
Session handling
The API uses HTTP cookies for session state. When usingrequests in Python, the Session object handles cookie persistence automatically. When using curl, use -b cookies.txt -c cookies.txt for every request in the same session.
OAuth
For third-party applications acting on behalf of users, OAuth is the appropriate choice. The MediaWiki OAuth extension implements OAuth 1.0a and OAuth 2.0. OAuth requires registering a consumer application atSpecial:OAuthConsumerRegistration. Once approved, users can authorize your application through the standard OAuth redirect flow, and your application receives an access token for that user.
With OAuth, you do not use action=login. Instead, sign each API request with your OAuth credentials using an OAuth library.
action=query&meta=tokens&type=csrf — with the OAuth-signed request.
Logout
End the current session withaction=logout. This invalidates the session cookie and the CSRF token.
action=logout requires a CSRF token (not a login token). Fetch it before logging out if you have not already done so.Common authentication errors
| Error code | Cause |
|---|---|
Failed | Wrong username or password |
NeedToken | Login token was missing from the request |
WrongToken | Login token did not match the session |
Aborted | Login was aborted, typically because the wiki requires bot passwords and a main account login was attempted |
badtoken | CSRF token is invalid, stale, or belongs to a different session |
sessionlost | The session expired between requests |
notloggedin | A write operation was attempted without a valid session |
