Chilkat Online Tools

C++ / Support API / Create Or Update User

Back to Collection Items

#include <CkHttp.h>
#include <CkJsonObject.h>
#include <CkHttpResponse.h>
#include <CkStringBuilder.h>

void ChilkatSample(void)
    {
    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkHttp http;
    bool success;

    http.put_BasicAuth(true);
    http.put_Login("login");
    http.put_Password("password");

    // Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // {
    //   "user": {
    //     "name": "<string>",
    //     "email": "<string>",
    //     "custom_role_id": "<integer>",
    //     "external_id": "<string>",
    //     "identities": [
    //       {
    //         "type": "<string>",
    //         "value": "<string>"
    //       },
    //       {
    //         "type": "<string>",
    //         "value": "<string>"
    //       }
    //     ],
    //     "organization": {
    //       "name": "<string>"
    //     },
    //     "organization_id": "<integer>",
    //     "role": "<string>"
    //   }
    // }

    CkJsonObject json;
    json.UpdateString("user.name","<string>");
    json.UpdateString("user.email","<string>");
    json.UpdateString("user.custom_role_id","<integer>");
    json.UpdateString("user.external_id","<string>");
    json.UpdateString("user.identities[0].type","<string>");
    json.UpdateString("user.identities[0].value","<string>");
    json.UpdateString("user.identities[1].type","<string>");
    json.UpdateString("user.identities[1].value","<string>");
    json.UpdateString("user.organization.name","<string>");
    json.UpdateString("user.organization_id","<integer>");
    json.UpdateString("user.role","<string>");

    http.SetRequestHeader("Content-Type","application/json");
    http.SetRequestHeader("Accept","application/json");

    CkHttpResponse *resp = http.PostJson3("https://example.zendesk.com/api/v2/users/create_or_update","application/json",json);
    if (http.get_LastMethodSuccess() == false) {
        std::cout << http.lastErrorText() << "\r\n";
        return;
    }

    CkStringBuilder sbResponseBody;
    resp->GetBodySb(sbResponseBody);

    CkJsonObject jResp;
    jResp.LoadSb(sbResponseBody);
    jResp.put_EmitCompact(false);

    std::cout << "Response Body:" << "\r\n";
    std::cout << jResp.emit() << "\r\n";

    int respStatusCode = resp->get_StatusCode();
    std::cout << "Response Status Code = " << respStatusCode << "\r\n";
    if (respStatusCode >= 400) {
        std::cout << "Response Header:" << "\r\n";
        std::cout << resp->header() << "\r\n";
        std::cout << "Failed." << "\r\n";
        delete resp;
        return;
    }

    delete resp;

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "user": {
    //     "name": "<string>",
    //     "active": "<boolean>",
    //     "alias": "<string>",
    //     "chat_only": "<boolean>",
    //     "created_at": "<string>",
    //     "custom_role_id": "<integer>",
    //     "default_group_id": "<integer>",
    //     "details": "<string>",
    //     "email": "<string>",
    //     "external_id": "<string>",
    //     "iana_time_zone": "<string>",
    //     "id": "<integer>",
    //     "last_login_at": "<string>",
    //     "locale": "<string>",
    //     "locale_id": "<integer>",
    //     "moderator": "<boolean>",
    //     "notes": "<string>",
    //     "only_private_comments": "<boolean>",
    //     "organization_id": "<integer>",
    //     "phone": "<string>",
    //     "photo": {
    //       "dolorea1": false,
    //       "deserunt3": 95124319.10825741
    //     },
    //     "remote_photo_url": "<string>",
    //     "report_csv": "<boolean>",
    //     "restricted_agent": "<boolean>",
    //     "role": "<string>",
    //     "role_type": "<integer>",
    //     "shared": "<boolean>",
    //     "shared_agent": "<boolean>",
    //     "shared_phone_number": "<boolean>",
    //     "signature": "<string>",
    //     "suspended": "<boolean>",
    //     "tags": "<array>",
    //     "ticket_restriction": "<string>",
    //     "time_zone": "<string>",
    //     "two_factor_auth_enabled": "<boolean>",
    //     "updated_at": "<string>",
    //     "url": "<string>",
    //     "user_fields": {
    //       "nostrudacc": -12929960.546221554,
    //       "in_80e": 25167751.960886702,
    //       "proident_e2": true,
    //       "voluptate_98": -58995022
    //     },
    //     "verified": "<boolean>"
    //   }
    // }

    // Sample code for parsing the JSON response...
    // Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.

    const char *Name = jResp.stringOf("user.name");
    const char *Active = jResp.stringOf("user.active");
    const char *Alias = jResp.stringOf("user.alias");
    const char *Chat_only = jResp.stringOf("user.chat_only");
    const char *Created_at = jResp.stringOf("user.created_at");
    const char *Custom_role_id = jResp.stringOf("user.custom_role_id");
    const char *Default_group_id = jResp.stringOf("user.default_group_id");
    const char *Details = jResp.stringOf("user.details");
    const char *v_Email = jResp.stringOf("user.email");
    const char *External_id = jResp.stringOf("user.external_id");
    const char *Iana_time_zone = jResp.stringOf("user.iana_time_zone");
    const char *Id = jResp.stringOf("user.id");
    const char *Last_login_at = jResp.stringOf("user.last_login_at");
    const char *Locale = jResp.stringOf("user.locale");
    const char *Locale_id = jResp.stringOf("user.locale_id");
    const char *Moderator = jResp.stringOf("user.moderator");
    const char *Notes = jResp.stringOf("user.notes");
    const char *Only_private_comments = jResp.stringOf("user.only_private_comments");
    const char *Organization_id = jResp.stringOf("user.organization_id");
    const char *Phone = jResp.stringOf("user.phone");
    bool Dolorea1 = jResp.BoolOf("user.photo.dolorea1");
    const char *Deserunt3 = jResp.stringOf("user.photo.deserunt3");
    const char *Remote_photo_url = jResp.stringOf("user.remote_photo_url");
    const char *Report_csv = jResp.stringOf("user.report_csv");
    const char *Restricted_agent = jResp.stringOf("user.restricted_agent");
    const char *Role = jResp.stringOf("user.role");
    const char *Role_type = jResp.stringOf("user.role_type");
    const char *v_Shared = jResp.stringOf("user.shared");
    const char *Shared_agent = jResp.stringOf("user.shared_agent");
    const char *Shared_phone_number = jResp.stringOf("user.shared_phone_number");
    const char *Signature = jResp.stringOf("user.signature");
    const char *Suspended = jResp.stringOf("user.suspended");
    const char *Tags = jResp.stringOf("user.tags");
    const char *Ticket_restriction = jResp.stringOf("user.ticket_restriction");
    const char *Time_zone = jResp.stringOf("user.time_zone");
    const char *Two_factor_auth_enabled = jResp.stringOf("user.two_factor_auth_enabled");
    const char *Updated_at = jResp.stringOf("user.updated_at");
    const char *v_Url = jResp.stringOf("user.url");
    const char *Nostrudacc = jResp.stringOf("user.user_fields.nostrudacc");
    const char *In_80e = jResp.stringOf("user.user_fields.in_80e");
    bool Proident_e2 = jResp.BoolOf("user.user_fields.proident_e2");
    int Voluptate_98 = jResp.IntOf("user.user_fields.voluptate_98");
    const char *Verified = jResp.stringOf("user.verified");
    }

Curl Command

curl  -u login:password -X POST
	-H "Content-Type: application/json"
	-H "Accept: application/json"
	-d '{
  "user": {
    "name": "<string>",
    "email": "<string>",
    "custom_role_id": "<integer>",
    "external_id": "<string>",
    "identities": [
      {
        "type": "<string>",
        "value": "<string>"
      },
      {
        "type": "<string>",
        "value": "<string>"
      }
    ],
    "organization": {
      "name": "<string>"
    },
    "organization_id": "<integer>",
    "role": "<string>"
  }
}'
https://example.zendesk.com/api/v2/users/create_or_update

