API:Account creation

From MediaWiki.org
Jump to navigation Jump to search

Other languages:
English • ‎dansk • ‎español • ‎français • ‎italiano • ‎polski • ‎português • ‎română • ‎български • ‎русский • ‎العربية • ‎ไทย • ‎中文 • ‎日本語 • ‎한국어
MediaWiki version: 1.27
This page documents the account creation API as of MediaWiki 1.27. Documentation of the API as it existed in earlier versions is available here: Api:Account creation/pre-1.27 .

API documentation[edit]




action=createaccount (create)

(main | createaccount)
  • This module requires write rights.
  • This module only accepts POST requests.
  • Source: MediaWiki
  • License: GPL-2.0-or-later

Create a new user account.

The general procedure to use this module is:

  1. Fetch the fields available from action=query&meta=authmanagerinfo with amirequestsfor=create, and a createaccount token from action=query&meta=tokens.
  2. Present the fields to the user, and obtain their submission.
  3. Post to this module, supplying createreturnurl and any relevant fields.
  4. Check the status in the response.
    • If you received PASS or FAIL, you're done. The operation either succeeded or it didn't.
    • If you received UI, present the new fields to the user and obtain their submission. Then post to this module with createcontinue and the relevant fields set, and repeat step 4.
    • If you received REDIRECT, direct the user to the redirecttarget and wait for the return to createreturnurl. Then post to this module with createcontinue and any fields passed to the return URL, and repeat step 4.
    • If you received RESTART, that means the authentication worked but we don't have a linked user account. You might treat this as UI or as FAIL.
Parameters:
createrequests

Only use these authentication requests, by the id returned from action=query&meta=authmanagerinfo with amirequestsfor=create or from a previous response from this module.

Separate values with | or alternative. Maximum number of values is 50 (500 for bots).
createmessageformat

Format to use for returning messages.

One of the following values: html, wikitext, raw, none
Default: wikitext
createmergerequestfields

Merge field information for all authentication requests into one array.

Type: boolean (details)
createpreservestate

Preserve state from a previous failed login attempt, if possible.

If action=query&meta=authmanagerinfo returned true for hasprimarypreservedstate, requests marked as primary-required should be omitted. If it returned a non-empty value for preservedusername, that username must be used for the username parameter.

Type: boolean (details)
createreturnurl

Return URL for third-party authentication flows, must be absolute. Either this or createcontinue is required.

Upon receiving a REDIRECT response, you will typically open a browser or web view to the specified redirecttarget URL for a third-party authentication flow. When that completes, the third party will send the browser or web view to this URL. You should extract any query or POST parameters from the URL and pass them as a createcontinue request to this API module.

createcontinue

This request is a continuation after an earlier UI or REDIRECT response. Either this or createreturnurl is required.

Type: boolean (details)
createtoken

A "createaccount" token retrieved from action=query&meta=tokens

This parameter is required.
*
This module accepts additional parameters depending on the available authentication requests. Use action=query&meta=authmanagerinfo with amirequestsfor=create (or a previous response from this module, if applicable) to determine the requests available and the fields that they use.

Creating an account[edit]

The process has three general steps:

  1. Fetch the fields from API:Authmanagerinfo and the token from API:Tokens.
  2. Send a POST request with the fetched token, user information and other fields, and return URL to the API.
  3. Deal with the response, which might involve further POST requests to supply more information.

Example 1: Process on a wiki without special authentication extensions[edit]

A wiki without special authentication extensions can be rather straightforward. If your code knows which fields will be required, it might skip the call to API:Authmanagerinfo and just assume which fields will be needed (i.e. username, password & retyped password, email, possibly realname).

Note: If you're creating an account for someone else, you'll need to specify a reason for the same by including a reason parameter to the POST request. You could also use mailpassword in place of password and retype parameters to have MediaWiki send the new user a temporary password via email.

POST Request[edit]

Response[edit]

{
    "createaccount": {
        "status": "PASS",
        "username": "Zane"
    }
}

Sample code[edit]

create_account.py

#!/usr/bin/python3

"""
    create_account.py

    MediaWiki Action API Code Samples
    Demo of `createaccount` module: Create an account on a wiki without the 
    special authentication extensions

    MIT license
"""

import requests

S = requests.Session()

