Multi User Magento 2 - Adding and Modifying Users (restAPI)


Adding User accounts using the ReST API

Back to User Guides

It is possible for developers to use the ReST API to allow the creation, editing and deletion of user accounts from external sources without the need to have log in credentials as either a User or as an Administrator on the site.

To do this the developer will need session tokens for authentication. The documentation for the initial setup of the ReST API can be found by following this link.

If you have not yet installed the API  click here to find instructions to do so.


Affecting Master (Parent) account data

The following four Methods are used for working with Parent accounts via the API:

  • Get Customer Data - [ method GET ]
    • /rest/V1/parentaccount/{ID}
  • Create Parent Customer Account - [ method POST ]
    • /rest/V1/parentaccount/
  • Update Parent Customer Account By Id - [ method PUT ]
    • /rest/V1/parentaccount/{ID}
  • Delete Parent Customer Account By Id- [ method DELETE ]
    • /rest/V1/parentaccount/{ID}

For POST / PUT the body of the API call should look like this:

note: in PUT you can omit every parameter you don’t want to change

body: {
                "firstname": "Account",
                "lastname": "Lastname",
                "email": "team@cminds.com",
                "password":"Password123",
                "id": "5",
                "website_id": "1",
                "group_id": "1",
                "prefix": null,
                "middlename": null,,
                "suffix": null,
                "dob": "1973-03-19",
                "taxvat": null,
                "gender": "1",
                "is_active": "1",
                “can_manage_subaccounts”:”1”,
                "supplier_approve": "",
                "company": "",
                "city": "Calder",
                "country_id": "Calder",
                "region": "33",
                "postcode": "49628-7978",
                "telephone": "(555) 229-3320",
                "fax": "",
                "vat_id": "",
                "street_1": "6146 Honey Bluff Parkway",
                "street_2": ""
            }

Parameters details and options

  • firstname - The first name of the user of the account being created / modified.
  • lastname - The last name of the user of the account being created / modified.
  • email - The email address for the account.
  • password - The initial password to be assigned to the sub account being created.
  • id - The ID number of the user for cases of getting account information, is ignored for create and modify
  • website_id - This is the ID of the website. To obtain this go to System Manage Stores. Then hover over 'Main Store'.
  • group_id - The ID of the group to which the account belongs.
  • prefix - e.g. Mrs, Ms., Mr., etc
  • middlename - The middle name of the user
  • suffix - The suffix of the user
  • dob - The date of birth of the user being created
  • taxvat - The VAT number for the User account if applicable
  • gender - 0 = undefined; 1 = male; 2 = female
  • is_active - 0 = inactive; 1 = active
  • can_manage_subaccounts - 0 = cannot; 1 = can
  • company - The company associated with the user account
  • city - The city in which the user is registering or lives
  • country_id - The country code for the country in which the user account is registered
  • region - The region in which the user is registered
  • postcode - The post code for the account address
  • telephone - The telephone number of the user
  • fax - The fax number of the user
  • vat_id - The Vat ID of the user
  • street_1 - The first line of the street address of the user
  • street_2 - The second line of the street address of the user

NOTES:

The above is a list of available customer attributes for use with the API and some corresponding parameters or examples of parameters. 

For instances of Parent user account details being modified the API call must include the parent id then the body of the call needs only contain the attribute - parameters that are to be modified.

e.g. to change a user's name and their city you'd send the following call to  /rest/V1/parentaccount/{id of parent}

body: {
                "firstname": "Example",
                "lastname": "person",
                "city": "Dublin"
}

A GET response for the customer with this id with updated firstname, lastname and city attributes is then returned.


Converting Parent Account into Sub-user Account

  • Update Parent Customer Account By Id - [ method PUT ]
    • /rest/V1/parentaccount/{Parent ID}/subaccounts/{Sub-user ID}

When the Parent account is converted to a Sub-user account, they will need to be assigned a parent / master account.

The {Parent ID} above is the customer ID of the new parent / master account for the converted parent account.

The {Sub-user ID} is the customer ID of the parent being converted to a sub-user

The "parent_email":"demo@cminds.com" is the email address of the new parent account.

body: {
                "parent_email": "demo@email.com" 
            }

Affecting Subaccounts using the API

Working with Subaccounts using the API is similar to working with Masteraccounts with some slight differences. The API calls must include the Parent ID.

The following four Methods are used for working with Subaccounts via the API:

  • Get All Customer Sub Accounts Data - [ method GET ]
    • /rest/V1/parentaccount/{parentID} 
  • Get Customer Sub Account Data By Id - [ method GET ]
    • /rest/V1/parentaccount/{parentID}/subaccounts/{subID}
  • Create Sub Customer Account - [ method POST ]
    • /rest/V1/parentaccount/{parentID}/subaccounts/
  • Update Sub Customer Account By Id - [ method PUT ]
    • /rest/V1/parentaccount/{parentID}/subaccounts/{subID}
  • Delete Sub Customer Account By Id- [ method DELETE ]
    • /rest/V1/parentaccount/{parentID}/subaccounts/{subID}

For POST / PUT body of a API call should look like so:

body: {
                "firstname": "Account",
                "lastname": "LastName",
                "email": "team@cminds.com",
                "password":"Password123",
                "id": "5",
                "website_id": "1",
                "group_id": "1",
                "prefix": null,
                "middlename": null,
                "suffix": null,
                "dob": "1973-03-19",
                "taxvat": null,
                "gender": "1",
                "is_active": "1",
                “can_manage_subaccounts”:”1”,
                "supplier_approve": "",
                "company": "",
                "city": "Calder",
                "country_id": "Calder",
                "region": "33",
                "postcode": "49628-7978",
                "telephone": "(555) 229-3320",
                "fax": "",
                "vat_id": "",
                "street_1": "6146 Honey Bluff Parkway",
                "street_2": ""
}

Moving Sub-user Account from one Parent to another

The rest-API can be used to change the parent account of a sub-user account.

  • Changing parent account of sub-user account by Id - [ method PUT ]
    • /rest/V1/parentaccount/{Parent ID}/subaccounts/{Sub-user ID}

The {Parent ID} above is the customer ID of the new parent / master account for the sub-user account.

The {Sub-user ID} is the customer ID of the sub-user who is having a new parent account assigned.

The "parent_email":"demo@cminds.com" is the email address of the new parent account.

body: {
                "parent_email": "demo@email.com" 
            }

Promoting Sub-user Accounts to Parent account status

Sub-user accounts can be promoted to parent accounts quite easily through API calls.

  • Changing parent account of sub-user account by Id - [ method PUT ]
    • /rest/V1/parentaccount/{Parent ID}/subaccounts/{Sub-user ID}

Then use the parameter:-

body: {
                "promote": "1" 
            }


Get more information about the Multi User Account M2 Magento Extension

Find other Magento products at the CreativeMinds Magento Store

Let us know how we can Improve this Product Documentation Page

To open a Support Ticket visit our support center