Chilkat Online Tools

PureBasic / Squadcast API V3 / Create Service

Back to Collection Items

IncludeFile "CkJsonObject.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkHttpResponse.pb"

Procedure ChilkatExample()

    ; This example assumes the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    ; 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": "<string>",
    ;   "description": "<string>",
    ;   "escalation_policy_id": "<string>",
    ;   "email_prefix": "<string>"
    ; }

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json,"name","<string>")
    CkJsonObject::ckUpdateString(json,"description","<string>")
    CkJsonObject::ckUpdateString(json,"escalation_policy_id","<string>")
    CkJsonObject::ckUpdateString(json,"email_prefix","<string>")

    CkHttp::ckSetRequestHeader(http,"Authorization","")
    CkHttp::ckSetRequestHeader(http,"Content-Type","application/json")

    resp.i = CkHttp::ckPostJson3(http,"https://api.squadcast.com/v3/services","application/json",json)
    If CkHttp::ckLastMethodSuccess(http) = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttpResponse::ckGetBodySb(resp,sbResponseBody)

    jResp.i = CkJsonObject::ckCreate()
    If jResp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(jResp,sbResponseBody)
    CkJsonObject::setCkEmitCompact(jResp, 0)

    Debug "Response Body:"
    Debug CkJsonObject::ckEmit(jResp)

    respStatusCode.i = CkHttpResponse::ckStatusCode(resp)
    Debug "Response Status Code = " + Str(respStatusCode)
    If respStatusCode >= 400
        Debug "Response Header:"
        Debug CkHttpResponse::ckHeader(resp)
        Debug "Failed."
        CkHttpResponse::ckDispose(resp)

        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbResponseBody)
        CkJsonObject::ckDispose(jResp)
        ProcedureReturn
    EndIf

    CkHttpResponse::ckDispose(resp)

    ; Sample JSON response:
    ; (Sample code for parsing the JSON response is shown below)

    ; {
    ;   "data": {
    ;     "id": "5e8edb24668e003cb0b18ba1",
    ;     "name": "Payment API Service",
    ;     "slug": "payment-api-service",
    ;     "email": "unique_string@gopherhut.incidents.squadcast.com",
    ;     "escalation_policy_id": "5d81d9407000fb6b9def7e33",
    ;     "organization_id": "5d81d9187000fb6b9def7e32",
    ;     "api_key": "2f81ac8b2362990dd220f8bb4f7cd30ccc3dac43",
    ;     "description": "Payment API Service monitor",
    ;     "depends": null,
    ;     "escalation_policy": {
    ;       "id": "5d81d9407000fb6b9def7e33",
    ;       "name": "Example Escalation Policy",
    ;       "description": "On-Boarding Example",
    ;       "slug": "example-escalation-policy"
    ;     }
    ;   }
    ; }

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

    Id.s = CkJsonObject::ckStringOf(jResp,"data.id")
    Name.s = CkJsonObject::ckStringOf(jResp,"data.name")
    Slug.s = CkJsonObject::ckStringOf(jResp,"data.slug")
    v_Email.s = CkJsonObject::ckStringOf(jResp,"data.email")
    Escalation_policy_id.s = CkJsonObject::ckStringOf(jResp,"data.escalation_policy_id")
    Organization_id.s = CkJsonObject::ckStringOf(jResp,"data.organization_id")
    Api_key.s = CkJsonObject::ckStringOf(jResp,"data.api_key")
    Description.s = CkJsonObject::ckStringOf(jResp,"data.description")
    Depends.s = CkJsonObject::ckStringOf(jResp,"data.depends")
    Escalation_policyId.s = CkJsonObject::ckStringOf(jResp,"data.escalation_policy.id")
    Escalation_policyName.s = CkJsonObject::ckStringOf(jResp,"data.escalation_policy.name")
    Escalation_policyDescription.s = CkJsonObject::ckStringOf(jResp,"data.escalation_policy.description")
    Escalation_policySlug.s = CkJsonObject::ckStringOf(jResp,"data.escalation_policy.slug")


    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(json)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jResp)


    ProcedureReturn
EndProcedure

Curl Command

curl -X POST
	-H "Authorization: "
	-H "Content-Type: application/json"
	-d '{
    "name": "<string>",
    "description": "<string>",
    "escalation_policy_id": "<string>",
    "email_prefix": "<string>"
}'
https://api.squadcast.com/v3/services

Postman Collection Item JSON