WIKI_URL = "https://test.wikipedia.org"
API_ENDPOINT = WIKI_URL + "/w/api.php"

# First step
# Retrieve account creation token from `tokens` module

PARAMS_0 = {
    'action':"query",
    'meta':"tokens",
    'type':"createaccount",
    'format':"json"
}

R = S.get(url=API_ENDPOINT, params=PARAMS_0)
DATA = R.json()

TOKEN = DATA['query']['tokens']['createaccounttoken']

# Second step
# Send a post request with the fetched token and other data (user information,
# return URL, etc.)  to the API to create an account

PARAMS_1 = {
    'action': "createaccount",
    'createtoken': TOKEN,
    'username': 'your_username',
    'password': 'your_password',
    'retype': 'retype_your_password',
    'createreturnurl': WIKI_URL,
    'format': "json"
}

R = S.post(API_ENDPOINT, data=PARAMS_1)
DATA = R.json()

print(DATA)

Example 2: Process on a wiki with a CAPTCHA extension[edit]

Note the first step below could, if you'd rather, be done as two steps: one to fetch the fields available from API:Authmanagerinfo and another to fetch the token from API:Tokens.

First step: Fetch fields available from API:Authmanagerinfo and token from API:Tokens[edit]

Second step: Send a post request along with a create account token, user information and return URL[edit]

Sample Code[edit]

Note this code sample separates the API:Authmanagerinfo and API:Tokens requests, and generally assumes there will be a CAPTCHA and no other complications.

Example 3: Account creation on a wiki with a CAPTCHA, an OpenID extension, and a two-factor authentication extension enabled[edit]

First step: Fetch fields available from API:Authmanagerinfo and token from API:Tokens[edit]

The fetching of API:Authmanagerinfo and API:Tokens is largely the same as in the previous example, and so is not repeated here. The list of requests returned by API:Authmanagerinfo will include definitions for both the CAPTCHA extension and the OpenID extension.

Second step: Answer the CAPTCHA and select OpenID authentication.[edit]

The client would be expected to redirect the user's browser to the provided redirecttarget.

The OpenID provider would authenticate, and redirect to Special:OpenIDConnectReturn on the wiki, which would validate the OpenID response and then redirect to the createreturnurl provided in the first POST to the API with the code and state parameters added.

The client gets control of the process back at this point and makes its next API request.

Third step: Back from OpenID.[edit]

The client posts the code and state back to the API. The API's response has the two-factor authentication extension prompting the user to set up their second factor.

Now the client would prompt the user to set up a new account in their two-factor authentication app and enter the current code, or allow the user to skip 2FA setup. Let's assume the user does set up 2FA.

Fourth step: Set up two-factor authentication.[edit]

The account creation has finally succeeded.

If at any point account creation fails, a response with status FAIL will be returned, along with a message to display to the user.

Possible errors[edit]

Code Info
badtoken Invalid create account token
notoken The token parameter must be set.
mustpostparams The following parameter was found in the query string, but must be in the POST body: createtoken.
missingparam At least one of the parameters "createcontinue" and "createreturnurl" is required.
authmanager-create-no-primary The supplied credentials could not be used for account creation.
invalidemailaddress The email address cannot be accepted as it appears to have an invalid format.

Please enter a well-formatted address or empty that field.

badretype The passwords you entered do not match.
userexists Username entered already in use.

Please choose a different name.

captcha-createaccount-fail Incorrect or missing CAPTCHA.
acct_creation_throttle_hit Visitors to this wiki using your IP address have created num accounts in the last day, which is the maximum allowed in this time period.

As a result, visitors using this IP address cannot create any more accounts at the moment.

If you are at an event where contributing to Wikimedia projects is the focus, please see Requesting temporary lift of IP cap to help resolve this issue.

Additional notes[edit]

If you're logged in, your username will also be recorded when creating an account.

  • While executing the code snippets provided on this page, remember:
    • Once an account on a wiki is created, it cannot be deleted.
    • Always use https://test.wikipedia.org/w/api.php as the endpoint, so that you don't accidentally create accounts on production wikis.
  • MediaWiki site administrators and extension developers can disable this API feature by inserting the following line in the configuration file:
$wgAPIModules['createaccount'] = 'ApiDisabled';

See also[edit]