{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://artifacts.apis.io/spicrawl-com/json-schema/spicrawl-com-usage-by-key-schema.json",
  "title": "UsageByKey",
  "description": "`GET /v1/usage?group_by=key`. Only the caller's organization's keys, and only the key's own project for a project-scoped key. The dashboard route `GET /v1/orgs/{orgID}/usage?group_by=key` returns the same `keys` array with inclusive `from`/`to` dates instead of `period`.",
  "x-generated": "2026-11-10",
  "x-method": "derived",
  "x-generator": "derive-json-schema.py",
  "x-source": "openapi/spicrawl-com-usage-api-openapi.yml#/components/schemas/UsageByKey",
  "type": "object",
  "required": [
    "period",
    "group_by",
    "keys"
  ],
  "properties": {
    "period": {
      "$ref": "#/$defs/UsagePeriod"
    },
    "group_by": {
      "type": "string",
      "enum": [
        "key"
      ]
    },
    "keys": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "key_id",
          "name",
          "prefix",
          "project_id",
          "revoked",
          "days",
          "totals"
        ],
        "properties": {
          "key_id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "prefix": {
            "type": "string",
            "description": "Display prefix of the key, never the secret."
          },
          "project_id": {
            "type": "string"
          },
          "revoked": {
            "type": "boolean"
          },
          "days": {
            "type": "array",
            "items": {
              "type": "object",
              "required": [
                "day",
                "metrics"
              ],
              "properties": {
                "day": {
                  "type": "string",
                  "format": "date"
                },
                "metrics": {
                  "$ref": "#/$defs/UsageMetricMap"
                }
              }
            }
          },
          "totals": {
            "$ref": "#/$defs/UsageMetricMap"
          }
        }
      }
    }
  },
  "$defs": {
    "UsageMetricMap": {
      "type": "object",
      "description": "Metric name to quantity. Keys are UsageMetric values.",
      "additionalProperties": {
        "type": "integer",
        "format": "int64"
      }
    },
    "UsagePeriod": {
      "type": "object",
      "required": [
        "start",
        "end",
        "days",
        "bounds"
      ],
      "description": "Half-open range of UTC days.",
      "properties": {
        "start": {
          "type": "string",
          "format": "date",
          "description": "First day, inclusive."
        },
        "end": {
          "type": "string",
          "format": "date",
          "description": "End day, EXCLUSIVE. The last included day is the day before this."
        },
        "days": {
          "type": "integer",
          "minimum": 0,
          "description": "Number of days in the period."
        },
        "bounds": {
          "type": "string",
          "const": "[start, end)",
          "description": "Always `[start, end)`, a reminder that `end` is exclusive."
        }
      }
    }
  }
}