Chilkat Online Tools

Node.js / Datadog API Collection / Query timeseries data across multiple products

Back to Collection Items

var os = require('os');
if (os.platform() == 'win32') {  
    if (os.arch() == 'ia32') {
        var chilkat = require('@chilkat/ck-node21-win-ia32');
    } else {
        var chilkat = require('@chilkat/ck-node21-win64'); 
    }
} else if (os.platform() == 'linux') {
    if (os.arch() == 'arm') {
        var chilkat = require('@chilkat/ck-node21-arm');
    } else if (os.arch() == 'x86') {
        var chilkat = require('@chilkat/ck-node21-linux32');
    } else {
        var chilkat = require('@chilkat/ck-node21-linux64');
    }
} else if (os.platform() == 'darwin') {
    if (os.arch() == 'arm64') {
        var chilkat = require('@chilkat/ck-node21-mac-m1');
    } else {
        var chilkat = require('@chilkat/ck-node21-macosx');
    }
}


function chilkatExample() {

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

    var http = new chilkat.Http();
    var success;

    // Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // {
    //   "data": {
    //     "type": "timeseries_request",
    //     "attributes": {
    //       "to": "<long>",
    //       "from": "<long>",
    //       "queries": [
    //         {
    //           "data_source": "metrics",
    //           "query": "<string>",
    //           "name": "<string>"
    //         },
    //         {
    //           "data_source": "metrics",
    //           "query": "<string>",
    //           "name": "<string>"
    //         }
    //       ],
    //       "formulas": [
    //         {
    //           "formula": "<string>",
    //           "limit": {
    //             "count": "<integer>",
    //             "order": "desc"
    //           }
    //         },
    //         {
    //           "formula": "<string>",
    //           "limit": {
    //             "count": "<integer>",
    //             "order": "desc"
    //           }
    //         }
    //       ],
    //       "interval": "<long>"
    //     }
    //   }
    // }

    var json = new chilkat.JsonObject();
    json.UpdateString("data.type","timeseries_request");
    json.UpdateString("data.attributes.to","<long>");
    json.UpdateString("data.attributes.from","<long>");
    json.UpdateString("data.attributes.queries[0].data_source","metrics");
    json.UpdateString("data.attributes.queries[0].query","<string>");
    json.UpdateString("data.attributes.queries[0].name","<string>");
    json.UpdateString("data.attributes.queries[1].data_source","metrics");
    json.UpdateString("data.attributes.queries[1].query","<string>");
    json.UpdateString("data.attributes.queries[1].name","<string>");
    json.UpdateString("data.attributes.formulas[0].formula","<string>");
    json.UpdateString("data.attributes.formulas[0].limit.count","<integer>");
    json.UpdateString("data.attributes.formulas[0].limit.order","desc");
    json.UpdateString("data.attributes.formulas[1].formula","<string>");
    json.UpdateString("data.attributes.formulas[1].limit.count","<integer>");
    json.UpdateString("data.attributes.formulas[1].limit.order","desc");
    json.UpdateString("data.attributes.interval","<long>");

    http.SetRequestHeader("Content-Type","application/json");
    http.SetRequestHeader("Accept","application/json");

    // resp: HttpResponse
    var resp = http.PostJson3("https://api.app.ddog-gov.com/api/v2/query/timeseries","application/json",json);
    if (http.LastMethodSuccess == false) {
        console.log(http.LastErrorText);
        return;
    }

    var sbResponseBody = new chilkat.StringBuilder();
    resp.GetBodySb(sbResponseBody);

    var jResp = new chilkat.JsonObject();
    jResp.LoadSb(sbResponseBody);
    jResp.EmitCompact = false;

    console.log("Response Body:");
    console.log(jResp.Emit());

    var respStatusCode = resp.StatusCode;
    console.log("Response Status Code = " + respStatusCode);
    if (respStatusCode >= 400) {
        console.log("Response Header:");
        console.log(resp.Header);
        console.log("Failed.");

        return;
    }

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

    // {
    //   "data": {
    //     "attributes": {
    //       "series": [
    //         {
    //           "group_tags": [
    //             "<string>",
    //             "<string>"
    //           ],
    //           "query_index": "<integer>",
    //           "unit": [
    //             {
    //               "family": "<string>",
    //               "name": "<string>",
    //               "plural": "<string>",
    //               "scale_factor": "<double>",
    //               "short_name": "<string>"
    //             },
    //             {
    //               "family": "<string>",
    //               "name": "<string>",
    //               "plural": "<string>",
    //               "scale_factor": "<double>",
    //               "short_name": "<string>"
    //             }
    //           ]
    //         },
    //         {
    //           "group_tags": [
    //             "<string>",
    //             "<string>"
    //           ],
    //           "query_index": "<integer>",
    //           "unit": [
    //             {
    //               "family": "<string>",
    //               "name": "<string>",
    //               "plural": "<string>",
    //               "scale_factor": "<double>",
    //               "short_name": "<string>"
    //             },
    //             {
    //               "family": "<string>",
    //               "name": "<string>",
    //               "plural": "<string>",
    //               "scale_factor": "<double>",
    //               "short_name": "<string>"
    //             }
    //           ]
    //         }
    //       ],
    //       "times": [
    //         "<long>",
    //         "<long>"
    //       ],
    //       "values": [
    //         [
    //           "<double>",
    //           "<double>"
    //         ],
    //         [
    //           "<double>",
    //           "<double>"
    //         ]
    //       ]
    //     },
    //     "type": "timeseries_response"
    //   },
    //   "errors": "<string>"
    // }

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

    var query_index;
    var j;
    var count_j;
    var strVal;
    var family;
    var name;
    var plural;
    var scale_factor;
    var short_name;

    var v_Type = jResp.StringOf("data.type");
    var errors = jResp.StringOf("errors");
    var i = 0;
    var count_i = jResp.SizeOfArray("data.attributes.series");
    while (i < count_i) {
        jResp.I = i;
        query_index = jResp.StringOf("data.attributes.series[i].query_index");
        j = 0;
        count_j = jResp.SizeOfArray("data.attributes.series[i].group_tags");
        while (j < count_j) {
            jResp.J = j;
            strVal = jResp.StringOf("data.attributes.series[i].group_tags[j]");
            j = j+1;
        }

        j = 0;
        count_j = jResp.SizeOfArray("data.attributes.series[i].unit");
        while (j < count_j) {
            jResp.J = j;
            family = jResp.StringOf("data.attributes.series[i].unit[j].family");
            name = jResp.StringOf("data.attributes.series[i].unit[j].name");
            plural = jResp.StringOf("data.attributes.series[i].unit[j].plural");
            scale_factor = jResp.StringOf("data.attributes.series[i].unit[j].scale_factor");
            short_name = jResp.StringOf("data.attributes.series[i].unit[j].short_name");
            j = j+1;
        }

        i = i+1;
    }

    i = 0;
    count_i = jResp.SizeOfArray("data.attributes.times");
    while (i < count_i) {
        jResp.I = i;
        strVal = jResp.StringOf("data.attributes.times[i]");
        i = i+1;
    }

    i = 0;
    count_i = jResp.SizeOfArray("data.attributes.values");
    while (i < count_i) {
        jResp.I = i;
        j = 0;
        count_j = jResp.SizeOfArray("data.attributes.values[i]");
        while (j < count_j) {
            jResp.J = j;
            strVal = jResp.StringOf("data.attributes.values[i][j]");
            j = j+1;
        }

        i = i+1;
    }


}

