{
  "openapi": "3.1.0",
  "info": {
    "title": "HandleMy Dispatch API",
    "version": "1.0.0",
    "summary": "Hand a real-world job to a vetted HandleMy handler, with an explicit consent scope.",
    "description": "Any agent, life OS, calendar or task tool can dispatch work into HandleMy. A job carries a plain-language `task`, optional structured `details`, and a per-job `consent` block (service, budget, autonomy, contact channels, expiry) that must fit inside the API key's standing scope. Handlers see the consent on the job card and act within it. State changes are pushed to an optional signed `callback_url`.",
    "contact": { "url": "https://handlemy.world/developers" }
  },
  "servers": [{ "url": "https://api.handlemy.world/api/v1" }],
  "security": [{ "bearer": [] }],
  "components": {
    "securitySchemes": {
      "bearer": { "type": "http", "scheme": "bearer", "bearerFormat": "hm_live_…", "description": "Create a key at https://handlemy.world/profile#agents. The key acts as the member who made it." }
    },
    "schemas": {
      "Consent": {
        "type": "object",
        "required": ["service"],
        "properties": {
          "service":    { "type": "string", "description": "One HandleMy service the job belongs to.", "enum": ["art","business","casa","finance","health","house","money","net","org","party","store","website"] },
          "budget_usd": { "type": "number", "minimum": 0, "default": 0, "description": "Maximum pass-through spend the handler may commit on the member's behalf. 0 = no spend authorised." },
          "autonomy":   { "type": "string", "enum": ["ask_first", "proceed"], "default": "ask_first", "description": "`proceed` lets the handler act before checking in; only allowed if the key permits it." },
          "contact":    { "type": "array", "items": { "type": "string", "enum": ["chat","email","phone"] }, "default": ["chat"], "description": "How the handler may reach the member." },
          "expires_at": { "type": "string", "format": "date-time", "description": "Void the job if it is still unclaimed after this time." },
          "notes":      { "type": "string", "maxLength": 500, "description": "Anything else the member wants honoured, in plain language." }
        }
      },
      "DispatchRequest": {
        "type": "object",
        "required": ["task", "consent"],
        "properties": {
          "task":         { "type": "string", "maxLength": 500, "description": "What needs doing, in plain language." },
          "catalog":      { "type": "string", "description": "Pin a catalog entry, e.g. `casa/deep_clean_kitchen`. Otherwise HandleMy matches on the task text." },
          "details":      { "type": "object", "additionalProperties": true, "description": "Answers to the catalog entry's `required_details` (snake_case keys)." },
          "consent":      { "$ref": "#/components/schemas/Consent" },
          "ref":          { "type": "string", "maxLength": 120, "description": "Your own id for this job. Re-posting the same `ref` on the same key returns the existing job (idempotent)." },
          "callback_url": { "type": "string", "format": "uri", "description": "https URL to POST signed state changes to." }
        }
      },
      "Job": {
        "type": "object",
        "properties": {
          "id":          { "type": "integer" },
          "status":      { "type": "string", "enum": ["open","claimed","in_progress","blocked","done","cancelled"] },
          "task":        { "type": "string" },
          "service":     { "type": "string" },
          "catalog":     { "type": ["string","null"] },
          "details":     { "type": "object" },
          "missing":     { "type": "array", "items": { "type": "object" }, "description": "Required details still unanswered — the handler will ask, or you can send a note." },
          "consent":     { "$ref": "#/components/schemas/Consent" },
          "source":      { "type": "object", "properties": { "kind": { "type": "string" }, "label": { "type": "string" }, "ref": { "type": ["string","null"] } } },
          "handler":     { "type": ["object","null"], "properties": { "name": { "type": "string" } } },
          "notes_count": { "type": "integer" },
          "callback":    { "type": ["object","null"], "properties": { "url": { "type": "string" }, "last": { "type": ["string","null"] }, "at": { "type": ["string","null"] } } },
          "created_at":  { "type": "string" },
          "updated_at":  { "type": "string" }
        }
      },
      "Note": {
        "type": "object",
        "properties": {
          "id": { "type": "integer" }, "author": { "type": "string", "enum": ["customer","handler","system"] },
          "name": { "type": "string" }, "body": { "type": "string" }, "attachment": { "type": ["string","null"] }, "created_at": { "type": "string" }
        }
      },
      "CatalogEntry": {
        "type": "object",
        "properties": {
          "catalog": { "type": "string" }, "service": { "type": "string" }, "name": { "type": "string" }, "summary": { "type": "string" },
          "category": { "type": "string" }, "complexity": { "type": "string" }, "estimated_minutes": { "type": ["integer","null"] },
          "lead_time_hours": { "type": ["integer","null"] },
          "required_details": { "type": "array", "items": { "type": "object", "properties": { "key": { "type": "string" }, "label": { "type": "string" }, "type": { "type": "string" } } } },
          "optional_details": { "type": "array", "items": { "type": "object" } },
          "cost_estimate": { "type": ["object","null"] }, "tags": { "type": "array", "items": { "type": "string" } }
        }
      },
      "Error": { "type": "object", "properties": { "status": { "const": "error" }, "error": { "type": "string" }, "message": { "type": "string" } } },
      "CallbackEvent": {
        "type": "object",
        "description": "POSTed to `callback_url`. Header `X-HandleMy-Signature: sha256=<HMAC-SHA256 of the raw body with your key's webhook_secret>`; header `X-HandleMy-Event` repeats `event`.",
        "properties": {
          "event":   { "type": "string", "enum": ["dispatch.created","dispatch.claimed","dispatch.in_progress","dispatch.blocked","dispatch.done","dispatch.cancelled","dispatch.handler_note","dispatch.note"] },
          "job":     { "$ref": "#/components/schemas/Job" },
          "sent_at": { "type": "string", "format": "date-time" }
        }
      }
    },
    "responses": {
      "Unauthenticated": { "description": "Missing, revoked, expired or malformed key.", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } }
    }
  },
  "paths": {
    "/": { "get": { "summary": "Discovery manifest", "security": [], "responses": { "200": { "description": "Routes, auth scheme, services, callback contract." } } } },
    "/openapi.json": { "get": { "summary": "This document", "security": [], "responses": { "200": { "description": "OpenAPI 3.1" } } } },
    "/vault": { "get": { "summary": "Member's vault status + people graph", "description": "Whether the member's MeCentral vault is connected (facts approved on HandleMy mirror there) and the people graph HandleMy holds locally. Needs the `vault` scope.", "responses": { "200": { "description": "`{vault: {provider, available, connected, email, connected_at, connect_url}, people: [{id, kind, name, relationship, facts}]}`" }, "403": { "description": "Key scope lacks `vault`." } } } },
    "/me": { "get": { "summary": "Who the key acts as, and its scope", "responses": { "200": { "description": "Member + key (including `webhook_secret` for verifying callbacks)." }, "401": { "$ref": "#/components/responses/Unauthenticated" } } } },
    "/catalog": {
      "get": {
        "summary": "Browse dispatchable tasks",
        "parameters": [
          { "name": "service", "in": "query", "schema": { "type": "string" } },
          { "name": "q", "in": "query", "schema": { "type": "string" } },
          { "name": "limit", "in": "query", "schema": { "type": "integer", "maximum": 100, "default": 25 } }
        ],
        "responses": { "200": { "description": "`{entries: CatalogEntry[]}`" } }
      }
    },
    "/catalog/{service}/{slug}": {
      "get": {
        "summary": "One catalog entry with handler instructions and deliverables",
        "parameters": [ { "name": "service", "in": "path", "required": true, "schema": { "type": "string" } }, { "name": "slug", "in": "path", "required": true, "schema": { "type": "string" } } ],
        "responses": { "200": { "description": "CatalogEntry + description, handler_instructions, deliverables" }, "404": { "description": "Unknown entry" } }
      }
    },
    "/dispatch": {
      "post": {
        "summary": "Dispatch a job",
        "requestBody": { "required": true, "content": { "application/json": { "schema": { "$ref": "#/components/schemas/DispatchRequest" } } } },
        "responses": {
          "201": { "description": "Created. `{job: Job, created: true}`" },
          "200": { "description": "Idempotent replay — a job with this `ref` already exists. `{job: Job, created: false}`" },
          "401": { "$ref": "#/components/responses/Unauthenticated" },
          "403": { "description": "Key scope lacks `dispatch`." },
          "422": { "description": "Consent does not fit the key's scope (see `error`, e.g. `consent.budget_exceeds_key_cap`)." },
          "429": { "description": "More than 60 dispatches from this key in an hour." }
        }
      }
    },
    "/dispatches": {
      "get": {
        "summary": "List the member's jobs",
        "parameters": [ { "name": "mine", "in": "query", "description": "1 = only jobs dispatched by this key", "schema": { "type": "integer" } }, { "name": "limit", "in": "query", "schema": { "type": "integer", "maximum": 200 } } ],
        "responses": { "200": { "description": "`{jobs: Job[]}`" } }
      }
    },
    "/dispatch/{id}": {
      "get": {
        "summary": "One job + its notes thread",
        "parameters": [ { "name": "id", "in": "path", "required": true, "schema": { "type": "integer" } } ],
        "responses": { "200": { "description": "`{job: Job, notes: Note[]}`" }, "404": { "description": "Not this member's job." } }
      }
    },
    "/dispatch/{id}/note": {
      "post": {
        "summary": "Add a member-side note (e.g. answer a missing detail)",
        "parameters": [ { "name": "id", "in": "path", "required": true, "schema": { "type": "integer" } } ],
        "requestBody": { "required": true, "content": { "application/json": { "schema": { "type": "object", "required": ["body"], "properties": { "body": { "type": "string", "maxLength": 4000 } } } } } },
        "responses": { "201": { "description": "`{note_id}`" } }
      }
    },
    "/dispatch/{id}/cancel": {
      "post": {
        "summary": "Cancel a job that is not yet done",
        "parameters": [ { "name": "id", "in": "path", "required": true, "schema": { "type": "integer" } } ],
        "requestBody": { "content": { "application/json": { "schema": { "type": "object", "properties": { "reason": { "type": "string", "maxLength": 300 } } } } } },
        "responses": { "200": { "description": "`{job: Job}`" }, "409": { "description": "Already done or cancelled." } }
      }
    }
  },
  "webhooks": {
    "dispatchEvent": { "post": { "summary": "State change on a job you dispatched", "requestBody": { "content": { "application/json": { "schema": { "$ref": "#/components/schemas/CallbackEvent" } } } }, "responses": { "2XX": { "description": "Acknowledged. Anything else is recorded as `failed:<code>` on the job's `callback`." } } } }
  }
}
