---
title: Query the usage data scoped to the customer
slug: reference/query-the-usage-data-scoped-to-the-customer
docTags: 
createdAt: 2024-09-30T17:03:00.666Z
---

{
  "id": "LaKvXgCiQR1fzVpU5UbN5",
  "type": "api-oas-v2",
  "data": {
    "method": "POST",
    "url": "https://app.amberflo.io/customer-portal/usage",
    "servers": [
      {
        "url": "https://app.amberflo.io/customer-portal/usage"
      }
    ],
    "name": "Query the usage data scoped to the customer",
    "description": "",
    "contentType": "application/json",
    "request": {
      "pathParameters": [],
      "headerParameters": [],
      "queryParameters": [
        {
          "kind": "required",
          "name": "sessionToken",
          "type": "string",
          "description": "<p>A required session token used to authenticate API calls made on behalf of a customer. This token is obtained from the <code>Create a session</code> endpoint and must be included with every request to authorize access.</p>"
        }
      ],
      "bodyDataParameters": [
        {
          "kind": "optional",
          "name": "body",
          "type": "object",
          "description": "",
          "customType": "usage-request",
          "schema": [
            {
              "name": "aggregation",
              "kind": "optional",
              "type": "string<SUM | MIN | MAX | AVG>",
              "description": "Aggregation function. If not specified, the default is obtained from the associated meter definition.\n",
              "example": ""
            },
            {
              "name": "meterApiName",
              "kind": "required",
              "type": "string",
              "description": "`meterApiName` as defined in [create a meter](ref:post_meters).\n",
              "example": "",
              "default": "ApiCalls"
            },
            {
              "name": "timeGroupingInterval",
              "kind": "required",
              "type": "string<HOUR | DAY | WEEK | MONTH>",
              "description": "The usage service can provide you with statistics about the usage of your system. For each group of statistics the usage service can also provide you with a time stream of statistics per the `timeGroupingInterval`. So you can see for example, how the `SUM` of the usage changes over time. Notice that for aggregation types: `MIN`, `MAX`, `AVG` this field has a more delicate meaning. For example for `AVG` if this field value is an `HOUR`, then you get the hourly average usage value for each group and a breakdown of each hour usage. On the other hand if this field is a `DAY` then you get the avg of the total daily usage, with a breakdown of the total usage per day.\n",
              "example": "",
              "default": "DAY"
            },
            {
              "name": "valuesTimeGroupingInterval",
              "kind": "optional",
              "type": "string<HOUR | DAY | WEEK | MONTH>",
              "description": "This is an advanced feature. Let's assume you set the `aggregation` to be `AVG` and the `timeGroupingInterval` to be a day. In such a case the system will return the avg of the total daily usage for every group, together with a breakdown of the total usage per day. Now, if you still want to get daily aggregation-value breakdown, but you want each aggregation-value to have the hourly avg over each specific day, and the hourly avg of the entire group. Then the way to do it is setting this param to an `HOUR`. In other words this param allows you to have an hourly avg aggregation (for example), but change each group aggregation-values set to be daily slices instead of hourly slices.\n- Limitation 1: Can't be applied together with `SUM`.\n- Limitation 2: Can't be less granular than `timeGroupingInterval`\n  (for example one can't have an `HOUR` timeGroupingInterval with a `MONTH` `valuesTimeGroupingInterval`)\n",
              "example": ""
            },
            {
              "name": "timeRange",
              "kind": "required",
              "type": "object",
              "description": "Get the usage during this time range.",
              "example": "",
              "schema": [
                {
                  "name": "startTimeInSeconds",
                  "kind": "required",
                  "type": "integer",
                  "description": "Start date time represented as seconds since the Unix Epoch (1970-01-01T00:00:00Z) and using UTC. For example, 1619293642 => 2021-04-26T20:03:33.263Z",
                  "example": 0,
                  "default": 1619293642
                },
                {
                  "name": "endTimeInSeconds",
                  "kind": "optional",
                  "type": "integer",
                  "description": "End date time represented as seconds since the Unix Epoch (1970-01-01T00:00:00Z) and using UTC. Defaults to \"now\".",
                  "example": 0
                }
              ]
            },
            {
              "name": "groupBy",
              "kind": "optional",
              "type": "array",
              "description": "Group the usage data by these dimensions. If unspecified or empty, data will be aggregated for the meter as a whole.\n\nValid values are:\n- `customerId`\n- `customerName`\n- Any dimension defined in for the meter, see the [dimensions field](ref:post_meters).\n",
              "example": "",
              "default": [
                "customerId"
              ],
              "itemType": "string",
              "customType": "string[]",
              "itemExample": "",
              "itemDefault": ""
            },
            {
              "name": "take",
              "kind": "optional",
              "type": "object",
              "example": "",
              "schema": [
                {
                  "name": "limit",
                  "kind": "optional",
                  "type": "integer",
                  "description": "limit the number of records in the result set",
                  "example": 0,
                  "default": 10
                },
                {
                  "name": "isAscending",
                  "kind": "optional",
                  "type": "string",
                  "description": "Sort order (default is descending)",
                  "example": "",
                  "default": "false"
                }
              ]
            },
            {
              "name": "filter",
              "kind": "optional",
              "type": "map",
              "description": "\"where\" clause for the usage query. Each entry is a dimension and a list of values to filter for.\n\nValid filters are:\n- Any dimension defined in for the meter, see the [dimensions field](ref:post_meters).\n",
              "example": "",
              "customType": "map<string, string[]>",
              "schema": []
            }
          ],
          "modelRef": "#/components/schemas/usage-request",
          "isExpanded": true
        }
      ],
      "formDataParameters": [],
      "oAuthParameters": [
        {
          "id": "api_key",
          "name": "x-api-key",
          "kind": "optional",
          "type": "apiKey",
          "in": "header"
        }
      ]
    },
    "responses": [
      {
        "statusCode": "200",
        "description": "Successful response",
        "jsonExample": "",
        "isExpanded": true,
        "schema": [
          {
            "kind": "optional",
            "type": "object",
            "description": "",
            "customType": "usage-response",
            "schema": [
              {
                "name": "metadata",
                "kind": "optional",
                "type": "object",
                "description": "",
                "modelRef": "#/components/schemas/usage-request",
                "customType": "usage-request",
                "schema": [
                  {
                    "name": "aggregation",
                    "kind": "optional",
                    "type": "string<SUM | MIN | MAX | AVG>",
                    "description": "Aggregation function. If not specified, the default is obtained from the associated meter definition.\n",
                    "example": ""
                  },
                  {
                    "name": "meterApiName",
                    "kind": "required",
                    "type": "string",
                    "description": "`meterApiName` as defined in [create a meter](ref:post_meters).\n",
                    "example": "",
                    "default": "ApiCalls"
                  },
                  {
                    "name": "timeGroupingInterval",
                    "kind": "required",
                    "type": "string<HOUR | DAY | WEEK | MONTH>",
                    "description": "The usage service can provide you with statistics about the usage of your system. For each group of statistics the usage service can also provide you with a time stream of statistics per the `timeGroupingInterval`. So you can see for example, how the `SUM` of the usage changes over time. Notice that for aggregation types: `MIN`, `MAX`, `AVG` this field has a more delicate meaning. For example for `AVG` if this field value is an `HOUR`, then you get the hourly average usage value for each group and a breakdown of each hour usage. On the other hand if this field is a `DAY` then you get the avg of the total daily usage, with a breakdown of the total usage per day.\n",
                    "example": "",
                    "default": "DAY"
                  },
                  {
                    "name": "valuesTimeGroupingInterval",
                    "kind": "optional",
                    "type": "string<HOUR | DAY | WEEK | MONTH>",
                    "description": "This is an advanced feature. Let's assume you set the `aggregation` to be `AVG` and the `timeGroupingInterval` to be a day. In such a case the system will return the avg of the total daily usage for every group, together with a breakdown of the total usage per day. Now, if you still want to get daily aggregation-value breakdown, but you want each aggregation-value to have the hourly avg over each specific day, and the hourly avg of the entire group. Then the way to do it is setting this param to an `HOUR`. In other words this param allows you to have an hourly avg aggregation (for example), but change each group aggregation-values set to be daily slices instead of hourly slices.\n- Limitation 1: Can't be applied together with `SUM`.\n- Limitation 2: Can't be less granular than `timeGroupingInterval`\n  (for example one can't have an `HOUR` timeGroupingInterval with a `MONTH` `valuesTimeGroupingInterval`)\n",
                    "example": ""
                  },
                  {
                    "name": "timeRange",
                    "kind": "required",
                    "type": "object",
                    "description": "Get the usage during this time range.",
                    "example": "",
                    "schema": [
                      {
                        "name": "startTimeInSeconds",
                        "kind": "required",
                        "type": "integer",
                        "description": "Start date time represented as seconds since the Unix Epoch (1970-01-01T00:00:00Z) and using UTC. For example, 1619293642 => 2021-04-26T20:03:33.263Z",
                        "example": 0,
                        "default": 1619293642
                      },
                      {
                        "name": "endTimeInSeconds",
                        "kind": "optional",
                        "type": "integer",
                        "description": "End date time represented as seconds since the Unix Epoch (1970-01-01T00:00:00Z) and using UTC. Defaults to \"now\".",
                        "example": 0
                      }
                    ]
                  },
                  {
                    "name": "groupBy",
                    "kind": "optional",
                    "type": "array",
                    "description": "Group the usage data by these dimensions. If unspecified or empty, data will be aggregated for the meter as a whole.\n\nValid values are:\n- `customerId`\n- `customerName`\n- Any dimension defined in for the meter, see the [dimensions field](ref:post_meters).\n",
                    "example": "",
                    "default": [
                      "customerId"
                    ],
                    "itemType": "string",
                    "customType": "string[]",
                    "itemExample": "",
                    "itemDefault": ""
                  },
                  {
                    "name": "take",
                    "kind": "optional",
                    "type": "object",
                    "example": "",
                    "schema": [
                      {
                        "name": "limit",
                        "kind": "optional",
                        "type": "integer",
                        "description": "limit the number of records in the result set",
                        "example": 0,
                        "default": 10
                      },
                      {
                        "name": "isAscending",
                        "kind": "optional",
                        "type": "string",
                        "description": "Sort order (default is descending)",
                        "example": "",
                        "default": "false"
                      }
                    ]
                  },
                  {
                    "name": "filter",
                    "kind": "optional",
                    "type": "map",
                    "description": "\"where\" clause for the usage query. Each entry is a dimension and a list of values to filter for.\n\nValid filters are:\n- Any dimension defined in for the meter, see the [dimensions field](ref:post_meters).\n",
                    "example": "",
                    "customType": "map<string, string[]>",
                    "schema": []
                  }
                ]
              },
              {
                "name": "secondsSinceEpochIntervals",
                "kind": "optional",
                "type": "array",
                "description": "Buckets or time-slots with the starting at time in seconds since the Unix Epoch (1970-01-01T00:00:00Z) and using UTC for time representation.\n\nThe query was run for usage activity in last 2 days with start time at\n1619293642 => 2021-04-26T20:03:33.263Z\n\nIn this example, following are the buckets:\n1619222400 => 2021-04-24T00:00:00.000Z\n1619308800 => 2021-04-25T00:00:00.000Z\n1619395200 => 2021-04-26T00:00:00.000Z\n\nAll time buckets are present, even if no usage was reported for it.\n",
                "example": "",
                "itemType": "integer",
                "customType": "integer[]",
                "itemExample": "",
                "itemDefault": ""
              },
              {
                "name": "clientMeters",
                "kind": "optional",
                "type": "array",
                "description": "Aggregated usage, per group.\n",
                "example": "",
                "itemType": "object",
                "customType": "object[]",
                "schema": [
                  {
                    "name": "group",
                    "kind": "optional",
                    "type": "object",
                    "example": "",
                    "schema": [
                      {
                        "name": "groupInfo",
                        "kind": "optional",
                        "type": "object",
                        "description": "Name-value map of the `groupBy` dimensions for the group.\n\nWill be empty if no `groupBy` was specified.\n",
                        "example": "",
                        "schema": []
                      }
                    ]
                  },
                  {
                    "name": "groupValue",
                    "kind": "optional",
                    "type": "number",
                    "description": "Aggregated value for the group for all buckets.",
                    "example": ""
                  },
                  {
                    "name": "values",
                    "kind": "optional",
                    "type": "array",
                    "description": "Time series of the usage, corresponding to `secondsSinceEpochIntervals`.\n",
                    "example": "",
                    "itemType": "object",
                    "customType": "object[]",
                    "schema": [
                      {
                        "name": "value",
                        "kind": "optional",
                        "type": "number",
                        "example": ""
                      },
                      {
                        "name": "secondsSinceEpochUtc",
                        "kind": "optional",
                        "type": "number",
                        "example": ""
                      },
                      {
                        "name": "percentageFromPrevious",
                        "kind": "optional",
                        "type": "string",
                        "description": "Percent change from previous bucket.",
                        "example": ""
                      }
                    ]
                  }
                ]
              }
            ],
            "modelRef": "#/components/schemas/usage-response",
            "isExpanded": true
          }
        ]
      }
    ],
    "hasXCodeSamples": false,
    "examples": {
      "languages": [
        {
          "id": "MSN8I3zMxIRiXW29uQZCe",
          "language": "curl",
          "label": "cURL",
          "code": "curl --request POST \\\n     --url 'https://app.amberflo.io/customer-portal/usage?sessionToken=string' \\\n     --header 'accept: application/json' \\\n     --header 'content-type: application/json' \\\n     --data '{\n     \"meterApiName\": \"ApiCalls\",\n     \"timeGroupingInterval\": \"DAY\",\n     \"timeRange\": {\n     \"startTimeInSeconds\": 1619293642\n     },\n     \"groupBy\": [\n     \"customerId\"\n     ],\n     \"take\": {\n     \"limit\": 10,\n     \"isAscending\": \"false\"\n     }\n     }'"
        },
        {
          "id": "1NTlvrU3oHj4J00Ur0jC2",
          "language": "javascript",
          "label": "javascript",
          "code": "var myHeaders = new Headers();\nmyHeaders.append(\"accept\", \"application/json\");\nmyHeaders.append(\"content-type\", \"application/json\");\n\nvar raw = JSON.stringify({\n   \"meterApiName\": \"ApiCalls\",\n   \"timeGroupingInterval\": \"DAY\",\n   \"timeRange\": {\n      \"startTimeInSeconds\": 1619293642\n   },\n   \"groupBy\": [\n      \"customerId\"\n   ],\n   \"take\": {\n      \"limit\": 10,\n      \"isAscending\": \"false\"\n   }\n});\n\nvar requestOptions = {\n   method: 'POST',\n   headers: myHeaders,\n   body: raw,\n   redirect: 'follow'\n};\n\nfetch(\"https://app.amberflo.io/customer-portal/usage?sessionToken=string\", requestOptions)\n   .then(response => response.text())\n   .then(result => console.log(result))\n   .catch(error => console.log('error', error));"
        },
        {
          "id": "oSHPm3qbSQAhjeOk1mAsN",
          "language": "ruby",
          "label": "Ruby",
          "code": "require \"uri\"\nrequire \"json\"\nrequire \"net/http\"\n\nurl = URI(\"https://app.amberflo.io/customer-portal/usage?sessionToken=string\")\n\nhttps = Net::HTTP.new(url.host, url.port)\nhttps.use_ssl = true\n\nrequest = Net::HTTP::Post.new(url)\nrequest[\"accept\"] = \"application/json\"\nrequest[\"content-type\"] = \"application/json\"\nrequest.body = JSON.dump({\n   \"meterApiName\": \"ApiCalls\",\n   \"timeGroupingInterval\": \"DAY\",\n   \"timeRange\": {\n      \"startTimeInSeconds\": 1619293642\n   },\n   \"groupBy\": [\n      \"customerId\"\n   ],\n   \"take\": {\n      \"limit\": 10,\n      \"isAscending\": \"false\"\n   }\n})\n\nresponse = https.request(request)\nputs response.read_body\n"
        },
        {
          "id": "sILlgS8hIjLNe4UII07-A",
          "language": "python",
          "label": "Python",
          "code": "import requests\nimport json\n\nurl = \"https://app.amberflo.io/customer-portal/usage?sessionToken=string\"\n\npayload = json.dumps({\n   \"meterApiName\": \"ApiCalls\",\n   \"timeGroupingInterval\": \"DAY\",\n   \"timeRange\": {\n      \"startTimeInSeconds\": 1619293642\n   },\n   \"groupBy\": [\n      \"customerId\"\n   ],\n   \"take\": {\n      \"limit\": 10,\n      \"isAscending\": \"false\"\n   }\n})\nheaders = {\n   'accept': 'application/json',\n   'content-type': 'application/json'\n}\n\nresponse = requests.request(\"POST\", url, headers=headers, data=payload)\n\nprint(response.text)\n"
        }
      ],
      "selectedLanguageId": "MSN8I3zMxIRiXW29uQZCe"
    },
    "results": {
      "languages": [
        {
          "id": "response-0-200",
          "language": "200",
          "code": "// Successful response\n{\n  \"metadata\": {\n    \"aggregation\": \"\",\n    \"meterApiName\": \"\",\n    \"timeGroupingInterval\": \"\",\n    \"valuesTimeGroupingInterval\": \"\",\n    \"timeRange\": {\n      \"startTimeInSeconds\": 0,\n      \"endTimeInSeconds\": 0\n    },\n    \"groupBy\": [\n      \"\"\n    ],\n    \"take\": {\n      \"limit\": 0,\n      \"isAscending\": \"\"\n    },\n    \"filter\": \"\"\n  },\n  \"secondsSinceEpochIntervals\": [\n    0\n  ],\n  \"clientMeters\": [\n    {\n      \"group\": {\n        \"groupInfo\": {}\n      },\n      \"groupValue\": 0,\n      \"values\": [\n        {\n          \"value\": 0,\n          \"secondsSinceEpochUtc\": 0,\n          \"percentageFromPrevious\": \"\"\n        }\n      ]\n    }\n  ]\n}"
        }
      ],
      "selectedLanguageId": "response-0-200"
    }
  },
  "children": [
    {
      "text": "",
      "id": "3CPooC5szGOA0h3-pzKZA"
    }
  ]
}