Creating Strapi admin users via API

Hey @skery,
If you want to create your own controller logic and create admins by yourself:

You could simply make use of:

const adminUserData = {
      username:,
      password: ,
      firstname:,
      lastname: ,
      email: ,
      roles: [],
      blocked: false,
      isActive: true,
}
    let superAdminRole = await strapi.db.query("admin::role").findOne({
      select: [],
      where: { code: "strapi-super-admin" },
      orderBy: {},
      populate: {},
    });

[superAdminRole.id] is what you need to use as adminUserData.roles-Value.

To create a hased password…

password = await strapi.service("admin::auth").hashPassword(unhasedPasswordString);

Set this as adminUserData.password.

And finally create the adminUser from adminUserData.

await strapi.db.query("admin::user").create({ data: { ...adminUserData } });

Note: I quickly copy & pasted code-blocks of my strapi-plugin-init-admin-user - if these code-snippets are not helpful enough: I invite you to take look into it’s code.

3 Likes