{
  "name": "Create Service",
  "request": {
    "auth": {
      "type": "noauth"
    },
    "method": "POST",
    "header": [
      {
        "key": "Authorization",
        "value": ""
      },
      {
        "key": "Content-Type",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"name\": \"<string>\",\n    \"description\": \"<string>\",\n    \"escalation_policy_id\": \"<string>\",\n    \"email_prefix\": \"<string>\"\n}"
    },
    "url": {
      "raw": "{{baseUrl}}/services",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "services"
      ]
    },
    "description": "This endpoint is used to create a service.\nRequires `access_token` as a `Bearer {{token}}` in the `Authorization` header with `service-write` scope.\n"
  },
  "response": [
    {
      "name": "Unauthorized",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"name\": \"auxpkg create 1\",\n    \"escalation_policy_id\": \"5d81d9407000fb6b9def7e33\",\n    \"description\": \"service api\",\n    \"email_prefix\": \"\"\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/services",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "services"
          ]
        }
      },
      "status": "Unauthorized",
      "code": 401,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"meta\": {\n  \"status\": \"<integer>\",\n  \"error_message\": \"<string>\"\n }\n}"
    },
    {
      "name": "Created",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"name\": \"auxpkg create 1\",\n    \"escalation_policy_id\": \"5d81d9407000fb6b9def7e33\",\n    \"description\": \"service api\",\n    \"email_prefix\": \"\"\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/services",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "services"
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"data\": {\n        \"id\": \"5e8edb24668e003cb0b18ba1\",\n        \"name\": \"Payment API Service\",\n        \"slug\": \"payment-api-service\",\n        \"email\": \"unique_string@gopherhut.incidents.squadcast.com\",\n        \"escalation_policy_id\": \"5d81d9407000fb6b9def7e33\",\n        \"organization_id\": \"5d81d9187000fb6b9def7e32\",\n        \"api_key\": \"2f81ac8b2362990dd220f8bb4f7cd30ccc3dac43\",\n        \"description\": \"Payment API Service monitor\",\n        \"depends\": null,\n        \"escalation_policy\": {\n            \"id\": \"5d81d9407000fb6b9def7e33\",\n            \"name\": \"Example Escalation Policy\",\n            \"description\": \"On-Boarding Example\",\n            \"slug\": \"example-escalation-policy\"\n        }\n    }\n}"
    },
    {
      "name": "Internal Server Error",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"name\": \"auxpkg create 1\",\n    \"escalation_policy_id\": \"5d81d9407000fb6b9def7e33\",\n    \"description\": \"service api\",\n    \"email_prefix\": \"\"\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/services",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "services"
          ]
        }
      },
      "status": "Internal Server Error",
      "code": 500,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"meta\": {\n  \"status\": \"<integer>\",\n  \"error_message\": \"<string>\"\n }\n}"
    },
    {
      "name": "Conflict",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"name\": \"auxpkg create 1\",\n    \"escalation_policy_id\": \"5d81d9407000fb6b9def7e33\",\n    \"description\": \"service api\",\n    \"email_prefix\": \"\"\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/services",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "services"
          ]
        }
      },
      "status": "Conflict",
      "code": 409,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"meta\": {\n  \"status\": \"<integer>\",\n  \"error_message\": \"<string>\"\n }\n}"
    },
    {
      "name": "Bad Request",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"name\": \"auxpkg create 1\",\n    \"escalation_policy_id\": \"5d81d9407000fb6b9def7e33\",\n    \"description\": \"service api\",\n    \"email_prefix\": \"\"\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/services",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "services"
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"meta\": {\n  \"status\": \"<integer>\",\n  \"error_message\": \"<string>\"\n }\n}"
    },
    {
      "name": "Forbidden",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"name\": \"auxpkg create 1\",\n    \"escalation_policy_id\": \"5d81d9407000fb6b9def7e33\",\n    \"description\": \"service api\",\n    \"email_prefix\": \"\"\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/services",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "services"
          ]
        }
      },
      "status": "Forbidden",
      "code": 403,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"meta\": {\n  \"status\": \"<integer>\",\n  \"error_message\": \"<string>\"\n }\n}"
    },
    {
      "name": "Payment Required",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Authorization",
            "value": ""
          },
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"name\": \"<string>\",\n    \"description\": \"<string>\",\n    \"escalation_policy_id\": \"<string>\",\n    \"email_prefix\": \"<string>\"\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/services",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "services"
          ]
        }
      },
      "status": "Payment Required",
      "code": 402,
      "_postman_previewlanguage": "json",
      "header": null,
      "cookie": [
      ],
      "body": "{\n\t\"meta\": {\n\t\t\"status\": 402,\n\t\t\"error_message\": \"organization has crossed the plan limits for 'service'\",\n\t\t\"error_details\": {\n\t\t\t\"code\": \"E0402\",\n\t\t\t\"description\": \"You've exceeded the number of services under your current plan. To continue, please upgrade.\",\n\t\t\t\"link\": \"https://support.squadcast.com/docs/error-codes-and-what-they-mean\"\n\t\t}\n\t}\n}"
    }
  ]
}