Chilkat Online Tools

SQL Server / Support API / Create Many Users

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
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.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.

    -- {
    --   "users": [
    --     {
    --       "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>"
    --     },
    --     {
    --       "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
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'users[0].name', '<string>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'users[0].email', '<string>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'users[0].custom_role_id', '<integer>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'users[0].external_id', '<string>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'users[0].identities[0].type', '<string>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'users[0].identities[0].value', '<string>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'users[0].identities[1].type', '<string>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'users[0].identities[1].value', '<string>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'users[0].organization.name', '<string>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'users[0].organization_id', '<integer>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'users[0].role', '<string>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'users[1].name', '<string>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'users[1].email', '<string>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'users[1].custom_role_id', '<integer>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'users[1].external_id', '<string>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'users[1].identities[0].type', '<string>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'users[1].identities[0].value', '<string>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'users[1].identities[1].type', '<string>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'users[1].identities[1].value', '<string>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'users[1].organization.name', '<string>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'users[1].organization_id', '<integer>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'users[1].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_many', '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
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody

    DECLARE @jResp int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.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)

    -- {
    --   "job_status": {
    --     "id": "<string>",
    --     "message": "<string>",
    --     "progress": "<integer>",
    --     "results": [
    --       {
    --         "id": "<integer>",
    --         "index": "<integer>"
    --       },
    --       {
    --         "id": "<integer>",
    --         "index": "<integer>"
    --       }
    --     ],
    --     "status": "<string>",
    --     "total": "<integer>",
    --     "url": "<string>"
    --   }
    -- }

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

    DECLARE @id nvarchar(4000)

    DECLARE @index nvarchar(4000)

    DECLARE @Id nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Id OUT, 'job_status.id'
    DECLARE @Message nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Message OUT, 'job_status.message'
    DECLARE @Progress nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Progress OUT, 'job_status.progress'
    DECLARE @Status nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Status OUT, 'job_status.status'
    DECLARE @Total nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Total OUT, 'job_status.total'
    DECLARE @v_Url nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @v_Url OUT, 'job_status.url'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'job_status.results'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @id OUT, 'job_status.results[i].id'
        EXEC sp_OAMethod @jResp, 'StringOf', @index OUT, 'job_status.results[i].index'
        SELECT @i = @i + 1
      END

    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 '{
  "users": [
    {
      "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>"
    },
    {
      "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_many

Postman Collection Item JSON

{
  "name": "Create Many Users",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      },
      {
        "key": "Accept",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"users\": [\n    {\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    {\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  ]\n}",
      "options": {
        "raw": {
          "headerFamily": "json",
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{baseUrl}}/api/v2/users/create_many",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "api",
        "v2",
        "users",
        "create_many"
      ]
    },
    "description": "Accepts an array of up to 100 user objects.\n\n**Note**: To protect the data in your Zendesk account, bulk user imports are not enabled by default in Zendesk accounts. The account owner must contact [Zendesk Customer Support](https://support.zendesk.com/hc/en-us/articles/4408843597850) to enable the imports. A 403 Forbidden\nerror is returned if data imports are not enabled.\n\n#### Allowed For\n\n* Agents, with restrictions on certain actions\n\n#### Specifying an organization\n\nYou can assign a user to an existing organization by setting an\n`organization_id` property in the user object.\n\n#### Response\n\nThis endpoint returns a `job_status` [JSON object](/api-reference/ticketing/ticket-management/job_statuses/#json-format) and queues a background job to do the work.\nUse the [Show Job Status](/api-reference/ticketing/ticket-management/job_statuses/#show-job-status) endpoint to check for the job's completion.\n"
  },
  "response": [
    {
      "name": "Success response",
      "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  \"users\": [\n    {\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    {\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  ]\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v2/users/create_many",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "users",
            "create_many"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"job_status\": {\n    \"id\": \"<string>\",\n    \"message\": \"<string>\",\n    \"progress\": \"<integer>\",\n    \"results\": [\n      {\n        \"id\": \"<integer>\",\n        \"index\": \"<integer>\"\n      },\n      {\n        \"id\": \"<integer>\",\n        \"index\": \"<integer>\"\n      }\n    ],\n    \"status\": \"<string>\",\n    \"total\": \"<integer>\",\n    \"url\": \"<string>\"\n  }\n}"
    }
  ]
}