Chilkat Online Tools

SQL Server / CleverTap API / Upload Device Tokens API GCM/FCM/APNS/CHROME

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.

    -- {
    --   "d": [
    --     {
    --       "type": "token",
    --       "tokenData": {
    --         "id": "frfsgvrwe:APfdsafsgdfsgghfgfgjkhfsfgdhjhbvcvnetry767456fxsasdf",
    --         "type": "chrome",
    --         "keys": {
    --           "p256dh": "BLc4xRzKlKORKWlbdgFaBrrPK3ydWAHo4M0gs0i1oEKgPpWC5cW8OCzVrOQRv-1npXRWk8udnW3oYhIO4475rds=",
    --           "auth": "5I2Bu2oKdyy9CwL8QVF0NQ=="
    --         }
    --       },
    --       "objectId": "QBOpVJZmKilRAzfaiVS86Tovxm75lHxg"
    --     },
    --     {
    --       "type": "token",
    --       "tokenData": {
    --         "id": "dYfuuBmHLGI:APA91bEfAq1b6NAmz0uhUbVxwSLqgKF25zDb7vhgajzS-bOAEUKekH_jbzO5oU1Qip-ZLvDpwKccxAxVNjC3rUUJnFTKDUiBcF9AtuG03_PRfjoUxLKHMR9qykK22SiubrLirNzQtNnO",
    --         "type": "fcm"
    --       },
    --       "objectId": "__gc4grg4053N1eYkW6OBH71jhFev7b1iP"
    --     },
    --     {
    --       "type": "token",
    --       "tokenData": {
    --         "id": "frfsgvrwe:APfdsafsgdfsgghfgfgjkhfsfgdhjhbvcvnetry767456fxsasdf",
    --         "type": "gcm"
    --       },
    --       "objectId": "__QlXHaDHBZxZPLEMpPo6VvwdZ7FnbGvWA"
    --     },
    --     {
    --       "type": "token",
    --       "tokenData": {
    --         "id": "frfsgvrwe:APfdsafsgdfsgghfgfgjkhfsfgdhjhbvcvnetry767456fxsasdf",
    --         "type": "apns"
    --       },
    --       "objectId": "-HwB9gZWb3RcKhXCQ222FhAhkjeYQs0Hj"
    --     },
    --     {
    --       "type": "token",
    --       "tokenData": {
    --         "id": "frfsgvrwe:APfdsafsgdfsgghfgfgjkhfsfgdhjhbvcvnetry767456fxsasdf",
    --         "type": "wns"
    --       },
    --       "objectId": "~9XrS4Dy6GsnDbdX98Ijs63kEtJDQbhJA"
    --     },
    --     {
    --       "type": "token",
    --       "tokenData": {
    --         "id": "frfsgvrwe:APfdsafsgdfsgghfgfgjkhfsfgdhjhbvcvnetry767456fxsasdf",
    --         "type": "mpns"
    --       },
    --       "objectId": "~y9MqjkDGRfbzwwH8fZI6LIXgTKaEPxHr"
    --     }
    --   ]
    -- }

    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, 'd[0].type', 'token'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'd[0].tokenData.id', 'frfsgvrwe:APfdsafsgdfsgghfgfgjkhfsfgdhjhbvcvnetry767456fxsasdf'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'd[0].tokenData.type', 'chrome'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'd[0].tokenData.keys.p256dh', 'BLc4xRzKlKORKWlbdgFaBrrPK3ydWAHo4M0gs0i1oEKgPpWC5cW8OCzVrOQRv-1npXRWk8udnW3oYhIO4475rds='
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'd[0].tokenData.keys.auth', '5I2Bu2oKdyy9CwL8QVF0NQ=='
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'd[0].objectId', 'QBOpVJZmKilRAzfaiVS86Tovxm75lHxg'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'd[1].type', 'token'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'd[1].tokenData.id', 'dYfuuBmHLGI:APA91bEfAq1b6NAmz0uhUbVxwSLqgKF25zDb7vhgajzS-bOAEUKekH_jbzO5oU1Qip-ZLvDpwKccxAxVNjC3rUUJnFTKDUiBcF9AtuG03_PRfjoUxLKHMR9qykK22SiubrLirNzQtNnO'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'd[1].tokenData.type', 'fcm'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'd[1].objectId', '__gc4grg4053N1eYkW6OBH71jhFev7b1iP'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'd[2].type', 'token'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'd[2].tokenData.id', 'frfsgvrwe:APfdsafsgdfsgghfgfgjkhfsfgdhjhbvcvnetry767456fxsasdf'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'd[2].tokenData.type', 'gcm'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'd[2].objectId', '__QlXHaDHBZxZPLEMpPo6VvwdZ7FnbGvWA'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'd[3].type', 'token'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'd[3].tokenData.id', 'frfsgvrwe:APfdsafsgdfsgghfgfgjkhfsfgdhjhbvcvnetry767456fxsasdf'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'd[3].tokenData.type', 'apns'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'd[3].objectId', '-HwB9gZWb3RcKhXCQ222FhAhkjeYQs0Hj'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'd[4].type', 'token'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'd[4].tokenData.id', 'frfsgvrwe:APfdsafsgdfsgghfgfgjkhfsfgdhjhbvcvnetry767456fxsasdf'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'd[4].tokenData.type', 'wns'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'd[4].objectId', '~9XrS4Dy6GsnDbdX98Ijs63kEtJDQbhJA'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'd[5].type', 'token'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'd[5].tokenData.id', 'frfsgvrwe:APfdsafsgdfsgghfgfgjkhfsfgdhjhbvcvnetry767456fxsasdf'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'd[5].tokenData.type', 'mpns'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'd[5].objectId', '~y9MqjkDGRfbzwwH8fZI6LIXgTKaEPxHr'

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'X-CleverTap-Passcode', '{{passcode}}'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'X-CleverTap-Account-Id', '{{id}}'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://api.clevertap.com/1/upload', '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 "X-CleverTap-Account-Id: {{id}}"
	-H "X-CleverTap-Passcode: {{passcode}}"
	-H "Content-Type: application/json"
	-d '{
    "d": [
        {
            "type": "token",
            "tokenData": {
                "id": "frfsgvrwe:APfdsafsgdfsgghfgfgjkhfsfgdhjhbvcvnetry767456fxsasdf",
                "type": "chrome",
                "keys": {
                    "p256dh": "BLc4xRzKlKORKWlbdgFaBrrPK3ydWAHo4M0gs0i1oEKgPpWC5cW8OCzVrOQRv-1npXRWk8udnW3oYhIO4475rds=",
                    "auth": "5I2Bu2oKdyy9CwL8QVF0NQ=="
                }
            },
            "objectId": "QBOpVJZmKilRAzfaiVS86Tovxm75lHxg"
        },
        {
            "type": "token",
            "tokenData": {
                "id": "dYfuuBmHLGI:APA91bEfAq1b6NAmz0uhUbVxwSLqgKF25zDb7vhgajzS-bOAEUKekH_jbzO5oU1Qip-ZLvDpwKccxAxVNjC3rUUJnFTKDUiBcF9AtuG03_PRfjoUxLKHMR9qykK22SiubrLirNzQtNnO",
                "type": "fcm"
            },
            "objectId": "__gc4grg4053N1eYkW6OBH71jhFev7b1iP"
        },
        {
            "type": "token",
            "tokenData": {
                "id": "frfsgvrwe:APfdsafsgdfsgghfgfgjkhfsfgdhjhbvcvnetry767456fxsasdf",
                "type": "gcm"
            },
            "objectId": "__QlXHaDHBZxZPLEMpPo6VvwdZ7FnbGvWA"
        },
        {
            "type": "token",
            "tokenData": {
                "id": "frfsgvrwe:APfdsafsgdfsgghfgfgjkhfsfgdhjhbvcvnetry767456fxsasdf",
                "type": "apns"
            },
            "objectId": "-HwB9gZWb3RcKhXCQ222FhAhkjeYQs0Hj"
        },
        {
            "type": "token",
            "tokenData": {
                "id": "frfsgvrwe:APfdsafsgdfsgghfgfgjkhfsfgdhjhbvcvnetry767456fxsasdf",
                "type": "wns"
            },
            "objectId": "~9XrS4Dy6GsnDbdX98Ijs63kEtJDQbhJA"
        },
        {
            "type": "token",
            "tokenData": {
                "id": "frfsgvrwe:APfdsafsgdfsgghfgfgjkhfsfgdhjhbvcvnetry767456fxsasdf",
                "type": "mpns"
            },
            "objectId": "~y9MqjkDGRfbzwwH8fZI6LIXgTKaEPxHr"
        }
    ]
}'
https://api.clevertap.com/1/upload

