You'll need to create a web service plugin
The developer documentation is here
https://moodledev.io/docs/apis/subsystems/external/writing-a-service
To get you started, see the Moodle functions in /user/externallib.php
There are 3 existing Moodle functions
get_users_by_field
- the main code
get_users_by_field_parameters
- incoming parameters
get_users_by_field_returns
- return parameters
Then create a local plugin for your new web service
https://moodledev.io/docs/apis/plugintypes/local
At a minimum, you will need the following files
/local/mypluginname/version.php
/local/mypluginname/lang/en/local_mypluginname.php
A services file to tell Moodle where your web service code is
/local/mypluginname/db/services.php
And your web service code in a class
/local/mypluginname/classes/external/get_user_by_custom_field.php
The class will need 3 functions
execute
- the main code
execute_parameters
- incoming parameters
execute_return
- return parameters
See https://moodledev.io/docs/apis/subsystems/external/writing-a-service for further details