{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.healthexport.dev/schemas/webhook/v1/metrics-envelope.json",
  "title": "MetricBridge webhook: metrics envelope (metrics.daily, metrics.intraday, workouts)",
  "description": "Health Auto Export compatible body, unchanged since 1.0. It has no type key: the kind is in the X-MetricBridge-Type header. Daily runs carry one point per metric per local day; intraday runs one per hour. Sleep totals arrive here as the sleep_analysis metric.",
  "type": "object",
  "required": ["data"],
  "not": { "required": ["type"] },
  "properties": {
    "token": { "type": "string", "description": "Only when the auth method is Body." },
    "data": {
      "type": "object",
      "required": ["metrics"],
      "properties": {
        "metrics": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["name", "units", "data"],
            "properties": {
              "name": { "type": "string", "description": "Catalog metric name, e.g. step_count, sleep_analysis." },
              "units": { "type": "string" },
              "data": {
                "type": "array",
                "items": {
                  "type": "object",
                  "required": ["date", "qty"],
                  "properties": {
                    "date": { "$ref": "common.json#/$defs/haeDate" },
                    "qty": { "type": "number" },
                    "Min": { "type": "number" }, "Max": { "type": "number" }, "Avg": { "type": "number" },
                    "totalSleep": { "type": "number" }, "asleep": { "type": "number" },
                    "core": { "type": "number" }, "deep": { "type": "number" }, "rem": { "type": "number" }
                  }
                }
              }
            }
          }
        },
        "workouts": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["id", "name", "start", "end", "duration"],
            "properties": {
              "id": { "type": "string", "description": "HealthKit workout UUID: upsert on it." },
              "name": { "type": "string" },
              "activityType": { "type": ["string", "integer"] },
              "start": { "type": "string" },
              "end": { "type": "string" },
              "duration": { "type": "number", "description": "Seconds." },
              "activeEnergyBurned": { "type": "number" },
              "distanceMeters": { "type": "number" },
              "avgHeartRate": { "type": "number" },
              "maxHeartRate": { "type": "number" }
            }
          }
        }
      }
    }
  }
}
