Chilkat Online Tools

SQL Server / Postman API / Create Webhook

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.

    -- {
    --   "webhook": {
    --     "name": "{{webhook_name}}",
    --     "collection": "{{collection_uid}}"
    --   }
    -- }

    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, 'webhook.name', '{{webhook_name}}'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'webhook.collection', '{{collection_uid}}'

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'X-API-Key', '{{postman_api_key}}'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://api.getpostman.com/webhooks?workspace={{workspace_id}}', '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-API-Key: {{postman_api_key}}"
	-d '{
    "webhook":{
        "name": "{{webhook_name}}",
        "collection": "{{collection_uid}}"
    }
}'
https://api.getpostman.com/webhooks?workspace={{workspace_id}}

Postman Collection Item JSON

{
  "name": "Create Webhook",
  "request": {
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"webhook\":{\n        \"name\": \"{{webhook_name}}\",\n        \"collection\": \"{{collection_uid}}\"\n    }\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "https://api.getpostman.com/webhooks?workspace={{workspace_id}}",
      "protocol": "https",
      "host": [
        "api",
        "getpostman",
        "com"
      ],
      "path": [
        "webhooks"
      ],
      "query": [
        {
          "key": "workspace",
          "value": "{{workspace_id}}"
        }
      ]
    },
    "description": "Create a webhook that triggers a collection with your custom payload.\n\nYou can specify the webhook name and the collection to trigger by using the following attributes of the `webhook` object on your JSON body:\n\n* `name`: the name of the webhook that you're creating.\n* `collection`: the UID of the collection that you want to trigger once this webhook is called.\n\nOnce created, the webhook URL can be retrieved by accessing the `webhookUrl` attribute of the `webhook` object on the response JSON payload."
  },
  "response": [
    {
      "name": "Create Webhook",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "x-workspace-id",
            "value": "c90a3482-f80d-43d6-9dfb-0830f5e43605",
            "type": "text"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"webhook\":{\n        \"name\": \"Sample Webhook\",\n        \"collection\": \"13223329-78f7e7dd-e9f4-4607-9610-098524f06589\"\n    }\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "https://api.getpostman.com/webhooks?workspace={{workspace_id}}",
          "protocol": "https",
          "host": [
            "api",
            "getpostman",
            "com"
          ],
          "path": [
            "webhooks"
          ],
          "query": [
            {
              "key": "workspace",
              "value": "{{workspace_id}}"
            }
          ]
        }
      },
      "_postman_previewlanguage": "json",
      "header": null,
      "cookie": [
      ],
      "body": "{\n    \"webhook\": {\n        \"id\": \"1eba1c36-c343-4eb0-a25a-37117650e628\",\n        \"name\": \"Sample Webhook\",\n        \"collection\": \"13323329-78f7e7dd-e9f4-4607-9610-098524f06589\",\n        \"webhookUrl\": \"https://example.com/webhookUrl\",\n        \"uid\": \"13323329-1eba1c36-c343-4eb0-a25a-37117650e628\"\n    }\n}"
    }
  ]
}