Creating Strapi admin users via API

System Information
  • Strapi Version: v4.2.3
  • Operating System: MacOS
  • Database: sqlite
  • Node Version: v17.7.2
  • NPM Version:
  • Yarn Version:

I’d like my Strapi back-office users to be able to register through a form, to then be able to login to the Strapi admin panel as Authors (after they are validated by a super admin). My problem is I can’t find any documentation about an API endpoint allowing me to create new admin users.

When I create a user via the admin panel, I can see it’s hitting the /api/users endpoint, but I don’t know how to send my POST request to make it work (I have a “Missing or invalid credentials” right now). Is it even possible to create admin users through the API ?

Thank you

Hello and welcome :wave: to the community.
I think that if you need Admin users you need to hit up admin/register etc :slightly_smiling_face:

I can’t remember the exact endpoint but I think that there was some guide on discord or on the forums which listed it.

I think it’s admin/local/register to register admin accounts.
If not you can always just create your OWN endpoint, and register admin users that way.

api/users is used for API users not for admin credentials iirc.

Thank you Eventyret for the answer and for the welcoming.

After further research, it seems that the API endpoint exists: /admin/register
My concern now is that is I don’t know what to provide. It seems the endpoint is expecting a newUser object with firstname and password, which is ok. But it also needs a registrationToken that I don’t know how to generate. When I try some random string, I get an error with “Invalid registration info” message.

I tried to find the guide you’re talking about on discord and the forum, with no success

If there is no documentation about it then it seems it was aimed to not be reached by anyone but the Strapi itself, in this case you will require to learn it by yourself.

Seems the url you want to hit is /admin/register-admin
To register new admin users from

1 Like

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.

4 Likes

Hey @minzig,

Thank you for the hint, I’m indeed able to create new admin users programmatically with this code through a custom API endpoint controller. I’ll go deeper into strapi-plugin-init-admin-user to see how it works.

Thank you !

I was stuck into this same problem, here is the final code if someone is looking for it

src/api/admin/routes/custom.js

module.exports = {
  routes: [
    {
      method: 'POST',
      path: '/create-author',
      handler: async (ctx) => {
        try {
          const { firstname, lastname, email, password } = ctx.request.body;
          if (!firstname || !lastname || !email || !password) {
            // ctx.badRequest(message, details)
            return ctx.badRequest(`firstname, lastname, email and password are required fields`);
          }

          const user = await strapi.db.query("admin::user").findOne({ where: { email: email } });
          if (user) {
            strapi.log.error(`Couldn't create author: ${email} already exists`);
            return ctx.badRequest(`${email} already exists`)
          }

          const hashedPassword = await strapi.service("admin::auth").hashPassword(password);
          const authorRole = await strapi.db.query("admin::role").findOne({ where: { code: 'strapi-author' } })
          const adminUserData = {
            firstname,
            lastname,
            email,
            password: hashedPassword,
            roles: [authorRole],
            blocked: false,
            isActive: true,
          }

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

          strapi.log.info(`Created author: ${firstname} ${lastname} (${email})`);
          return ctx.send({ message: 'Author created successfully!', details: response }, 200);
        } catch (err) {
          return ctx.internalServerError(err.message)
        }
      },
      config: {
        auth: false
      }
    }
  ]
}
2 Likes

I want to redirect ‘/admin’ route to ‘/admin/content-manager’ when go to ‘/admin’ route

Kind of default redirecting route?