Chilkat Online Tools

SQL Server / Datadog API Collection / Create a 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
    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

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

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

    -- {
    --   "data": {
    --     "attributes": {
    --       "email": "<string>",
    --       "name": "<string>",
    --       "title": "<string>"
    --     },
    --     "type": "users",
    --     "relationships": {
    --       "roles": {
    --         "data": [
    --           {
    --             "id": "<string>",
    --             "type": "roles"
    --           },
    --           {
    --             "id": "<string>",
    --             "type": "roles"
    --           }
    --         ]
    --       }
    --     }
    --   }
    -- }

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data.attributes.email', '<string>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data.attributes.name', '<string>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data.attributes.title', '<string>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data.type', 'users'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data.relationships.roles.data[0].id', '<string>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data.relationships.roles.data[0].type', 'roles'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data.relationships.roles.data[1].id', '<string>'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data.relationships.roles.data[1].type', 'roles'

    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://api.app.ddog-gov.com/api/v2/users', '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)

    -- {
    --   "data": {
    --     "attributes": {
    --       "created_at": "<dateTime>",
    --       "disabled": "<boolean>",
    --       "email": "<string>",
    --       "handle": "<string>",
    --       "icon": "<string>",
    --       "modified_at": "<dateTime>",
    --       "name": "<string>",
    --       "service_account": "<boolean>",
    --       "status": "<string>",
    --       "title": "<string>",
    --       "verified": "<boolean>"
    --     },
    --     "id": "<string>",
    --     "relationships": {
    --       "org": {
    --         "data": {
    --           "id": "<string>",
    --           "type": "orgs"
    --         }
    --       },
    --       "other_orgs": {
    --         "data": [
    --           {
    --             "id": "<string>",
    --             "type": "orgs"
    --           },
    --           {
    --             "id": "<string>",
    --             "type": "orgs"
    --           }
    --         ]
    --       },
    --       "other_users": {
    --         "data": [
    --           {
    --             "id": "<string>",
    --             "type": "users"
    --           },
    --           {
    --             "id": "<string>",
    --             "type": "users"
    --           }
    --         ]
    --       },
    --       "roles": {
    --         "data": [
    --           {
    --             "id": "<string>",
    --             "type": "roles"
    --           },
    --           {
    --             "id": "<string>",
    --             "type": "roles"
    --           }
    --         ]
    --       }
    --     },
    --     "type": "users"
    --   },
    --   "included": [
    --     {
    --       "type": "orgs",
    --       "attributes": {
    --         "created_at": "<dateTime>",
    --         "description": "<string>",
    --         "disabled": "<boolean>",
    --         "modified_at": "<dateTime>",
    --         "name": "<string>",
    --         "public_id": "<string>",
    --         "sharing": "<string>",
    --         "url": "<string>"
    --       },
    --       "id": "<string>"
    --     },
    --     {
    --       "type": "orgs",
    --       "attributes": {
    --         "created_at": "<dateTime>",
    --         "description": "<string>",
    --         "disabled": "<boolean>",
    --         "modified_at": "<dateTime>",
    --         "name": "<string>",
    --         "public_id": "<string>",
    --         "sharing": "<string>",
    --         "url": "<string>"
    --       },
    --       "id": "<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 @v_type nvarchar(4000)

    DECLARE @attributesCreated_at nvarchar(4000)

    DECLARE @Description nvarchar(4000)

    DECLARE @attributesDisabled nvarchar(4000)

    DECLARE @attributesModified_at nvarchar(4000)

    DECLARE @attributesName nvarchar(4000)

    DECLARE @Public_id nvarchar(4000)

    DECLARE @Sharing nvarchar(4000)

    DECLARE @v_Url nvarchar(4000)

    DECLARE @Created_at nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Created_at OUT, 'data.attributes.created_at'
    DECLARE @Disabled nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Disabled OUT, 'data.attributes.disabled'
    DECLARE @v_Email nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @v_Email OUT, 'data.attributes.email'
    DECLARE @Handle nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Handle OUT, 'data.attributes.handle'
    DECLARE @Icon nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Icon OUT, 'data.attributes.icon'
    DECLARE @Modified_at nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Modified_at OUT, 'data.attributes.modified_at'
    DECLARE @Name nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Name OUT, 'data.attributes.name'
    DECLARE @Service_account nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Service_account OUT, 'data.attributes.service_account'
    DECLARE @Status nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Status OUT, 'data.attributes.status'
    DECLARE @Title nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Title OUT, 'data.attributes.title'
    DECLARE @Verified nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Verified OUT, 'data.attributes.verified'
    DECLARE @Id nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Id OUT, 'data.id'
    DECLARE @DataId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @DataId OUT, 'data.relationships.org.data.id'
    DECLARE @v_Type nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @v_Type OUT, 'data.relationships.org.data.type'
    DECLARE @dataType nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @dataType OUT, 'data.type'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'data.relationships.other_orgs.data'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @id OUT, 'data.relationships.other_orgs.data[i].id'
        EXEC sp_OAMethod @jResp, 'StringOf', @v_type OUT, 'data.relationships.other_orgs.data[i].type'
        SELECT @i = @i + 1
      END
    SELECT @i = 0
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'data.relationships.other_users.data'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @id OUT, 'data.relationships.other_users.data[i].id'
        EXEC sp_OAMethod @jResp, 'StringOf', @v_type OUT, 'data.relationships.other_users.data[i].type'
        SELECT @i = @i + 1
      END
    SELECT @i = 0
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'data.relationships.roles.data'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @id OUT, 'data.relationships.roles.data[i].id'
        EXEC sp_OAMethod @jResp, 'StringOf', @v_type OUT, 'data.relationships.roles.data[i].type'
        SELECT @i = @i + 1
      END
    SELECT @i = 0
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'included'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @v_type OUT, 'included[i].type'
        EXEC sp_OAMethod @jResp, 'StringOf', @attributesCreated_at OUT, 'included[i].attributes.created_at'
        EXEC sp_OAMethod @jResp, 'StringOf', @Description OUT, 'included[i].attributes.description'
        EXEC sp_OAMethod @jResp, 'StringOf', @attributesDisabled OUT, 'included[i].attributes.disabled'
        EXEC sp_OAMethod @jResp, 'StringOf', @attributesModified_at OUT, 'included[i].attributes.modified_at'
        EXEC sp_OAMethod @jResp, 'StringOf', @attributesName OUT, 'included[i].attributes.name'
        EXEC sp_OAMethod @jResp, 'StringOf', @Public_id OUT, 'included[i].attributes.public_id'
        EXEC sp_OAMethod @jResp, 'StringOf', @Sharing OUT, 'included[i].attributes.sharing'
        EXEC sp_OAMethod @jResp, 'StringOf', @v_Url OUT, 'included[i].attributes.url'
        EXEC sp_OAMethod @jResp, 'StringOf', @id OUT, 'included[i].id'
        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 -X POST
	-H "Content-Type: application/json"
	-H "Accept: application/json"
	-d '{
  "data": {
    "attributes": {
      "email": "<string>",
      "name": "<string>",
      "title": "<string>"
    },
    "type": "users",
    "relationships": {
      "roles": {
        "data": [
          {
            "id": "<string>",
            "type": "roles"
          },
          {
            "id": "<string>",
            "type": "roles"
          }
        ]
      }
    }
  }
}'
https://api.app.ddog-gov.com/api/v2/users

Postman Collection Item JSON

{
  "name": "Create a user",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      },
      {
        "key": "Accept",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"data\": {\n    \"attributes\": {\n      \"email\": \"<string>\",\n      \"name\": \"<string>\",\n      \"title\": \"<string>\"\n    },\n    \"type\": \"users\",\n    \"relationships\": {\n      \"roles\": {\n        \"data\": [\n          {\n            \"id\": \"<string>\",\n            \"type\": \"roles\"\n          },\n          {\n            \"id\": \"<string>\",\n            \"type\": \"roles\"\n          }\n        ]\n      }\n    }\n  }\n}",
      "options": {
        "raw": {
          "headerFamily": "json",
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{baseUrl}}/api/v2/users",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "api",
        "v2",
        "users"
      ]
    },
    "description": "Create a user for your organization."
  },
  "response": [
    {
      "name": "OK",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"data\": {\n    \"attributes\": {\n      \"email\": \"<string>\",\n      \"name\": \"<string>\",\n      \"title\": \"<string>\"\n    },\n    \"type\": \"users\",\n    \"relationships\": {\n      \"roles\": {\n        \"data\": [\n          {\n            \"id\": \"<string>\",\n            \"type\": \"roles\"\n          },\n          {\n            \"id\": \"<string>\",\n            \"type\": \"roles\"\n          }\n        ]\n      }\n    }\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v2/users",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "users"
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"data\": {\n    \"attributes\": {\n      \"created_at\": \"<dateTime>\",\n      \"disabled\": \"<boolean>\",\n      \"email\": \"<string>\",\n      \"handle\": \"<string>\",\n      \"icon\": \"<string>\",\n      \"modified_at\": \"<dateTime>\",\n      \"name\": \"<string>\",\n      \"service_account\": \"<boolean>\",\n      \"status\": \"<string>\",\n      \"title\": \"<string>\",\n      \"verified\": \"<boolean>\"\n    },\n    \"id\": \"<string>\",\n    \"relationships\": {\n      \"org\": {\n        \"data\": {\n          \"id\": \"<string>\",\n          \"type\": \"orgs\"\n        }\n      },\n      \"other_orgs\": {\n        \"data\": [\n          {\n            \"id\": \"<string>\",\n            \"type\": \"orgs\"\n          },\n          {\n            \"id\": \"<string>\",\n            \"type\": \"orgs\"\n          }\n        ]\n      },\n      \"other_users\": {\n        \"data\": [\n          {\n            \"id\": \"<string>\",\n            \"type\": \"users\"\n          },\n          {\n            \"id\": \"<string>\",\n            \"type\": \"users\"\n          }\n        ]\n      },\n      \"roles\": {\n        \"data\": [\n          {\n            \"id\": \"<string>\",\n            \"type\": \"roles\"\n          },\n          {\n            \"id\": \"<string>\",\n            \"type\": \"roles\"\n          }\n        ]\n      }\n    },\n    \"type\": \"users\"\n  },\n  \"included\": [\n    {\n      \"type\": \"orgs\",\n      \"attributes\": {\n        \"created_at\": \"<dateTime>\",\n        \"description\": \"<string>\",\n        \"disabled\": \"<boolean>\",\n        \"modified_at\": \"<dateTime>\",\n        \"name\": \"<string>\",\n        \"public_id\": \"<string>\",\n        \"sharing\": \"<string>\",\n        \"url\": \"<string>\"\n      },\n      \"id\": \"<string>\"\n    },\n    {\n      \"type\": \"orgs\",\n      \"attributes\": {\n        \"created_at\": \"<dateTime>\",\n        \"description\": \"<string>\",\n        \"disabled\": \"<boolean>\",\n        \"modified_at\": \"<dateTime>\",\n        \"name\": \"<string>\",\n        \"public_id\": \"<string>\",\n        \"sharing\": \"<string>\",\n        \"url\": \"<string>\"\n      },\n      \"id\": \"<string>\"\n    }\n  ]\n}"
    },
    {
      "name": "Bad Request",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"data\": {\n    \"attributes\": {\n      \"email\": \"<string>\",\n      \"name\": \"<string>\",\n      \"title\": \"<string>\"\n    },\n    \"type\": \"users\",\n    \"relationships\": {\n      \"roles\": {\n        \"data\": [\n          {\n            \"id\": \"<string>\",\n            \"type\": \"roles\"\n          },\n          {\n            \"id\": \"<string>\",\n            \"type\": \"roles\"\n          }\n        ]\n      }\n    }\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v2/users",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "users"
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"<string>\",\n    \"<string>\"\n  ]\n}"
    },
    {
      "name": "Authentication error",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"data\": {\n    \"attributes\": {\n      \"email\": \"<string>\",\n      \"name\": \"<string>\",\n      \"title\": \"<string>\"\n    },\n    \"type\": \"users\",\n    \"relationships\": {\n      \"roles\": {\n        \"data\": [\n          {\n            \"id\": \"<string>\",\n            \"type\": \"roles\"\n          },\n          {\n            \"id\": \"<string>\",\n            \"type\": \"roles\"\n          }\n        ]\n      }\n    }\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v2/users",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "users"
          ]
        }
      },
      "status": "Forbidden",
      "code": 403,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"<string>\",\n    \"<string>\"\n  ]\n}"
    },
    {
      "name": "Too many requests",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"data\": {\n    \"attributes\": {\n      \"email\": \"<string>\",\n      \"name\": \"<string>\",\n      \"title\": \"<string>\"\n    },\n    \"type\": \"users\",\n    \"relationships\": {\n      \"roles\": {\n        \"data\": [\n          {\n            \"id\": \"<string>\",\n            \"type\": \"roles\"\n          },\n          {\n            \"id\": \"<string>\",\n            \"type\": \"roles\"\n          }\n        ]\n      }\n    }\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v2/users",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "users"
          ]
        }
      },
      "status": "Too Many Requests",
      "code": 429,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"<string>\",\n    \"<string>\"\n  ]\n}"
    }
  ]
}