chilkatExample();

Curl Command

curl -X POST
	-H "Content-Type: application/json"
	-H "Accept: application/json"
	-d '{
  "data": {
    "type": "timeseries_request",
    "attributes": {
      "to": "<long>",
      "from": "<long>",
      "queries": [
        {
          "data_source": "metrics",
          "query": "<string>",
          "name": "<string>"
        },
        {
          "data_source": "metrics",
          "query": "<string>",
          "name": "<string>"
        }
      ],
      "formulas": [
        {
          "formula": "<string>",
          "limit": {
            "count": "<integer>",
            "order": "desc"
          }
        },
        {
          "formula": "<string>",
          "limit": {
            "count": "<integer>",
            "order": "desc"
          }
        }
      ],
      "interval": "<long>"
    }
  }
}'
https://api.app.ddog-gov.com/api/v2/query/timeseries

Postman Collection Item JSON

{
  "name": "Query timeseries data across multiple products",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      },
      {
        "key": "Accept",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"data\": {\n    \"type\": \"timeseries_request\",\n    \"attributes\": {\n      \"to\": \"<long>\",\n      \"from\": \"<long>\",\n      \"queries\": [\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"name\": \"<string>\"\n        },\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"name\": \"<string>\"\n        }\n      ],\n      \"formulas\": [\n        {\n          \"formula\": \"<string>\",\n          \"limit\": {\n            \"count\": \"<integer>\",\n            \"order\": \"desc\"\n          }\n        },\n        {\n          \"formula\": \"<string>\",\n          \"limit\": {\n            \"count\": \"<integer>\",\n            \"order\": \"desc\"\n          }\n        }\n      ],\n      \"interval\": \"<long>\"\n    }\n  }\n}",
      "options": {
        "raw": {
          "headerFamily": "json",
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{baseUrl}}/api/v2/query/timeseries",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "api",
        "v2",
        "query",
        "timeseries"
      ]
    },
    "description": "Query timeseries data across various data sources and\nprocess the data by applying formulas and functions."
  },
  "response": [
    {
      "name": "OK",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"data\": {\n    \"type\": \"timeseries_request\",\n    \"attributes\": {\n      \"to\": \"<long>\",\n      \"from\": \"<long>\",\n      \"queries\": [\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"name\": \"<string>\"\n        },\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"name\": \"<string>\"\n        }\n      ],\n      \"formulas\": [\n        {\n          \"formula\": \"<string>\",\n          \"limit\": {\n            \"count\": \"<integer>\",\n            \"order\": \"desc\"\n          }\n        },\n        {\n          \"formula\": \"<string>\",\n          \"limit\": {\n            \"count\": \"<integer>\",\n            \"order\": \"desc\"\n          }\n        }\n      ],\n      \"interval\": \"<long>\"\n    }\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v2/query/timeseries",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "query",
            "timeseries"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"data\": {\n    \"attributes\": {\n      \"series\": [\n        {\n          \"group_tags\": [\n            \"<string>\",\n            \"<string>\"\n          ],\n          \"query_index\": \"<integer>\",\n          \"unit\": [\n            {\n              \"family\": \"<string>\",\n              \"name\": \"<string>\",\n              \"plural\": \"<string>\",\n              \"scale_factor\": \"<double>\",\n              \"short_name\": \"<string>\"\n            },\n            {\n              \"family\": \"<string>\",\n              \"name\": \"<string>\",\n              \"plural\": \"<string>\",\n              \"scale_factor\": \"<double>\",\n              \"short_name\": \"<string>\"\n            }\n          ]\n        },\n        {\n          \"group_tags\": [\n            \"<string>\",\n            \"<string>\"\n          ],\n          \"query_index\": \"<integer>\",\n          \"unit\": [\n            {\n              \"family\": \"<string>\",\n              \"name\": \"<string>\",\n              \"plural\": \"<string>\",\n              \"scale_factor\": \"<double>\",\n              \"short_name\": \"<string>\"\n            },\n            {\n              \"family\": \"<string>\",\n              \"name\": \"<string>\",\n              \"plural\": \"<string>\",\n              \"scale_factor\": \"<double>\",\n              \"short_name\": \"<string>\"\n            }\n          ]\n        }\n      ],\n      \"times\": [\n        \"<long>\",\n        \"<long>\"\n      ],\n      \"values\": [\n        [\n          \"<double>\",\n          \"<double>\"\n        ],\n        [\n          \"<double>\",\n          \"<double>\"\n        ]\n      ]\n    },\n    \"type\": \"timeseries_response\"\n  },\n  \"errors\": \"<string>\"\n}"
    },
    {
      "name": "Bad Request",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"data\": {\n    \"type\": \"timeseries_request\",\n    \"attributes\": {\n      \"to\": \"<long>\",\n      \"from\": \"<long>\",\n      \"queries\": [\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"name\": \"<string>\"\n        },\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"name\": \"<string>\"\n        }\n      ],\n      \"formulas\": [\n        {\n          \"formula\": \"<string>\",\n          \"limit\": {\n            \"count\": \"<integer>\",\n            \"order\": \"desc\"\n          }\n        },\n        {\n          \"formula\": \"<string>\",\n          \"limit\": {\n            \"count\": \"<integer>\",\n            \"order\": \"desc\"\n          }\n        }\n      ],\n      \"interval\": \"<long>\"\n    }\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v2/query/timeseries",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "query",
            "timeseries"
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"<string>\",\n    \"<string>\"\n  ]\n}"
    },
    {
      "name": "Unauthorized",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"data\": {\n    \"type\": \"timeseries_request\",\n    \"attributes\": {\n      \"to\": \"<long>\",\n      \"from\": \"<long>\",\n      \"queries\": [\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"name\": \"<string>\"\n        },\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"name\": \"<string>\"\n        }\n      ],\n      \"formulas\": [\n        {\n          \"formula\": \"<string>\",\n          \"limit\": {\n            \"count\": \"<integer>\",\n            \"order\": \"desc\"\n          }\n        },\n        {\n          \"formula\": \"<string>\",\n          \"limit\": {\n            \"count\": \"<integer>\",\n            \"order\": \"desc\"\n          }\n        }\n      ],\n      \"interval\": \"<long>\"\n    }\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v2/query/timeseries",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "query",
            "timeseries"
          ]
        }
      },
      "status": "Unauthorized",
      "code": 401,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"<string>\",\n    \"<string>\"\n  ]\n}"
    },
    {
      "name": "Forbidden",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"data\": {\n    \"type\": \"timeseries_request\",\n    \"attributes\": {\n      \"to\": \"<long>\",\n      \"from\": \"<long>\",\n      \"queries\": [\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"name\": \"<string>\"\n        },\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"name\": \"<string>\"\n        }\n      ],\n      \"formulas\": [\n        {\n          \"formula\": \"<string>\",\n          \"limit\": {\n            \"count\": \"<integer>\",\n            \"order\": \"desc\"\n          }\n        },\n        {\n          \"formula\": \"<string>\",\n          \"limit\": {\n            \"count\": \"<integer>\",\n            \"order\": \"desc\"\n          }\n        }\n      ],\n      \"interval\": \"<long>\"\n    }\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v2/query/timeseries",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "query",
            "timeseries"
          ]
        }
      },
      "status": "Forbidden",
      "code": 403,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"<string>\",\n    \"<string>\"\n  ]\n}"
    },
    {
      "name": "Too many requests",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "DD-API-KEY",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"data\": {\n    \"type\": \"timeseries_request\",\n    \"attributes\": {\n      \"to\": \"<long>\",\n      \"from\": \"<long>\",\n      \"queries\": [\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"name\": \"<string>\"\n        },\n        {\n          \"data_source\": \"metrics\",\n          \"query\": \"<string>\",\n          \"name\": \"<string>\"\n        }\n      ],\n      \"formulas\": [\n        {\n          \"formula\": \"<string>\",\n          \"limit\": {\n            \"count\": \"<integer>\",\n            \"order\": \"desc\"\n          }\n        },\n        {\n          \"formula\": \"<string>\",\n          \"limit\": {\n            \"count\": \"<integer>\",\n            \"order\": \"desc\"\n          }\n        }\n      ],\n      \"interval\": \"<long>\"\n    }\n  }\n}",
          "options": {
            "raw": {
              "headerFamily": "json",
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/api/v2/query/timeseries",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "query",
            "timeseries"
          ]
        }
      },
      "status": "Too Many Requests",
      "code": 429,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"errors\": [\n    \"<string>\",\n    \"<string>\"\n  ]\n}"
    }
  ]
}