Read this document
https://docs.moodle.org/dev/Web_services_API
A web service is defined by adding services.php to your plugin (as outlined in the link above)
Example of that can be found here
https://github.com/moodle/moodle/blob/master/lib/db/services.php
Use this for creating user
$userdata = [ 'auth' => 'manual', 'confirmed' => true, 'username' => 'foo', 'password' => 'foo', 'firstname' => 'foo', 'lastname' => 'faa', 'email' => 'some@email.com' ]; $userid = user_create_user($userdata);
For assigning capabilities use API assign_capability.