Chilkat Online Tools

SQL Server / Datadog API Collection / Create a child organization

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.

    -- {
    --   "name": "New child org",
    --   "billing": {
    --     "type": "parent_billing"
    --   },
    --   "subscription": {
    --     "type": "pro"
    --   }
    -- }

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

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'name', 'New child org'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'billing.type', 'parent_billing'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'subscription.type', 'pro'

    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/v1/org', '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)

    -- {
    --   "api_key": {
    --     "created": "2019-08-02 15:31:07",
    --     "created_by": "john@example.com",
    --     "key": "1234512345123456abcabc912349abcd",
    --     "name": "example user"
    --   },
    --   "application_key": {
    --     "hash": "1234512345123459cda4eb9ced49a3d84fd0138c",
    --     "name": "example user",
    --     "owner": "example.com"
    --   },
    --   "org": {
    --     "billing": {
    --       "type": "parent_billing"
    --     },
    --     "created": "2019-09-26T17:28:28Z",
    --     "description": "some description",
    --     "name": "New child org",
    --     "public_id": "abcdef12345",
    --     "settings": {
    --       "private_widget_share": false,
    --       "saml": {
    --         "enabled": false
    --       },
    --       "saml_autocreate_access_role": "ro",
    --       "saml_autocreate_users_domains": {
    --         "domains": [
    --           "example.com",
    --           "example.com"
    --         ],
    --         "enabled": false
    --       },
    --       "saml_can_be_enabled": false,
    --       "saml_idp_endpoint": "https://my.saml.endpoint",
    --       "saml_idp_initiated_login": {
    --         "enabled": false
    --       },
    --       "saml_idp_metadata_uploaded": false,
    --       "saml_login_url": "https://my.saml.login.url",
    --       "saml_strict_mode": {
    --         "enabled": false
    --       }
    --     },
    --     "subscription": {
    --       "type": "pro"
    --     },
    --     "trial": false
    --   },
    --   "user": {
    --     "access_role": "ro",
    --     "disabled": false,
    --     "email": "test@datadoghq.com",
    --     "handle": "test@datadoghq.com",
    --     "icon": "/path/to/matching/gravatar/icon",
    --     "name": "test user",
    --     "verified": true
    --   }
    -- }

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

    DECLARE @strVal nvarchar(4000)

    DECLARE @Created nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Created OUT, 'api_key.created'
    DECLARE @Created_by nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Created_by OUT, 'api_key.created_by'
    DECLARE @Key nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Key OUT, 'api_key.key'
    DECLARE @Name nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Name OUT, 'api_key.name'
    DECLARE @Hash nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Hash OUT, 'application_key.hash'
    DECLARE @application_keyName nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @application_keyName OUT, 'application_key.name'
    DECLARE @Owner nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Owner OUT, 'application_key.owner'
    DECLARE @v_Type nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @v_Type OUT, 'org.billing.type'
    DECLARE @orgCreated nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @orgCreated OUT, 'org.created'
    DECLARE @Description nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Description OUT, 'org.description'
    DECLARE @orgName nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @orgName OUT, 'org.name'
    DECLARE @Public_id nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Public_id OUT, 'org.public_id'
    DECLARE @Private_widget_share int
    EXEC sp_OAMethod @jResp, 'BoolOf', @Private_widget_share OUT, 'org.settings.private_widget_share'
    DECLARE @Enabled int
    EXEC sp_OAMethod @jResp, 'BoolOf', @Enabled OUT, 'org.settings.saml.enabled'
    DECLARE @Saml_autocreate_access_role nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Saml_autocreate_access_role OUT, 'org.settings.saml_autocreate_access_role'
    DECLARE @Saml_autocreate_users_domainsEnabled int
    EXEC sp_OAMethod @jResp, 'BoolOf', @Saml_autocreate_users_domainsEnabled OUT, 'org.settings.saml_autocreate_users_domains.enabled'
    DECLARE @Saml_can_be_enabled int
    EXEC sp_OAMethod @jResp, 'BoolOf', @Saml_can_be_enabled OUT, 'org.settings.saml_can_be_enabled'
    DECLARE @Saml_idp_endpoint nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Saml_idp_endpoint OUT, 'org.settings.saml_idp_endpoint'
    DECLARE @Saml_idp_initiated_loginEnabled int
    EXEC sp_OAMethod @jResp, 'BoolOf', @Saml_idp_initiated_loginEnabled OUT, 'org.settings.saml_idp_initiated_login.enabled'
    DECLARE @Saml_idp_metadata_uploaded int
    EXEC sp_OAMethod @jResp, 'BoolOf', @Saml_idp_metadata_uploaded OUT, 'org.settings.saml_idp_metadata_uploaded'
    DECLARE @Saml_login_url nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Saml_login_url OUT, 'org.settings.saml_login_url'
    DECLARE @Saml_strict_modeEnabled int
    EXEC sp_OAMethod @jResp, 'BoolOf', @Saml_strict_modeEnabled OUT, 'org.settings.saml_strict_mode.enabled'
    DECLARE @SubscriptionType nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @SubscriptionType OUT, 'org.subscription.type'
    DECLARE @Trial int
    EXEC sp_OAMethod @jResp, 'BoolOf', @Trial OUT, 'org.trial'
    DECLARE @Access_role nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Access_role OUT, 'user.access_role'
    DECLARE @Disabled int
    EXEC sp_OAMethod @jResp, 'BoolOf', @Disabled OUT, 'user.disabled'
    DECLARE @v_Email nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @v_Email OUT, 'user.email'
    DECLARE @Handle nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Handle OUT, 'user.handle'
    DECLARE @Icon nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Icon OUT, 'user.icon'
    DECLARE @userName nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @userName OUT, 'user.name'
    DECLARE @Verified int
    EXEC sp_OAMethod @jResp, 'BoolOf', @Verified OUT, 'user.verified'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'org.settings.saml_autocreate_users_domains.domains'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @strVal OUT, 'org.settings.saml_autocreate_users_domains.domains[i]'
        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 '{
  "name": "New child org",
  "billing": {
    "type": "parent_billing"
  },
  "subscription": {
    "type": "pro"
  }
}'
https://api.app.ddog-gov.com/api/v1/org