Postman Collection Item JSON

{
  "name": "Upload Device Tokens API GCM/FCM/APNS/CHROME",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "X-CleverTap-Account-Id",
        "value": "{{id}}"
      },
      {
        "key": "X-CleverTap-Passcode",
        "value": "{{passcode}}"
      },
      {
        "key": "Content-Type",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"d\": [\n        {\n            \"type\": \"token\",\n            \"tokenData\": {\n                \"id\": \"frfsgvrwe:APfdsafsgdfsgghfgfgjkhfsfgdhjhbvcvnetry767456fxsasdf\",\n                \"type\": \"chrome\",\n                \"keys\": {\n                    \"p256dh\": \"BLc4xRzKlKORKWlbdgFaBrrPK3ydWAHo4M0gs0i1oEKgPpWC5cW8OCzVrOQRv-1npXRWk8udnW3oYhIO4475rds=\",\n                    \"auth\": \"5I2Bu2oKdyy9CwL8QVF0NQ==\"\n                }\n            },\n            \"objectId\": \"QBOpVJZmKilRAzfaiVS86Tovxm75lHxg\"\n        },\n        {\n            \"type\": \"token\",\n            \"tokenData\": {\n                \"id\": \"dYfuuBmHLGI:APA91bEfAq1b6NAmz0uhUbVxwSLqgKF25zDb7vhgajzS-bOAEUKekH_jbzO5oU1Qip-ZLvDpwKccxAxVNjC3rUUJnFTKDUiBcF9AtuG03_PRfjoUxLKHMR9qykK22SiubrLirNzQtNnO\",\n                \"type\": \"fcm\"\n            },\n            \"objectId\": \"__gc4grg4053N1eYkW6OBH71jhFev7b1iP\"\n        },\n        {\n            \"type\": \"token\",\n            \"tokenData\": {\n                \"id\": \"frfsgvrwe:APfdsafsgdfsgghfgfgjkhfsfgdhjhbvcvnetry767456fxsasdf\",\n                \"type\": \"gcm\"\n            },\n            \"objectId\": \"__QlXHaDHBZxZPLEMpPo6VvwdZ7FnbGvWA\"\n        },\n        {\n            \"type\": \"token\",\n            \"tokenData\": {\n                \"id\": \"frfsgvrwe:APfdsafsgdfsgghfgfgjkhfsfgdhjhbvcvnetry767456fxsasdf\",\n                \"type\": \"apns\"\n            },\n            \"objectId\": \"-HwB9gZWb3RcKhXCQ222FhAhkjeYQs0Hj\"\n        },\n        {\n            \"type\": \"token\",\n            \"tokenData\": {\n                \"id\": \"frfsgvrwe:APfdsafsgdfsgghfgfgjkhfsfgdhjhbvcvnetry767456fxsasdf\",\n                \"type\": \"wns\"\n            },\n            \"objectId\": \"~9XrS4Dy6GsnDbdX98Ijs63kEtJDQbhJA\"\n        },\n        {\n            \"type\": \"token\",\n            \"tokenData\": {\n                \"id\": \"frfsgvrwe:APfdsafsgdfsgghfgfgjkhfsfgdhjhbvcvnetry767456fxsasdf\",\n                \"type\": \"mpns\"\n            },\n            \"objectId\": \"~y9MqjkDGRfbzwwH8fZI6LIXgTKaEPxHr\"\n        }\n    ]\n}"
    },
    "url": {
      "raw": "https://api.clevertap.com/1/upload",
      "protocol": "https",
      "host": [
        "api",
        "clevertap",
        "com"
      ],
      "path": [
        "1",
        "upload"
      ]
    }
  },
  "response": [
  ]
}