Chilkat Online Tools

SQL Server / Anypoint Platform APIs / Create API from Exchange

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.

    -- {
    --   "endpoint": {
    --     "deploymentType": "CH",
    --     "isCloudHub": null,
    --     "muleVersion4OrAbove": null,
    --     "proxyUri": null,
    --     "referencesUserDomain": null,
    --     "responseTimeout": null,
    --     "type": "raml",
    --     "uri": null
    --   },
    --   "providerId": null,
    --   "instanceLabel": null,
    --   "spec": {
    --     "assetId": "hello-word",
    --     "groupId": "af696110-60f8-4a47-9367-e2464a468949",
    --     "version": "0.0.1"
    --   }
    -- }

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

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'endpoint.deploymentType', 'CH'
    EXEC sp_OAMethod @json, 'UpdateNull', @success OUT, 'endpoint.isCloudHub'
    EXEC sp_OAMethod @json, 'UpdateNull', @success OUT, 'endpoint.muleVersion4OrAbove'
    EXEC sp_OAMethod @json, 'UpdateNull', @success OUT, 'endpoint.proxyUri'
    EXEC sp_OAMethod @json, 'UpdateNull', @success OUT, 'endpoint.referencesUserDomain'
    EXEC sp_OAMethod @json, 'UpdateNull', @success OUT, 'endpoint.responseTimeout'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'endpoint.type', 'raml'
    EXEC sp_OAMethod @json, 'UpdateNull', @success OUT, 'endpoint.uri'
    EXEC sp_OAMethod @json, 'UpdateNull', @success OUT, 'providerId'
    EXEC sp_OAMethod @json, 'UpdateNull', @success OUT, 'instanceLabel'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'spec.assetId', 'hello-word'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'spec.groupId', 'af696110-60f8-4a47-9367-e2464a468949'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'spec.version', '0.0.1'

    -- Adds the "Authorization: Bearer {{token}}" header.
    EXEC sp_OASetProperty @http, 'AuthToken', '{{token}}'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://domain.com/apimanager/api/v1/organizations/{{organization_id}}/environments/{{environment_id}}/apis', '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 "Authorization: Bearer {{token}}"
	-d '{
   "endpoint":{
      "deploymentType":"CH",
      "isCloudHub":null,
      "muleVersion4OrAbove":null,
      "proxyUri":null,
      "referencesUserDomain":null,
      "responseTimeout":null,
      "type":"raml",
      "uri":null
   },
   "providerId":null,
   "instanceLabel":null,
   "spec":{
      "assetId":"hello-word",
      "groupId":"af696110-60f8-4a47-9367-e2464a468949",
      "version":"0.0.1"
   }
}'
https://domain.com/apimanager/api/v1/organizations/{{organization_id}}/environments/{{environment_id}}/apis

Postman Collection Item JSON

{
  "name": "Create API from Exchange",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Authorization",
        "value": "Bearer {{token}}",
        "type": "text"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\r\n   \"endpoint\":{\r\n      \"deploymentType\":\"CH\",\r\n      \"isCloudHub\":null,\r\n      \"muleVersion4OrAbove\":null,\r\n      \"proxyUri\":null,\r\n      \"referencesUserDomain\":null,\r\n      \"responseTimeout\":null,\r\n      \"type\":\"raml\",\r\n      \"uri\":null\r\n   },\r\n   \"providerId\":null,\r\n   \"instanceLabel\":null,\r\n   \"spec\":{\r\n      \"assetId\":\"hello-word\",\r\n      \"groupId\":\"af696110-60f8-4a47-9367-e2464a468949\",\r\n      \"version\":\"0.0.1\"\r\n   }\r\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{url}}/apimanager/api/v1/organizations/{{organization_id}}/environments/{{environment_id}}/apis",
      "host": [
        "{{url}}"
      ],
      "path": [
        "apimanager",
        "api",
        "v1",
        "organizations",
        "{{organization_id}}",
        "environments",
        "{{environment_id}}",
        "apis"
      ]
    }
  },
  "response": [
  ]
}