Postman Collection Item JSON

{
  "name": "Create a child organization",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      },
      {
        "key": "Accept",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"name\": \"New child org\",\n  \"billing\": {\n    \"type\": \"parent_billing\"\n  },\n  \"subscription\": {\n    \"type\": \"pro\"\n  }\n}",
      "options": {
        "raw": {
          "headerFamily": "json",
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{baseUrl}}/api/v1/org",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "api",
        "v1",
        "org"
      ]
    },
    "description": "Create a child organization.\n\nThis endpoint requires the\n[multi-organization account](https://docs.datadoghq.com/account_management/multi_organization/)\nfeature and must be enabled by\n[contacting support](https://docs.datadoghq.com/help/).\n\nOnce a new child organization is created, you can interact with it\nby using the `org.public_id`, `api_key.key`, and\n`application_key.hash` provided in the response."
  },
  "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  \"name\": \"New child org\",\n  \"billing\": {\n    \"type\": \"parent_billing\"\n  },\n  \"subscription\": {\n    \"type\": \"pro\"\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v1/org",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "org"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"api_key\": {\n    \"created\": \"2019-08-02 15:31:07\",\n    \"created_by\": \"john@example.com\",\n    \"key\": \"1234512345123456abcabc912349abcd\",\n    \"name\": \"example user\"\n  },\n  \"application_key\": {\n    \"hash\": \"1234512345123459cda4eb9ced49a3d84fd0138c\",\n    \"name\": \"example user\",\n    \"owner\": \"example.com\"\n  },\n  \"org\": {\n    \"billing\": {\n      \"type\": \"parent_billing\"\n    },\n    \"created\": \"2019-09-26T17:28:28Z\",\n    \"description\": \"some description\",\n    \"name\": \"New child org\",\n    \"public_id\": \"abcdef12345\",\n    \"settings\": {\n      \"private_widget_share\": false,\n      \"saml\": {\n        \"enabled\": false\n      },\n      \"saml_autocreate_access_role\": \"ro\",\n      \"saml_autocreate_users_domains\": {\n        \"domains\": [\n          \"example.com\",\n          \"example.com\"\n        ],\n        \"enabled\": false\n      },\n      \"saml_can_be_enabled\": false,\n      \"saml_idp_endpoint\": \"https://my.saml.endpoint\",\n      \"saml_idp_initiated_login\": {\n        \"enabled\": false\n      },\n      \"saml_idp_metadata_uploaded\": false,\n      \"saml_login_url\": \"https://my.saml.login.url\",\n      \"saml_strict_mode\": {\n        \"enabled\": false\n      }\n    },\n    \"subscription\": {\n      \"type\": \"pro\"\n    },\n    \"trial\": false\n  },\n  \"user\": {\n    \"access_role\": \"ro\",\n    \"disabled\": false,\n    \"email\": \"test@datadoghq.com\",\n    \"handle\": \"test@datadoghq.com\",\n    \"icon\": \"/path/to/matching/gravatar/icon\",\n    \"name\": \"test user\",\n    \"verified\": true\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  \"name\": \"New child org\",\n  \"billing\": {\n    \"type\": \"parent_billing\"\n  },\n  \"subscription\": {\n    \"type\": \"pro\"\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v1/org",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "org"
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"Bad Request\",\n    \"Bad Request\"\n  ]\n}"
    },
    {
      "name": "Forbidden",
      "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  \"name\": \"New child org\",\n  \"billing\": {\n    \"type\": \"parent_billing\"\n  },\n  \"subscription\": {\n    \"type\": \"pro\"\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v1/org",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "org"
          ]
        }
      },
      "status": "Forbidden",
      "code": 403,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"Bad Request\",\n    \"Bad Request\"\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  \"name\": \"New child org\",\n  \"billing\": {\n    \"type\": \"parent_billing\"\n  },\n  \"subscription\": {\n    \"type\": \"pro\"\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v1/org",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "org"
          ]
        }
      },
      "status": "Too Many Requests",
      "code": 429,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"Bad Request\",\n    \"Bad Request\"\n  ]\n}"
    }
  ]
}