Chilkat Online Tools

SQL Server / Cognite API v1 / Create assets

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

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

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

    -- {
    --   "items": [
    --     {
    --       "name": "anim aliquip cupidatat commodo laborum",
    --       "externalId": "veniam in reprehenderit culpa",
    --       "parentId": 7287302321534466,
    --       "parentExternalId": "nostrud pariatur aute",
    --       "description": "nisi incididunt",
    --       "dataSetId": 1616183313071840,
    --       "metadata": {},
    --       "source": "fugiat ",
    --       "labels": [
    --         {
    --           "externalId": "eu tempor aliqua ipsum quis"
    --         },
    --         {
    --           "externalId": "laborum est aliquip magna"
    --         }
    --       ]
    --     },
    --     {
    --       "name": "in culpa ullamco fugiat p",
    --       "externalId": "qui quis anim ",
    --       "parentId": 4737890210897342,
    --       "parentExternalId": "in",
    --       "description": "id Excepteur volu",
    --       "dataSetId": 6713921295205546,
    --       "metadata": {},
    --       "source": "nostrud",
    --       "labels": [
    --         {
    --           "externalId": "dolor consequat fugiat nisi"
    --         },
    --         {
    --           "externalId": "dol"
    --         }
    --       ]
    --     }
    --   ]
    -- }

    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, 'items[0].name', 'anim aliquip cupidatat commodo laborum'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].externalId', 'veniam in reprehenderit culpa'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'items[0].parentId', 123
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].parentExternalId', 'nostrud pariatur aute'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].description', 'nisi incididunt'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'items[0].dataSetId', 123
    EXEC sp_OAMethod @json, 'UpdateNewObject', @success OUT, 'items[0].metadata'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].source', 'fugiat '
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].labels[0].externalId', 'eu tempor aliqua ipsum quis'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[0].labels[1].externalId', 'laborum est aliquip magna'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].name', 'in culpa ullamco fugiat p'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].externalId', 'qui quis anim '
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'items[1].parentId', 123
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].parentExternalId', 'in'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].description', 'id Excepteur volu'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'items[1].dataSetId', 123
    EXEC sp_OAMethod @json, 'UpdateNewObject', @success OUT, 'items[1].metadata'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].source', 'nostrud'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].labels[0].externalId', 'dolor consequat fugiat nisi'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'items[1].labels[1].externalId', 'dol'

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'content-type', 'application/json'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'api-key', '{{api-key}}'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://domain.com/api/v1/projects/{{project}}/assets', '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

    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT @iTmp0
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0
    EXEC @hr = sp_OADestroy @resp


    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json


END
GO

Curl Command

curl -X POST
	-H "api-key: {{api-key}}"
	-H "content-type: application/json"
	-d '{
    "items": [
        {
            "name": "anim aliquip cupidatat commodo laborum",
            "externalId": "veniam in reprehenderit culpa",
            "parentId": 7287302321534466,
            "parentExternalId": "nostrud pariatur aute",
            "description": "nisi incididunt",
            "dataSetId": 1616183313071840,
            "metadata": {},
            "source": "fugiat ",
            "labels": [
                {
                    "externalId": "eu tempor aliqua ipsum quis"
                },
                {
                    "externalId": "laborum est aliquip magna"
                }
            ]
        },
        {
            "name": "in culpa ullamco fugiat p",
            "externalId": "qui quis anim ",
            "parentId": 4737890210897342,
            "parentExternalId": "in",
            "description": "id Excepteur volu",
            "dataSetId": 6713921295205546,
            "metadata": {},
            "source": "nostrud",
            "labels": [
                {
                    "externalId": "dolor consequat fugiat nisi"
                },
                {
                    "externalId": "dol"
                }
            ]
        }
    ]
}'
https://domain.com/api/v1/projects/{{project}}/assets

Postman Collection Item JSON

{
  "id": "createAssets",
  "name": "Create assets",
  "request": {
    "url": {
      "host": "{{baseUrl}}",
      "path": [
        "api",
        "v1",
        "projects",
        "{{project}}",
        "assets"
      ],
      "query": [
      ],
      "variable": [
      ]
    },
    "method": "POST",
    "header": [
      {
        "key": "api-key",
        "value": "{{api-key}}",
        "description": "An admin can create API keys in the Cognite console."
      },
      {
        "key": "content-type",
        "value": "application/json"
      }
    ],
    "description": "You can create a maximum of 1000 assets per request.",
    "body": {
      "mode": "raw",
      "raw": "{\n    \"items\": [\n        {\n            \"name\": \"anim aliquip cupidatat commodo laborum\",\n            \"externalId\": \"veniam in reprehenderit culpa\",\n            \"parentId\": 7287302321534466,\n            \"parentExternalId\": \"nostrud pariatur aute\",\n            \"description\": \"nisi incididunt\",\n            \"dataSetId\": 1616183313071840,\n            \"metadata\": {},\n            \"source\": \"fugiat \",\n            \"labels\": [\n                {\n                    \"externalId\": \"eu tempor aliqua ipsum quis\"\n                },\n                {\n                    \"externalId\": \"laborum est aliquip magna\"\n                }\n            ]\n        },\n        {\n            \"name\": \"in culpa ullamco fugiat p\",\n            \"externalId\": \"qui quis anim \",\n            \"parentId\": 4737890210897342,\n            \"parentExternalId\": \"in\",\n            \"description\": \"id Excepteur volu\",\n            \"dataSetId\": 6713921295205546,\n            \"metadata\": {},\n            \"source\": \"nostrud\",\n            \"labels\": [\n                {\n                    \"externalId\": \"dolor consequat fugiat nisi\"\n                },\n                {\n                    \"externalId\": \"dol\"\n                }\n            ]\n        }\n    ]\n}"
    }
  }
}