Add User
Purpose
To add a user to your organization.
Endpoints
Request Details
Request URL
{api-domain}/crm/v2/users
Header
Authorization: Zoho-oauthtoken 100xx.d92d4xxxxxxxxxxxxx15f52
Scope
scope=ZohoCRM.users.{operation_type}
Possible operation types
ALL - Full access to users
CREATE - To create users
You can add only one user per POST request.
You must use only Field API names in the input. You can obtain the field API names from Fields metadata API (the value for the key “api_name” for every field).
Sample Request
Copiedcurl "https://zylkercorp.zohoplatform.com/crm/v2/users"
-H "Authorization: Zoho-oauthtoken 100xx.d92d4xxxxxxxxxxxxx15f52"
-d "@newuser.json"
-X POST
Copied//Get instance of UsersOperations Class
let usersOperations = new ZCRM.User.Operations();
//Get instance of RequestWrapper Class that will contain the request body
let request = new ZCRM.User.Model.RequestWrapper();
//Array to hold User instances
let userArray = [];
//Get instance of User Class
let user = new ZCRM.User.Model.User();
//Get instance of Role Class
let role = new ZCRM.Role.Model.Role();
//Set ID to Role instance
role.setId(347706126008n);
//Set Role instance to role in User
user.setRole(role);
user.setCountryLocale("en_US");
user.setFirstName("Test");
user.setLastName("User");
user.setEmail("testuser123@zylkercorp.com");
//Get instance of Profile Class
let profile = new ZCRM.Profile.Model.Profile();
profile.setId(347706126014n);
//Set profile instance to profile in User instance
user.setProfile(profile);
//Add the User instance to array
userArray.push(user);
//Set the array to users in RequestWrapper instance
request.setUsers(userArray);
//Call createUser method that takes RequestWrapper class instance as parameter
let response = await usersOperations.createUser(request);
Copiedvar listener = 0;
class AddUser {
async insertUser() {
var url = "https://zylkercorp.zohoplatform.com/crm/v2/users"
var parameters = new Map()
var headers = new Map()
var token = {
clientId:"1000.NPY9M1V0XXXXXXXXXXXXXXXXXXXF7H",
redirectUrl:"http://127.0.0.1:5500/redirect.html",
scope:"ZohoCRM.users.ALL,ZohoCRM.bulk.read,ZohoCRM.modules.ALL,ZohoCRM.settings.ALL,Aaaserver.profile.Read,ZohoCRM.org.ALL,profile.userphoto.READ,ZohoFiles.files.ALL,ZohoCRM.bulk.ALL,ZohoCRM.settings.variable_groups.ALL"
}
var accesstoken = await new AddUser().getToken(token)
headers.set("Authorization", "Zoho-oauthtoken " + accesstoken)
var requestMethod = "POST"
var reqBody = {"users":[
{
"email": "Patricsdia@zylkercorp.com",
"first_name":"Patricia",
"role": "352403326005",
"profile": "352403326011",
"last_name": "Boyle"
}
]}
var params = "";
parameters.forEach(function(value, key) {
if (parameters.has(key)) {
if (params) {
params = params + key + '=' + value + '&';
}
else {
params = key + '=' + value + '&';
}
}
});
var apiHeaders = {};
if(headers) {
headers.forEach(function(value, key) {
apiHeaders[key] = value;
});
}
if (params.length > 0){
url = url + '?' + params.substring(0, params.length - 1);
}
var requestObj = {
uri : url,
method : requestMethod,
headers : apiHeaders,
body : JSON.stringify(reqBody),
encoding: "utf8",
allowGetBody : true,
throwHttpErrors : false
};
var result = await new AddUser().makeAPICall(requestObj);
console.log(result.status)
console.log(result.response)
}
async getToken(token) {
if(listener == 0) {
window.addEventListener("storage", function(reponse) {
if(reponse.key === "access_token" && (reponse.oldValue != reponse.newValue || reponse.oldValue == null)){
location.reload();
}
if(reponse.key === "access_token"){
sessionStorage.removeItem("__auth_process");
}
}, false);
listener = 1;
if(sessionStorage.getItem("__auth_process")) {
sessionStorage.removeItem("__auth_process");
}
}
["granted_for_session", "access_token","expires_in","expires_in_sec","location","api_domain","state","__token_init","__auth_process"].forEach(function (k) {
var isKeyExists = localStorage.hasOwnProperty(k);
if(isKeyExists) {
sessionStorage.setItem(k, localStorage[k]);
}
localStorage.removeItem(k);
});
var valueInStore = sessionStorage.getItem("access_token");
var tokenInit = sessionStorage.getItem("__token_init");
if(tokenInit != null && valueInStore != null && Date.now() >= parseInt(tokenInit) + 59 * 60 * 1000){ // check after 59th minute
valueInStore = null;
sessionStorage.removeItem("access_token");
}
var auth_process = sessionStorage.getItem("__auth_process");
if ((valueInStore == null && auth_process == null) || (valueInStore == 'undefined' && (auth_process == null || auth_process == "true"))) {
var accountsUrl = "https://zylkercorp.zohoplatform.com/oauth/v2/auth"
var clientId;
var scope;
var redirectUrl;
if(token != null) {
clientId = token.clientId;
scope = token.scope;
redirectUrl = token.redirectUrl;
}
var fullGrant = sessionStorage.getItem("full_grant");
var grantedForSession = sessionStorage.getItem("granted_for_session");
if(sessionStorage.getItem("__token_init") != null && ((fullGrant != null && "true" == full_grant) || (grantedForSession != null && "true" == grantedForSession))) {
accountsUrl += '/refresh';
}
if (clientId && scope) {
sessionStorage.setItem("__token_init", Date.now());
sessionStorage.removeItem("access_token");
sessionStorage.setItem("__auth_process", "true");
window.open(accountsUrl + "?" + "scope" + "=" + scope + "&"+ "client_id" +"=" + clientId + "&response_type=token&state=zohocrmclient&redirect_uri=" + redirectUrl);
["granted_for_session", "access_token","expires_in","expires_in_sec","location","api_domain","state","__token_init","__auth_process"].forEach(function (k) {
var isKeyExists = localStorage.hasOwnProperty(k);
if(isKeyExists){
sessionStorage.setItem(k, localStorage[k]);
}
localStorage.removeItem(k);
});
valueInStore = sessionStorage.getItem("access_token");
}
}
if(token != null && valueInStore != 'undefined'){
token.accessToken = valueInStore;
}
return token.accessToken;
}
async makeAPICall(requestDetails) {
return new Promise(function (resolve, reject) {
var body, xhr, i;
body = requestDetails.body || null;
xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.open(requestDetails.method, requestDetails.uri, true);
for (i in requestDetails.headers) {
xhr.setRequestHeader(i, requestDetails.headers[i]);
}
xhr.send(body);
xhr.onreadystatechange = function() {
if(xhr.readyState == 4) {
resolve(xhr);
}
}
})
}
}
Copieduser1 = Map();
user1.put("last_name", "Shawn");
user1.put("email", "shawn@zylkercorp.com");
user1.put("role", "692969000000015969");
user1.put("profile", "692969000000015972");
users = List();
users.add(user1);
params = Map();
params.put("users", users);
response = invokeurl
[
url: "https://zylkercorp.zohoplatform.com/crm/v2/users"
type: POST
parameters: params.toString()
connection:"crm_connection"
];
info response ;
In the request, "@newuser.json" contains the sample input data.
Request JSON
- last_namestring, mandatory
Specify the last name of the user.
- emailstring, mandatory
Specify the email ID of the user.
- rolestring, mandatory
Specify the unique ID of the role you want to assign the user with. You can obtain the role ID from the Roles API.
- profilestring, mandatory
Specify the unique ID of the profile you want to assign the user with, to decide the user's level of access to the data. You can obtain the profile ID from the Profiles API.
Refer to Add Records API to know about the field types and limitations.
Sample Input
Copied{
"users": [
{
"role": "554023000000015969",
"first_name": "Patricia",
"email": "Patricia@zylkercorp.com",
"profile": "554023000000015975",
"last_name": "Boyle"
}
]
}
Possible Errors
- LICENSE_LIMIT_EXCEEDEDHTTP 400
Request exceeds your license limit. Need to upgrade in order to add.
Resolution: The maximum number of users you can add per your Vertical Solutions plan has exceeded. Please buy additional user licenses to add more users. - DUPLICATE_DATAHTTP 400
Failed to add user since same email id is already present
Resolution: You have entered a duplicate value for the email_id. Make sure you give unique values for the email_id and last_name fields. - MANDATORY_NOT_FOUNDHTTP 200
Last Name is required
Resolution: You have not specified one of the mandatory keys. Refer to request JSON section above. - INVALID_DATAHTTP 400
Invalid data. Valid values are comma/space/period/none.
Resolution: The key value specified is invalid. It can contain only the above mentioned special characters. - FORBIDDENHTTP 403
Permission denied
Resolution: Only the users with user profile is "administrator" can add new users. Contact your system administrator. - INVALID_URL_PATTERNHTTP 404
Please check if the URL trying to access is a correct one
Resolution: The request URL specified is incorrect. Specify a valid request URL. Refer to request URL section above. - OAUTH_SCOPE_MISMATCHHTTP 401
Unauthorized
Resolution: Client does not have ZohoCRM.users.CREATE scope. Create a new client with valid scope. Refer to scope section above. - NO_PERMISSIONHTTP 403
Permission denied to create
Resolution: The user does not have permission to create user records. Contact your org admin. - INTERNAL_ERRORHTTP 500
Internal Server Error
Resolution: Unexpected and unhandled exception in the server. Contact support team. - INVALID_REQUEST_METHODHTTP 400
The http request method type is not a valid one
Resolution: You have specified an invalid HTTP method to access the API URL. Specify a valid request method. Refer to endpoints section above. - AUTHORIZATION_FAILEDHTTP 400
User does not have sufficient privilege to add new users
Resolution: The user does not have the permission to add users. Contact your org admin.
Sample Response
Copied{
"users": [
{
"code": "SUCCESS",
"details": {
"id": "554023000000691003"
},
"message": "User added",
"status": "success"
}
]
}