Skip to main content

MediaWiki API for Python 3

Project description

mwng (MediaWiki API NG) is a MediaWiki API library for Python 3.

MediaWiki API

We will use mw to call mwng in this document. import mwng as mw!

API = mw.API(api_php,[session])

Create an mw.API instance.

  • api_php: URL to the api.php, usually at $SITEROOT/w/api.php.
  • session: a requests.Session(), either supplied or create a new one by the library.


Login via botpassword.

  • username: the username supplied by Special:Botpasswords
  • botpassword: the passowrd supplied by Special:Botpasswords As of MediaWiki 1.27, using the main account for login is not supported. Obtain credentials via Special:BotPasswords or use clientlogin method. We have no plan to introduce clientlogin, so you have to use a botpassword, or do it yourself via raw API.get and requests.


Edit a page, by its ID or title.

  • page: either the page ID or the page title
  • content: either a dictionary or a string, if it is a string, replace the entire page with content.
  • token: (Optional) the csrf token, can be obtained via the first returned value of API.csrf(), or generated by the library itself.
  • timestamp: (Optional) a valid timestamp for detecting edit confident, can be obtained via the second returned value of API.csrf(). Not detecting edit confident ("now") by default.


Upload a file

  • file: the file to upload, can be a URL, local file path or a binary file object
  • filename: the target file name, without the File: prefix
  • comment: (Optional) the upload summary. For new files, also set the wikitext content to this
  • token: (Optional) the csrf token, can be obtained via the first returned value of API.csrf(), or generated by the library itself.


Watch or unwatch a page

  • pages: Array of page titles or string of a page title
  • expiry: (Optional) When the watch expires, can be relative ("1 month"), absolutive ("2014-09-18T12:34:56Z") or no expiry ("infinite", "indefinite", "infinity" or "never", this is the default option)
  • unwatch: (Optional) if true, unwatch instead of watch the pages, default to false
  • token: (Optional) the watch token, can be obtained via API.token("watch"), or generated by the library itself.


Get a action token.

  • type: The token type, can be one of these: createaccount, csrf, deleteglobalaccount, login, patrol, rollback, setglobalaccountstatus, userrights, watch


Get a login token (type: login).


Get a CSRF token (type: csrf).

API.get(body) and,[files])

Do RAW API requests, by GET or POST.

  • body: the request parameters
  • files: (Optional, POST only) the file to upload, for action = upload

Error handling


The father of all MediaWiki Error Class. The following data are given:

  • dump: the RAW JSON data, in case you have to do more things to the data
  • codes: List of error codes
  • message: The error description text (if there are only one), or human-readable list of error codes (if there are more than one)


Error during login. There are no error codes (an empty list), only dump and message are given.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mwng-0.0.7.tar.gz (15.1 kB view hashes)

Uploaded source

Built Distribution

mwng-0.0.7-py3-none-any.whl (16.3 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page