Chilkat Online Tools

VB.NET / Datadog API Collection / Get hourly usage for CSPM

Back to Collection Items

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

Dim http As New Chilkat.Http
Dim success As Boolean

Dim queryParams As New Chilkat.JsonObject
queryParams.UpdateString("start_hr","1991-02-02T07:22:34.204Z")
queryParams.UpdateString("end_hr","1991-02-02T07:22:34.204Z")

http.SetRequestHeader("Accept","application/json;datetime-format=rfc3339")


Dim resp As Chilkat.HttpResponse = http.QuickRequestParams("GET","https://api.app.ddog-gov.com/api/v1/usage/cspm",queryParams)
If (http.LastMethodSuccess = False) Then
    Debug.WriteLine(http.LastErrorText)
    Exit Sub
End If


Dim sbResponseBody As New Chilkat.StringBuilder
resp.GetBodySb(sbResponseBody)

Dim jResp As New Chilkat.JsonObject
jResp.LoadSb(sbResponseBody)
jResp.EmitCompact = False

Debug.WriteLine("Response Body:")
Debug.WriteLine(jResp.Emit())

Dim respStatusCode As Integer = resp.StatusCode
Debug.WriteLine("Response Status Code = " & respStatusCode)
If (respStatusCode >= 400) Then
    Debug.WriteLine("Response Header:")
    Debug.WriteLine(resp.Header)
    Debug.WriteLine("Failed.")

    Exit Sub
End If



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

' {
'   "usage": [
'     {
'       "aas_host_count": -80766489.56845878,
'       "aws_host_count": 17971624.141541034,
'       "azure_host_count": -63389392.498256706,
'       "compliance_host_count": -36217158.04911183,
'       "container_count": 11133759.007289752,
'       "gcp_host_count": 50901682.36994678,
'       "host_count": -47780028.372240715,
'       "hour": "2018-03-17T20:30:04.637Z",
'       "org_name": "mollit",
'       "public_id": "laborum aliqua ex irure"
'     },
'     {
'       "aas_host_count": -98211553.22625725,
'       "aws_host_count": -72710141.40577123,
'       "azure_host_count": 57237415.532838285,
'       "compliance_host_count": 955332.3425777704,
'       "container_count": -47480535.50305298,
'       "gcp_host_count": -39047011.60659756,
'       "host_count": 61940711.68366918,
'       "hour": "2009-11-04T13:20:03.152Z",
'       "org_name": "mollit adipisicing velit",
'       "public_id": "officia amet do in"
'     }
'   ]
' }

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



Dim aas_host_count As String
Dim aws_host_count As String
Dim azure_host_count As String
Dim compliance_host_count As String
Dim container_count As String
Dim gcp_host_count As String
Dim host_count As String
Dim hour As String
Dim org_name As String
Dim public_id As String

Dim i As Integer = 0
Dim count_i As Integer = jResp.SizeOfArray("usage")
While i < count_i
    jResp.I = i
    aas_host_count = jResp.StringOf("usage[i].aas_host_count")
    aws_host_count = jResp.StringOf("usage[i].aws_host_count")
    azure_host_count = jResp.StringOf("usage[i].azure_host_count")
    compliance_host_count = jResp.StringOf("usage[i].compliance_host_count")
    container_count = jResp.StringOf("usage[i].container_count")
    gcp_host_count = jResp.StringOf("usage[i].gcp_host_count")
    host_count = jResp.StringOf("usage[i].host_count")
    hour = jResp.StringOf("usage[i].hour")
    org_name = jResp.StringOf("usage[i].org_name")
    public_id = jResp.StringOf("usage[i].public_id")
    i = i + 1
End While

Curl Command

curl -G -d "start_hr=1991-02-02T07%3A22%3A34.204Z"
	-d "end_hr=1991-02-02T07%3A22%3A34.204Z"
	-H "Accept: application/json;datetime-format=rfc3339"
https://api.app.ddog-gov.com/api/v1/usage/cspm

Postman Collection Item JSON

