{
  "openapi": "3.1.0",
  "info": {
    "title": "AskWearables API",
    "version": "1.0.0",
    "description": "A broker between wearable data and AI agents. Ask a question; receive a small computed context packet at the level the user's grant allows. Developer requests are signed: HMAC-SHA256 over `timestamp\\nnonce\\nMETHOD\\npath\\nsha256(body)` with your signing secret, a single-use nonce and a 300-second window."
  },
  "servers": [
    {
      "url": "https://api.askwearables.app"
    }
  ],
  "security": [
    {
      "bearer": []
    }
  ],
  "paths": {
    "/v1/ask": {
      "post": {
        "operationId": "ask",
        "summary": "Ask a question in plain language",
        "parameters": [
          {
            "name": "X-AskWearables-User",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string"
            },
            "description": "Developer keys: your end user's external id."
          },
          {
            "name": "X-AskWearables-Timestamp",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "X-AskWearables-Nonce",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "X-AskWearables-Signature",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "question"
                ],
                "properties": {
                  "question": {
                    "type": "string",
                    "minLength": 3,
                    "maxLength": 500
                  },
                  "purpose": {
                    "type": "string",
                    "description": "Recorded, never used: the purpose is fixed in the grant."
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/AskResponse"
                }
              }
            }
          },
          "403": {
            "description": "Denied, with reasons",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/AskResponse"
                }
              }
            }
          }
        }
      }
    },
    "/v1/query": {
      "post": {
        "operationId": "query",
        "summary": "Run a QuerySpec directly (agents: gated; owner keys: level 3)",
        "parameters": [
          {
            "name": "X-AskWearables-User",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string"
            },
            "description": "Developer keys: your end user's external id."
          },
          {
            "name": "X-AskWearables-Timestamp",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "X-AskWearables-Nonce",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "X-AskWearables-Signature",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "spec"
                ],
                "properties": {
                  "spec": {
                    "$ref": "#/components/schemas/QuerySpec"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/AskResponse"
                }
              }
            }
          }
        }
      }
    },
    "/v1/capabilities": {
      "get": {
        "operationId": "capabilities",
        "summary": "Metrics, operations, levels and this agent's grant",
        "parameters": [
          {
            "name": "X-AskWearables-User",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string"
            },
            "description": "Developer keys: your end user's external id."
          },
          {
            "name": "X-AskWearables-Timestamp",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "X-AskWearables-Nonce",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "X-AskWearables-Signature",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          }
        }
      }
    },
    "/v1/packets/{packet_id}": {
      "get": {
        "operationId": "getPacket",
        "summary": "A packet this agent received in the last 24 hours",
        "parameters": [
          {
            "name": "packet_id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "X-AskWearables-User",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string"
            },
            "description": "Developer keys: your end user's external id."
          },
          {
            "name": "X-AskWearables-Timestamp",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "X-AskWearables-Nonce",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "X-AskWearables-Signature",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Packet"
                }
              }
            }
          },
          "404": {
            "description": "Not found",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          }
        }
      }
    },
    "/v1/receipts": {
      "get": {
        "operationId": "receipts",
        "summary": "Owner: receipts, newest first, with chain verification",
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer"
            }
          },
          {
            "name": "before",
            "in": "query",
            "schema": {
              "type": "integer"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          }
        }
      }
    },
    "/v1/grants": {
      "post": {
        "operationId": "createGrantLink",
        "summary": "Developer: a one-time link to the hosted consent screen for your user",
        "parameters": [
          {
            "name": "X-AskWearables-User",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string"
            },
            "description": "Developer keys: your end user's external id."
          },
          {
            "name": "X-AskWearables-Timestamp",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "X-AskWearables-Nonce",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "X-AskWearables-Signature",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/GrantRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "url": {
                      "type": "string"
                    },
                    "expires_in": {
                      "type": "integer"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/v1/grants/{grant_id}": {
      "delete": {
        "operationId": "revokeGrant",
        "summary": "Owner: revoke a grant",
        "parameters": [
          {
            "name": "grant_id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          }
        }
      }
    },
    "/v1/users": {
      "post": {
        "operationId": "createUser",
        "summary": "Developer: register an end user by your own id",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "external_id"
                ],
                "properties": {
                  "external_id": {
                    "type": "string"
                  },
                  "tz": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          }
        }
      }
    },
    "/v1/ingest": {
      "post": {
        "operationId": "ingest",
        "summary": "Push normalized records (sync keys, owner keys, developer keys)",
        "parameters": [
          {
            "name": "X-AskWearables-User",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string"
            },
            "description": "Developer keys: your end user's external id."
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "source": {
                    "type": "object"
                  },
                  "records": {
                    "type": "array",
                    "maxItems": 2000,
                    "items": {
                      "type": "object"
                    }
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          }
        }
      }
    },
    "/v1/usage": {
      "get": {
        "operationId": "usage",
        "summary": "Developer: calls, packets and denials per day",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array"
                }
              }
            }
          }
        }
      }
    },
    "/v1/export": {
      "get": {
        "operationId": "export",
        "summary": "Owner: export everything",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearer": {
        "type": "http",
        "scheme": "bearer",
        "description": "An OAuth access token for an agent, or an API key: aw_own_ (owner), aw_dev_ (developer), aw_sync_ (device sync)."
      }
    },
    "schemas": {
      "QuerySpec": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "metrics",
          "operation",
          "window"
        ],
        "properties": {
          "metrics": {
            "type": "array",
            "minItems": 1,
            "maxItems": 2,
            "items": {
              "type": "string",
              "enum": [
                "steps",
                "active_energy",
                "resting_hr",
                "heart_rate",
                "hrv_rmssd",
                "hrv_sdnn",
                "sleep_duration",
                "sleep_efficiency",
                "sleep_stages",
                "spo2",
                "respiratory_rate",
                "skin_temp_deviation",
                "vo2max",
                "weight",
                "workout"
              ]
            }
          },
          "operation": {
            "type": "string",
            "enum": [
              "latest",
              "aggregate",
              "trend",
              "compare_to_baseline",
              "compare_periods",
              "threshold_check",
              "correlate",
              "list_sessions"
            ]
          },
          "window": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "start",
              "end"
            ],
            "properties": {
              "start": {
                "type": "string",
                "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
              },
              "end": {
                "type": "string",
                "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
              }
            }
          },
          "granularity": {
            "type": "string",
            "enum": [
              "day",
              "week"
            ]
          },
          "params": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "comparator": {
                "type": "string",
                "enum": [
                  "<",
                  "<=",
                  ">",
                  ">="
                ]
              },
              "threshold": {
                "type": "number"
              },
              "threshold_unit": {
                "type": "string"
              },
              "mode": {
                "type": "string",
                "enum": [
                  "any",
                  "all",
                  "count"
                ]
              },
              "compare_window": {
                "type": "object",
                "additionalProperties": false,
                "required": [
                  "start",
                  "end"
                ],
                "properties": {
                  "start": {
                    "type": "string",
                    "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
                  },
                  "end": {
                    "type": "string",
                    "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
                  }
                }
              },
              "lag_days": {
                "type": "integer",
                "enum": [
                  0,
                  1
                ]
              },
              "session_kind": {
                "type": "string",
                "enum": [
                  "workout",
                  "sleep"
                ]
              },
              "statistic": {
                "type": "string",
                "enum": [
                  "total",
                  "mean",
                  "min",
                  "max"
                ]
              }
            }
          }
        }
      },
      "GrantRequest": {
        "type": "object",
        "required": [
          "purpose",
          "metrics",
          "max_level",
          "max_window_days",
          "daily_packet_budget",
          "days"
        ],
        "properties": {
          "purpose": {
            "type": "string",
            "enum": [
              "scheduling",
              "training_advice",
              "sleep_coaching",
              "general_wellness",
              "personal_research"
            ]
          },
          "metrics": {
            "type": "array",
            "items": {
              "type": "string",
              "enum": [
                "steps",
                "active_energy",
                "resting_hr",
                "heart_rate",
                "hrv_rmssd",
                "hrv_sdnn",
                "sleep_duration",
                "sleep_efficiency",
                "sleep_stages",
                "spo2",
                "respiratory_rate",
                "skin_temp_deviation",
                "vo2max",
                "weight",
                "workout",
                "blood_glucose",
                "menstrual_cycle",
                "ecg_classification",
                "breathing_disturbances"
              ]
            }
          },
          "max_level": {
            "type": "integer",
            "enum": [
              0,
              1,
              2,
              3
            ]
          },
          "max_window_days": {
            "type": "integer",
            "minimum": 1,
            "maximum": 365
          },
          "daily_packet_budget": {
            "type": "integer",
            "minimum": 1
          },
          "days": {
            "type": "integer",
            "minimum": 1,
            "maximum": 90
          },
          "return_url": {
            "type": "string",
            "format": "uri"
          }
        }
      },
      "Finding": {
        "type": "object",
        "required": [
          "metric",
          "statement"
        ],
        "properties": {
          "metric": {
            "type": "string"
          },
          "statement": {
            "type": "string"
          },
          "flag": {
            "type": "string",
            "enum": [
              "yes",
              "no",
              "unknown"
            ]
          },
          "direction": {
            "type": "string"
          },
          "magnitude": {
            "type": "string"
          },
          "value": {
            "type": "number"
          },
          "unit": {
            "type": "string"
          },
          "values": {
            "type": "array",
            "items": {
              "type": "object"
            }
          },
          "unknown_reason": {
            "type": "string"
          }
        }
      },
      "Packet": {
        "type": "object",
        "required": [
          "packet_version",
          "packet_id",
          "level",
          "findings",
          "data_quality",
          "data_version",
          "not_medical_advice"
        ],
        "properties": {
          "packet_version": {
            "const": "1"
          },
          "packet_id": {
            "type": "string"
          },
          "level": {
            "type": "integer"
          },
          "operation": {
            "type": "string",
            "enum": [
              "latest",
              "aggregate",
              "trend",
              "compare_to_baseline",
              "compare_periods",
              "threshold_check",
              "correlate",
              "list_sessions"
            ]
          },
          "metrics": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "window": {
            "type": "object"
          },
          "findings": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Finding"
            }
          },
          "data_quality": {
            "type": "object",
            "properties": {
              "freshness_hours": {
                "type": [
                  "number",
                  "null"
                ]
              },
              "coverage": {
                "type": [
                  "number",
                  "null"
                ]
              },
              "sources": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "baseline_days": {
                "type": [
                  "integer",
                  "null"
                ]
              },
              "caveats": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              }
            }
          },
          "as_of": {
            "type": "string"
          },
          "data_version": {
            "type": "string"
          },
          "not_medical_advice": {
            "const": true
          }
        }
      },
      "AskResponse": {
        "type": "object",
        "required": [
          "status"
        ],
        "properties": {
          "status": {
            "type": "string",
            "enum": [
              "ok",
              "denied",
              "unsupported"
            ]
          },
          "packet": {
            "$ref": "#/components/schemas/Packet"
          },
          "receipt_id": {
            "type": "string"
          },
          "reasons": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "supported": {
            "type": "array",
            "items": {
              "type": "string"
            }
          }
        }
      }
    }
  }
}