Postman Collection Item JSON

{
  "name": "Create Or Update User",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      },
      {
        "key": "Accept",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"user\": {\n    \"name\": \"<string>\",\n    \"email\": \"<string>\",\n    \"custom_role_id\": \"<integer>\",\n    \"external_id\": \"<string>\",\n    \"identities\": [\n      {\n        \"type\": \"<string>\",\n        \"value\": \"<string>\"\n      },\n      {\n        \"type\": \"<string>\",\n        \"value\": \"<string>\"\n      }\n    ],\n    \"organization\": {\n      \"name\": \"<string>\"\n    },\n    \"organization_id\": \"<integer>\",\n    \"role\": \"<string>\"\n  }\n}",
      "options": {
        "raw": {
          "headerFamily": "json",
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{baseUrl}}/api/v2/users/create_or_update",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "api",
        "v2",
        "users",
        "create_or_update"
      ]
    },
    "description": "Creates a user if the user does not already exist, or updates an existing user\nidentified by e-mail address or external ID.\n\nIf you don't specify a role parameter, the new user is assigned the role of end user.\n\nIf you need to create users without sending out a verification email, include a `\"skip_verify_email\": true` property in the body.\n\n#### Response Status Code\n\n- If the user exists in Zendesk, a successful request returns a 200 status code with \"Location: /api/v2/users/{user_id}.json\".\n- If the user does not exist in Zendesk, a successful request returns a 201 status code with \"Location: /api/v2/users/{new_user_id}.json\".\n\n#### Rate Limit\n\nThe rate limit is 5 requests per minute for each unique end user profile. For example, you can make 10 calls per second as long as you make five calls for one user and five calls for another user.\nThe rate limiting mechanism behaves as described in\n[Usage Limits](/api-reference/introduction/rate-limits/#monitoring-your-request-activity)\nin the API introduction. Zendesk recommends that you obey the Retry-After header values.\n\n#### Allowed For\n\n* Agents, with restrictions on certain actions\n"
  },
  "response": [
    {
      "name": "Successful response, when user exits",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "description": "Added as a part of security scheme: basic",
            "key": "Authorization",
            "value": "Basic <credentials>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"user\": {\n    \"name\": \"<string>\",\n    \"email\": \"<string>\",\n    \"custom_role_id\": \"<integer>\",\n    \"external_id\": \"<string>\",\n    \"identities\": [\n      {\n        \"type\": \"<string>\",\n        \"value\": \"<string>\"\n      },\n      {\n        \"type\": \"<string>\",\n        \"value\": \"<string>\"\n      }\n    ],\n    \"organization\": {\n      \"name\": \"<string>\"\n    },\n    \"organization_id\": \"<integer>\",\n    \"role\": \"<string>\"\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v2/users/create_or_update",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "users",
            "create_or_update"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"user\": {\n    \"name\": \"<string>\",\n    \"active\": \"<boolean>\",\n    \"alias\": \"<string>\",\n    \"chat_only\": \"<boolean>\",\n    \"created_at\": \"<string>\",\n    \"custom_role_id\": \"<integer>\",\n    \"default_group_id\": \"<integer>\",\n    \"details\": \"<string>\",\n    \"email\": \"<string>\",\n    \"external_id\": \"<string>\",\n    \"iana_time_zone\": \"<string>\",\n    \"id\": \"<integer>\",\n    \"last_login_at\": \"<string>\",\n    \"locale\": \"<string>\",\n    \"locale_id\": \"<integer>\",\n    \"moderator\": \"<boolean>\",\n    \"notes\": \"<string>\",\n    \"only_private_comments\": \"<boolean>\",\n    \"organization_id\": \"<integer>\",\n    \"phone\": \"<string>\",\n    \"photo\": {\n      \"dolorea1\": false,\n      \"deserunt3\": 95124319.10825741\n    },\n    \"remote_photo_url\": \"<string>\",\n    \"report_csv\": \"<boolean>\",\n    \"restricted_agent\": \"<boolean>\",\n    \"role\": \"<string>\",\n    \"role_type\": \"<integer>\",\n    \"shared\": \"<boolean>\",\n    \"shared_agent\": \"<boolean>\",\n    \"shared_phone_number\": \"<boolean>\",\n    \"signature\": \"<string>\",\n    \"suspended\": \"<boolean>\",\n    \"tags\": \"<array>\",\n    \"ticket_restriction\": \"<string>\",\n    \"time_zone\": \"<string>\",\n    \"two_factor_auth_enabled\": \"<boolean>\",\n    \"updated_at\": \"<string>\",\n    \"url\": \"<string>\",\n    \"user_fields\": {\n      \"nostrudacc\": -12929960.546221554,\n      \"in_80e\": 25167751.960886702,\n      \"proident_e2\": true,\n      \"voluptate_98\": -58995022\n    },\n    \"verified\": \"<boolean>\"\n  }\n}"
    },
    {
      "name": "Created response, when user is new",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "description": "Added as a part of security scheme: basic",
            "key": "Authorization",
            "value": "Basic <credentials>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"user\": {\n    \"name\": \"<string>\",\n    \"email\": \"<string>\",\n    \"custom_role_id\": \"<integer>\",\n    \"external_id\": \"<string>\",\n    \"identities\": [\n      {\n        \"type\": \"<string>\",\n        \"value\": \"<string>\"\n      },\n      {\n        \"type\": \"<string>\",\n        \"value\": \"<string>\"\n      }\n    ],\n    \"organization\": {\n      \"name\": \"<string>\"\n    },\n    \"organization_id\": \"<integer>\",\n    \"role\": \"<string>\"\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v2/users/create_or_update",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "users",
            "create_or_update"
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"user\": {\n    \"name\": \"<string>\",\n    \"active\": \"<boolean>\",\n    \"alias\": \"<string>\",\n    \"chat_only\": \"<boolean>\",\n    \"created_at\": \"<string>\",\n    \"custom_role_id\": \"<integer>\",\n    \"default_group_id\": \"<integer>\",\n    \"details\": \"<string>\",\n    \"email\": \"<string>\",\n    \"external_id\": \"<string>\",\n    \"iana_time_zone\": \"<string>\",\n    \"id\": \"<integer>\",\n    \"last_login_at\": \"<string>\",\n    \"locale\": \"<string>\",\n    \"locale_id\": \"<integer>\",\n    \"moderator\": \"<boolean>\",\n    \"notes\": \"<string>\",\n    \"only_private_comments\": \"<boolean>\",\n    \"organization_id\": \"<integer>\",\n    \"phone\": \"<string>\",\n    \"photo\": {\n      \"dolorea1\": false,\n      \"deserunt3\": 95124319.10825741\n    },\n    \"remote_photo_url\": \"<string>\",\n    \"report_csv\": \"<boolean>\",\n    \"restricted_agent\": \"<boolean>\",\n    \"role\": \"<string>\",\n    \"role_type\": \"<integer>\",\n    \"shared\": \"<boolean>\",\n    \"shared_agent\": \"<boolean>\",\n    \"shared_phone_number\": \"<boolean>\",\n    \"signature\": \"<string>\",\n    \"suspended\": \"<boolean>\",\n    \"tags\": \"<array>\",\n    \"ticket_restriction\": \"<string>\",\n    \"time_zone\": \"<string>\",\n    \"two_factor_auth_enabled\": \"<boolean>\",\n    \"updated_at\": \"<string>\",\n    \"url\": \"<string>\",\n    \"user_fields\": {\n      \"nostrudacc\": -12929960.546221554,\n      \"in_80e\": 25167751.960886702,\n      \"proident_e2\": true,\n      \"voluptate_98\": -58995022\n    },\n    \"verified\": \"<boolean>\"\n  }\n}"
    }
  ]
}