{
  "name": "Get hourly usage for CSPM",
  "request": {
    "method": "GET",
    "header": [
      {
        "key": "Accept",
        "value": "application/json;datetime-format=rfc3339"
      }
    ],
    "url": {
      "raw": "{{baseUrl}}/api/v1/usage/cspm?start_hr=1991-02-02T07:22:34.204Z&end_hr=1991-02-02T07:22:34.204Z",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "api",
        "v1",
        "usage",
        "cspm"
      ],
      "query": [
        {
          "key": "start_hr",
          "value": "1991-02-02T07:22:34.204Z",
          "description": "(Required) Datetime in ISO-8601 format, UTC, precise to hour: `[YYYY-MM-DDThh]` for usage beginning at this hour."
        },
        {
          "key": "end_hr",
          "value": "1991-02-02T07:22:34.204Z",
          "description": "Datetime in ISO-8601 format, UTC, precise to hour: `[YYYY-MM-DDThh]` for usage ending\n**before** this hour."
        }
      ]
    },
    "description": "Get hourly usage for cloud security posture management (CSPM).\n**Note:** hourly usage data for all products is now available in the [Get hourly usage by product family API](https://docs.datadoghq.com/api/latest/usage-metering/#get-hourly-usage-by-product-family). Refer to [Migrating from the V1 Hourly Usage APIs to V2](https://docs.datadoghq.com/account_management/guide/hourly-usage-migration/) for the associated migration guide."
  },
  "response": [
    {
      "name": "OK",
      "originalRequest": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "application/json;datetime-format=rfc3339"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/api/v1/usage/cspm?start_hr=1991-02-02T07:22:34.204Z&end_hr=1991-02-02T07:22:34.204Z",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "usage",
            "cspm"
          ],
          "query": [
            {
              "key": "start_hr",
              "value": "1991-02-02T07:22:34.204Z",
              "description": "(Required) Datetime in ISO-8601 format, UTC, precise to hour: `[YYYY-MM-DDThh]` for usage beginning at this hour."
            },
            {
              "key": "end_hr",
              "value": "1991-02-02T07:22:34.204Z",
              "description": "Datetime in ISO-8601 format, UTC, precise to hour: `[YYYY-MM-DDThh]` for usage ending\n**before** this hour."
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json;datetime-format=rfc3339"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"usage\": [\n    {\n      \"aas_host_count\": -80766489.56845878,\n      \"aws_host_count\": 17971624.141541034,\n      \"azure_host_count\": -63389392.498256706,\n      \"compliance_host_count\": -36217158.04911183,\n      \"container_count\": 11133759.007289752,\n      \"gcp_host_count\": 50901682.36994678,\n      \"host_count\": -47780028.372240715,\n      \"hour\": \"2018-03-17T20:30:04.637Z\",\n      \"org_name\": \"mollit\",\n      \"public_id\": \"laborum aliqua ex irure\"\n    },\n    {\n      \"aas_host_count\": -98211553.22625725,\n      \"aws_host_count\": -72710141.40577123,\n      \"azure_host_count\": 57237415.532838285,\n      \"compliance_host_count\": 955332.3425777704,\n      \"container_count\": -47480535.50305298,\n      \"gcp_host_count\": -39047011.60659756,\n      \"host_count\": 61940711.68366918,\n      \"hour\": \"2009-11-04T13:20:03.152Z\",\n      \"org_name\": \"mollit adipisicing velit\",\n      \"public_id\": \"officia amet do in\"\n    }\n  ]\n}"
    },
    {
      "name": "Bad Request",
      "originalRequest": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "application/json;datetime-format=rfc3339"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/api/v1/usage/cspm?start_hr=1991-02-02T07:22:34.204Z&end_hr=1991-02-02T07:22:34.204Z",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "usage",
            "cspm"
          ],
          "query": [
            {
              "key": "start_hr",
              "value": "1991-02-02T07:22:34.204Z",
              "description": "(Required) Datetime in ISO-8601 format, UTC, precise to hour: `[YYYY-MM-DDThh]` for usage beginning at this hour."
            },
            {
              "key": "end_hr",
              "value": "1991-02-02T07:22:34.204Z",
              "description": "Datetime in ISO-8601 format, UTC, precise to hour: `[YYYY-MM-DDThh]` for usage ending\n**before** this hour."
            }
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json;datetime-format=rfc3339"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"Bad Request\",\n    \"Bad Request\"\n  ]\n}"
    },
    {
      "name": "Forbidden - User is not authorized",
      "originalRequest": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "application/json;datetime-format=rfc3339"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/api/v1/usage/cspm?start_hr=1991-02-02T07:22:34.204Z&end_hr=1991-02-02T07:22:34.204Z",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "usage",
            "cspm"
          ],
          "query": [
            {
              "key": "start_hr",
              "value": "1991-02-02T07:22:34.204Z",
              "description": "(Required) Datetime in ISO-8601 format, UTC, precise to hour: `[YYYY-MM-DDThh]` for usage beginning at this hour."
            },
            {
              "key": "end_hr",
              "value": "1991-02-02T07:22:34.204Z",
              "description": "Datetime in ISO-8601 format, UTC, precise to hour: `[YYYY-MM-DDThh]` for usage ending\n**before** this hour."
            }
          ]
        }
      },
      "status": "Forbidden",
      "code": 403,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json;datetime-format=rfc3339"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"Bad Request\",\n    \"Bad Request\"\n  ]\n}"
    },
    {
      "name": "Too many requests",
      "originalRequest": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "application/json;datetime-format=rfc3339"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/api/v1/usage/cspm?start_hr=1991-02-02T07:22:34.204Z&end_hr=1991-02-02T07:22:34.204Z",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v1",
            "usage",
            "cspm"
          ],
          "query": [
            {
              "key": "start_hr",
              "value": "1991-02-02T07:22:34.204Z",
              "description": "(Required) Datetime in ISO-8601 format, UTC, precise to hour: `[YYYY-MM-DDThh]` for usage beginning at this hour."
            },
            {
              "key": "end_hr",
              "value": "1991-02-02T07:22:34.204Z",
              "description": "Datetime in ISO-8601 format, UTC, precise to hour: `[YYYY-MM-DDThh]` for usage ending\n**before** this hour."
            }
          ]
        }
      },
      "status": "Too Many Requests",
      "code": 429,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json;datetime-format=rfc3339"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"Bad Request\",\n    \"Bad Request\"\n  ]\n}"
    }
  ]
}