No need for an API. Just use synchronous Apex code instead.
Simply create and insert a User in a try and check for the right exception in the catch and always rollback the insert in the finally.
This class should do the job.
public class UserService {
public static Boolean existsName(String fullUserName) {
Boolean result = false;
User user = new User();
user.FirstName = ‘any’;
user.LastName = ‘any’;
user.Alias = ‘any’;
user.EMail = ‘any@email.com’;
user.Username = fullUserName;
user.CommunityNickname = fullUserName;
user.LocaleSidKey = ‘en_US’;
user.LanguageLocaleKey = ‘en_US’;
user.EmailEncodingKey = ‘UTF-8’;
user.TimeZoneSidKey = ‘America/Los_Angeles’;
user.ProfileId = [SELECT Id, Name FROM Profile WHERE Name=’Standard User’ limit 1].Id;
Savepoint sp = Database.setSavepoint();
try {
insert user;
}
catch(DmlException ex) {
if(ex.getDmlStatusCode(0) == StatusCode.DUPLICATE_USERNAME) {
result = true;
}
}
finally {
Database.rollback(sp);
}
return result;
}
}