0

I need to create an admin user using an API, but I'm not sure about the correct process or how to.

My Questions:

  1. What API endpoint and parameters should be used to create an admin user?
  2. How do I assign admin roles/permissions during user creation?
  3. Are there any security best practices to follow when creating admin users via API?
asked Feb 8 at 20:24

1 Answer 1

3

Magento 2 does not provide a direct REST API to create an admin user. Instead, you need to programmatically create an admin user via a custom module.

Create an Admin User Using a Custom API in a Custom Module

Since Magento does not expose a REST API for admin user creation, you need to create a custom API endpoint.

Steps to Create a Custom REST API for Admin User Creation:

1. Create a Custom Module

Create a module, e.g., Vendor/AdminUserApi.

2. Create registration.php

Create app/code/Vendor/AdminUserApi/registration.php:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
 \Magento\Framework\Component\ComponentRegistrar::MODULE,
 'Vendor_AdminUserApi',
 __DIR__
);

3. Create module.xml

Create app/code/Vendor/AdminUserApi/etc/module.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
 <module name="Vendor_AdminUserApi" setup_version="1.0.0"/>
</config>

4. Create webapi.xml

Define your REST API route in app/code/Vendor/AdminUserApi/etc/webapi.xml:

<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework/Webapi/etc/webapi.xsd">
 <route method="POST" url="/V1/custom/adminuser">
 <service class="Vendor\AdminUserApi\Api\AdminUserInterface" method="createAdminUser"/>
 <resources>
 <resource ref="Magento_Backend::admin"/>
 </resources>
 </route>
</routes>

5. Create API Interface

Create app/code/Vendor/AdminUserApi/Api/AdminUserInterface.php:

<?php
namespace Vendor\AdminUserApi\Api;
interface AdminUserInterface
{
 /**
 * Create Admin User
 * @param string $username
 * @param string $firstname
 * @param string $lastname
 * @param string $email
 * @param string $password
 * @param int $roleId
 * @return array
 */
 public function createAdminUser(
 $username,
 $firstname,
 $lastname,
 $email,
 $password,
 $roleId
 );
}

6. Create di.xml file on path Vendor/AdminUserApi/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
 <preference for="Vendor\AdminUserApi\Api\AdminUserInterface" type="Vendor\AdminUserApi\Model\AdminUser" />
</config>

7. Create Model Class

Create app/code/Vendor/AdminUserApi/Model/AdminUser.php:

<?php
namespace Vendor\AdminUserApi\Model;
use Magento\User\Model\UserFactory;
use Magento\User\Model\ResourceModel\User as UserResource;
use Vendor\AdminUserApi\Api\AdminUserInterface;
class AdminUser implements AdminUserInterface
{
 protected $userFactory;
 protected $userResource;
 public function __construct(UserFactory $userFactory, UserResource $userResource)
 {
 $this->userFactory = $userFactory;
 $this->userResource = $userResource;
 }
 public function createAdminUser($username, $firstname, $lastname, $email, $password, $roleId)
 {
 try {
 $user = $this->userFactory->create();
 $user->setUsername($username)
 ->setFirstname($firstname)
 ->setLastname($lastname)
 ->setEmail($email)
 ->setPassword($password)
 ->setRoleId($roleId)
 ->setIsActive(1);
 
 $this->userResource->save($user);
 return ["message" => "Admin user created successfully"];
 } catch (\Exception $e) {
 return ["error" => $e->getMessage()];
 }
 }
}

8. Run Commands

After creating the module, enable it:

bin/magento setup:upgrade
bin/magento cache:flush

9. Use the API

Make a POST request:

POST <your-magento-site>/rest/V1/custom/adminuser
Content-Type: application/json
Authorization: Bearer YOUR_ADMIN_TOKEN

Payload:

{
 "username": "newadmin",
 "firstname": "New",
 "lastname": "Admin",
 "email": "[email protected]",
 "password": "Admin@123",
 "roleId": 1
}

Success Response:

{
 "message": "Admin user created successfully"
}

Conclusion

Magento does not have a built-in REST API for admin user creation, but you can:

  1. Use the CLI (bin/magento admin:user:create) → Fastest method.
  2. Create a Custom REST API (as explained above).
answered Feb 10 at 5:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.