SQL Server / Support API / Create Or Update User
Back to Collection Items
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @http int
-- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
DECLARE @success int
EXEC sp_OASetProperty @http, 'BasicAuth', 1
EXEC sp_OASetProperty @http, 'Login', 'login'
EXEC sp_OASetProperty @http, '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>"
-- }
-- }
DECLARE @json int
-- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'user.name', '<string>'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'user.email', '<string>'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'user.custom_role_id', '<integer>'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'user.external_id', '<string>'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'user.identities[0].type', '<string>'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'user.identities[0].value', '<string>'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'user.identities[1].type', '<string>'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'user.identities[1].value', '<string>'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'user.organization.name', '<string>'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'user.organization_id', '<integer>'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'user.role', '<string>'
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Accept', 'application/json'
DECLARE @resp int
EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://example.zendesk.com/api/v2/users/create_or_update', 'application/json', @json
EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @json
RETURN
END
DECLARE @sbResponseBody int
-- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT
EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody
DECLARE @jResp int
-- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jResp OUT
EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
EXEC sp_OASetProperty @jResp, 'EmitCompact', 0
PRINT 'Response Body:'
EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
PRINT @sTmp0
DECLARE @respStatusCode int
EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT
PRINT 'Response Status Code = ' + @respStatusCode
IF @respStatusCode >= 400
BEGIN
PRINT 'Response Header:'
EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
PRINT @sTmp0
PRINT 'Failed.'
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @sbResponseBody
EXEC @hr = sp_OADestroy @jResp
RETURN
END
EXEC @hr = sp_OADestroy @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
DECLARE @Name nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Name OUT, 'user.name'
DECLARE @Active nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Active OUT, 'user.active'
DECLARE @Alias nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Alias OUT, 'user.alias'
DECLARE @Chat_only nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Chat_only OUT, 'user.chat_only'
DECLARE @Created_at nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Created_at OUT, 'user.created_at'
DECLARE @Custom_role_id nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Custom_role_id OUT, 'user.custom_role_id'
DECLARE @Default_group_id nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Default_group_id OUT, 'user.default_group_id'
DECLARE @Details nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Details OUT, 'user.details'
DECLARE @v_Email nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @v_Email OUT, 'user.email'
DECLARE @External_id nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @External_id OUT, 'user.external_id'
DECLARE @Iana_time_zone nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Iana_time_zone OUT, 'user.iana_time_zone'
DECLARE @Id nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Id OUT, 'user.id'
DECLARE @Last_login_at nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Last_login_at OUT, 'user.last_login_at'
DECLARE @Locale nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Locale OUT, 'user.locale'
DECLARE @Locale_id nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Locale_id OUT, 'user.locale_id'
DECLARE @Moderator nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Moderator OUT, 'user.moderator'
DECLARE @Notes nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Notes OUT, 'user.notes'
DECLARE @Only_private_comments nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Only_private_comments OUT, 'user.only_private_comments'
DECLARE @Organization_id nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Organization_id OUT, 'user.organization_id'
DECLARE @Phone nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Phone OUT, 'user.phone'
DECLARE @Dolorea1 int
EXEC sp_OAMethod @jResp, 'BoolOf', @Dolorea1 OUT, 'user.photo.dolorea1'
DECLARE @Deserunt3 nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Deserunt3 OUT, 'user.photo.deserunt3'
DECLARE @Remote_photo_url nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Remote_photo_url OUT, 'user.remote_photo_url'
DECLARE @Report_csv nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Report_csv OUT, 'user.report_csv'
DECLARE @Restricted_agent nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Restricted_agent OUT, 'user.restricted_agent'
DECLARE @Role nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Role OUT, 'user.role'
DECLARE @Role_type nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Role_type OUT, 'user.role_type'
DECLARE @v_Shared nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @v_Shared OUT, 'user.shared'
DECLARE @Shared_agent nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Shared_agent OUT, 'user.shared_agent'
DECLARE @Shared_phone_number nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Shared_phone_number OUT, 'user.shared_phone_number'
DECLARE @Signature nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Signature OUT, 'user.signature'
DECLARE @Suspended nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Suspended OUT, 'user.suspended'
DECLARE @Tags nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Tags OUT, 'user.tags'
DECLARE @Ticket_restriction nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Ticket_restriction OUT, 'user.ticket_restriction'
DECLARE @Time_zone nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Time_zone OUT, 'user.time_zone'
DECLARE @Two_factor_auth_enabled nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Two_factor_auth_enabled OUT, 'user.two_factor_auth_enabled'
DECLARE @Updated_at nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Updated_at OUT, 'user.updated_at'
DECLARE @v_Url nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @v_Url OUT, 'user.url'
DECLARE @Nostrudacc nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Nostrudacc OUT, 'user.user_fields.nostrudacc'
DECLARE @In_80e nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @In_80e OUT, 'user.user_fields.in_80e'
DECLARE @Proident_e2 int
EXEC sp_OAMethod @jResp, 'BoolOf', @Proident_e2 OUT, 'user.user_fields.proident_e2'
DECLARE @Voluptate_98 int
EXEC sp_OAMethod @jResp, 'IntOf', @Voluptate_98 OUT, 'user.user_fields.voluptate_98'
DECLARE @Verified nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Verified OUT, 'user.verified'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @sbResponseBody
EXEC @hr = sp_OADestroy @jResp
END
GO
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}"
}
]
}