Create an admin user via REST API

I’m looking into a multisite setup, whereby I can completely automate the process of creating a new site when someone signs up. Creating the new site is easy: it’s just a case of running a script that creates a folder for the new site content and copies some default content files into it.

I’dlike to automate the creation of the new site’s admin and editor user with the REST API (the admin is me, the editor is the end user), which would then send an email to the user with their account credentials. But because the REST API uses username and password for HTTP authentication it doesn’t seem possible to create the admin user, because a user doesn’t yet exist.

Is there any way I can achieve this, or is it not possible to use the REST API until an admin user exists?

Yes, I think you either need an existing user or a session (when using api/system/install).

How about creating the user via a PHP script instead?

I am doing something similar and create my admin user with this script called from the shell, once the site is set up:

<?php
// createUser.php to create a kirby panel admin user
// Usage: php createUser.php DOCROOT EMAIL NAME LANG PASSWORD
// Example: php createUser.php '/srv/www/vhost/kirby-test/htdocs' 'adspectus@fastmail.com' 'Uwe Gehring' 'en' 'secret'

require $argv[1] . '/kirby/bootstrap.php';

$kirby = new Kirby([ 'roots' => [ 'index' => $argv[1] ] ]);

// authenticate as almighty
$kirby->impersonate('kirby');

// create new K3 user account
$kirby->users()->create(['role' => 'admin','email' => $argv[2],'name' => $argv[3],'language' => $argv[4],'password' => $argv[5]]);

?>
2 Likes

I just tested creating a first panel user via the API

config:

  'api' => [
        'allowInsecure' => true,
        'basicAuth' => true
    ],

route:

api/system/install

basic auth user: kirby (no password)

json-encoded data

email, role, password

Once that first user is installed, you should be able to authenticate with the admin user to create other users.

1 Like

Thanks both, that’s super helpful! I will try both those approaches and see which works best for me.