{
  "openapi": "3.1.0",
  "info": {
    "title": "Aprve Authorization API",
    "version": "1.1.0",
    "description": "Aprve is the real-time authority layer for autonomous action. Systems call it before an agent acts; the same call returns allow or deny and writes an append-only, hash-chained record that anyone can verify without an account.",
    "contact": {
      "name": "Aprve",
      "url": "https://aprve-trust.base44.app"
    }
  },
  "servers": [
    {
      "url": "https://aprve-trust.base44.app",
      "description": "Production"
    }
  ],
  "tags": [
    {
      "name": "authorize",
      "description": "Decide whether an agent may act"
    },
    {
      "name": "decisions",
      "description": "Poll a decision, including its human gate"
    },
    {
      "name": "verify",
      "description": "Public record verification, no authentication"
    },
    {
      "name": "evidence",
      "description": "Evidence packs behind a share token"
    },
    {
      "name": "transparency",
      "description": "Aggregate public metrics"
    }
  ],
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "paths": {
    "/api/v1/authorize": {
      "post": {
        "tags": [
          "authorize"
        ],
        "operationId": "authorizeAction",
        "summary": "Authorize one agent action",
        "description": "Evaluates the client's active policy in-process and returns allow or deny. The record is written before the response is sent, and the response is never cacheable. Fail closed: anything other than an explicit allow must stop the action.",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/AuthorizeRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "A decision, with its record",
            "headers": {
              "x-aprve-decision": {
                "schema": {
                  "type": "string",
                  "enum": [
                    "allow",
                    "deny"
                  ]
                }
              },
              "x-aprve-decision-id": {
                "schema": {
                  "type": "string"
                }
              },
              "x-aprve-record-seq": {
                "schema": {
                  "type": "string"
                }
              },
              "x-aprve-record-hash": {
                "schema": {
                  "type": "string"
                }
              },
              "x-aprve-latency-ms": {
                "schema": {
                  "type": "string"
                }
              },
              "x-aprve-signature": {
                "schema": {
                  "type": "string"
                },
                "description": "Present when signing_mode is hmac_sha256"
              },
              "x-aprve-signature-alg": {
                "schema": {
                  "type": "string"
                }
              },
              "x-aprve-replay": {
                "schema": {
                  "type": "string"
                },
                "description": "true when an idempotency key replayed"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/AuthorizeResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "422": {
            "$ref": "#/components/responses/Invalid"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "500": {
            "$ref": "#/components/responses/Internal"
          }
        }
      }
    },
    "/api/v1/decisions/{decision_id}": {
      "get": {
        "tags": [
          "decisions"
        ],
        "operationId": "getDecision",
        "summary": "Read a decision and its proof status",
        "description": "Authenticated status read for the calling system. Returns the terminal decision (allow or deny), the rule that produced it, the record hash, the public verify URL, and the statement status with its receipt id. A decision never changes state and nothing waits on a person.",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "name": "decision_id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "example": "dec_7f3a91c4b2"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Decision status",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/DecisionStatus"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "404": {
            "description": "No such decision for this client"
          }
        }
      }
    },
    "/api/v1/records/{decision_id}": {
      "get": {
        "tags": [
          "verify"
        ],
        "operationId": "getRecordPublic",
        "summary": "Public record by decision id",
        "description": "No authentication. Sanitized: no amount, no context, no payload.",
        "security": [],
        "parameters": [
          {
            "name": "decision_id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Verification result",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Verification"
                }
              }
            }
          },
          "404": {
            "description": "No record with that decision id"
          }
        }
      }
    },
    "/api/v1/verify/{hash}": {
      "get": {
        "tags": [
          "verify"
        ],
        "operationId": "verifyHash",
        "summary": "Public verification by record hash",
        "description": "Recomputes the record's hash from its stored fields, checks the declared link to the previous record, and reports anchor and signing status. Returns an ETag; send If-None-Match for a 304.",
        "security": [],
        "parameters": [
          {
            "name": "hash",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Verification result",
            "headers": {
              "etag": {
                "schema": {
                  "type": "string"
                }
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Verification"
                }
              }
            }
          },
          "304": {
            "description": "Unchanged since the supplied ETag"
          },
          "404": {
            "description": "No record with that hash"
          }
        }
      }
    },
    "/api/v1/verify": {
      "get": {
        "tags": [
          "verify"
        ],
        "operationId": "verifyHashQuery",
        "summary": "Public verification by query string",
        "security": [],
        "parameters": [
          {
            "name": "hash",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Verification result, or usage when hash is omitted",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Verification"
                }
              }
            }
          },
          "404": {
            "description": "No record with that hash"
          }
        }
      }
    },
    "/api/v1/evidence/{token}": {
      "get": {
        "tags": [
          "evidence"
        ],
        "operationId": "getEvidencePack",
        "summary": "Evidence pack as JSON",
        "description": "Public, behind an expiring share token. Rebuilt from the ledger at read time.",
        "security": [],
        "parameters": [
          {
            "name": "token",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Evidence payload",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/EvidencePack"
                }
              }
            }
          },
          "404": {
            "description": "Unknown token"
          },
          "410": {
            "description": "Link expired"
          }
        }
      }
    },
    "/api/v1/evidence/{token}/pdf": {
      "get": {
        "tags": [
          "evidence"
        ],
        "operationId": "getEvidencePackPdf",
        "summary": "Evidence pack as PDF",
        "description": "Server-rendered, printable attestation over the same range.",
        "security": [],
        "parameters": [
          {
            "name": "token",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "PDF document",
            "content": {
              "application/pdf": {
                "schema": {
                  "type": "string",
                  "format": "binary"
                }
              }
            }
          },
          "404": {
            "description": "Unknown token"
          },
          "410": {
            "description": "Link expired"
          }
        }
      }
    },
    "/api/v1/metrics": {
      "get": {
        "tags": [
          "transparency"
        ],
        "operationId": "getMetrics",
        "summary": "Public aggregate metrics",
        "description": "Decision volume, latency percentiles, anchoring health, human-gate throughput. No client names.",
        "security": [],
        "responses": {
          "200": {
            "description": "Metrics",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Metrics"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "An Aprve API key or capability token: apr_live_… or apr_cap_…. The x-aprve-key header is also accepted."
      }
    },
    "responses": {
      "Unauthorized": {
        "description": "Missing, invalid, revoked or expired credential",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "Forbidden": {
        "description": "Scope, tool allowlist, paused client or cross-client request",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "Invalid": {
        "description": "Malformed body or missing field",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "RateLimited": {
        "description": "Local or global window exhausted",
        "headers": {
          "retry-after": {
            "schema": {
              "type": "string"
            }
          },
          "x-ratelimit-scope": {
            "schema": {
              "type": "string",
              "enum": [
                "worker",
                "global"
              ]
            }
          }
        },
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "Internal": {
        "description": "Unexpected failure. No decision was made.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      }
    },
    "schemas": {
      "AuthorizeRequest": {
        "type": "object",
        "required": [
          "agent_id",
          "action"
        ],
        "properties": {
          "agent_id": {
            "type": "string",
            "description": "Stable identifier for the acting agent."
          },
          "agent_name": {
            "type": "string"
          },
          "client": {
            "type": "string",
            "description": "Client slug, when the token is not already scoped to one."
          },
          "project": {
            "type": "string",
            "description": "Project slug."
          },
          "action": {
            "type": "object",
            "required": [
              "tool"
            ],
            "properties": {
              "tool": {
                "type": "string",
                "description": "Tool name as policy sees it, e.g. refund.create"
              },
              "amount_usd": {
                "type": "number"
              },
              "target": {
                "type": "string"
              },
              "data_classes": {
                "type": "array",
                "items": {
                  "type": "string"
                },
                "maxItems": 32
              }
            }
          },
          "risk_score": {
            "type": "number",
            "minimum": 0,
            "maximum": 100
          },
          "context": {
            "type": "object",
            "description": "Caller context, stored on the record. Identifiers, not payloads."
          },
          "handoff_from": {
            "type": "string",
            "description": "Prior decision id or record hash this action is acting under."
          },
          "training_excluded": {
            "type": "boolean",
            "description": "Mark the record as excluded from model training."
          },
          "idempotency_key": {
            "type": "string",
            "description": "Replay returns the original decision and record hash."
          }
        }
      },
      "AuthorizeResponse": {
        "type": "object",
        "properties": {
          "decision_id": {
            "type": "string"
          },
          "decision": {
            "type": "string",
            "enum": [
              "allow",
              "deny"
            ]
          },
          "decision_source": {
            "type": "string",
            "enum": [
              "policy",
              "human",
              "timeout_default"
            ]
          },
          "matched_rule_id": {
            "type": [
              "string",
              "null"
            ]
          },
          "matched_rule_name": {
            "type": [
              "string",
              "null"
            ]
          },
          "policy_version": {
            "type": [
              "integer",
              "null"
            ]
          },
          "explanation": {
            "type": "string"
          },
          "record": {
            "type": "object",
            "properties": {
              "seq": {
                "type": "integer"
              },
              "hash": {
                "type": "string"
              }
            }
          },
          "signature": {
            "type": [
              "object",
              "null"
            ],
            "properties": {
              "alg": {
                "type": "string"
              },
              "key_prefix": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "input": {
                "type": "string",
                "description": "Canonical string that was signed, newlines escaped."
              }
            }
          },
          "statement": {
            "type": "object",
            "properties": {
              "status": {
                "type": "string",
                "example": "in_calendar"
              },
              "receipt_id": {
                "type": [
                  "string",
                  "null"
                ],
                "example": "moment-j5xx23u1"
              },
              "statement_url": {
                "type": [
                  "string",
                  "null"
                ]
              }
            }
          },
          "latency_ms": {
            "type": "integer"
          },
          "replayed": {
            "type": "boolean"
          }
        }
      },
      "DecisionStatus": {
        "type": "object",
        "properties": {
          "decision_id": {
            "type": "string"
          },
          "status": {
            "type": "string",
            "enum": [
              "pending",
              "resolved"
            ]
          },
          "decision": {
            "type": "string"
          },
          "decision_source": {
            "type": "string"
          },
          "matched_rule_id": {
            "type": [
              "string",
              "null"
            ]
          },
          "policy_version": {
            "type": [
              "integer",
              "null"
            ]
          },
          "explanation": {
            "type": "string"
          },
          "record": {
            "type": "object",
            "properties": {
              "seq": {
                "type": "integer"
              },
              "hash": {
                "type": "string"
              }
            }
          },
          "verify_url": {
            "type": "string"
          },
          "statement": {
            "type": "object"
          },
          "resolved": {
            "type": [
              "object",
              "null"
            ]
          }
        }
      },
      "Verification": {
        "type": "object",
        "properties": {
          "verified": {
            "type": "boolean"
          },
          "verification_url": {
            "type": "string"
          },
          "record": {
            "type": "object",
            "properties": {
              "decision_id": {
                "type": "string"
              },
              "seq": {
                "type": "integer"
              },
              "chain_position": {
                "type": "object",
                "properties": {
                  "seq": {
                    "type": "integer"
                  },
                  "total": {
                    "type": [
                      "integer",
                      "null"
                    ]
                  }
                }
              },
              "decision": {
                "type": "string"
              },
              "decision_source": {
                "type": "string"
              },
              "agent_id": {
                "type": "string"
              },
              "action_tool": {
                "type": "string"
              },
              "record_type": {
                "type": "string"
              },
              "correction_of_prior": {
                "type": "boolean"
              },
              "handoff": {
                "type": "boolean"
              },
              "policy_id": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "policy_version": {
                "type": [
                  "integer",
                  "null"
                ]
              },
              "matched_rule_id": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "matched_rule_name": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "explanation": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "created_at": {
                "type": "string",
                "format": "date-time"
              },
              "hash": {
                "type": "string"
              },
              "prev_hash": {
                "type": "string"
              },
              "training_excluded": {
                "type": "boolean"
              }
            }
          },
          "chain": {
            "type": "object",
            "properties": {
              "prev_present": {
                "type": "boolean"
              },
              "seq_contiguous": {
                "type": "boolean"
              },
              "recomputed": {
                "type": "string"
              },
              "algorithm": {
                "type": "string"
              }
            }
          },
          "anchor": {
            "type": "object",
            "properties": {
              "status": {
                "type": "string",
                "enum": [
                  "pending",
                  "anchored",
                  "failed",
                  "not_required"
                ]
              },
              "receipt_id": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "receipt_url": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "merkle_root": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "verified": {
                "type": "boolean"
              }
            }
          },
          "signing": {
            "type": "object",
            "properties": {
              "mode": {
                "type": "string"
              },
              "secret_issued": {
                "type": "boolean"
              }
            }
          },
          "client": {
            "type": "object",
            "properties": {
              "name": {
                "type": "string"
              },
              "slug": {
                "type": "string"
              }
            }
          }
        }
      },
      "EvidencePack": {
        "type": "object",
        "properties": {
          "format": {
            "type": "string",
            "example": "aprve.evidence.v1"
          },
          "generated_at": {
            "type": "string",
            "format": "date-time"
          },
          "client": {
            "type": "object"
          },
          "project": {
            "type": [
              "object",
              "null"
            ]
          },
          "period": {
            "type": "object"
          },
          "attestation": {
            "type": "object",
            "properties": {
              "record_count": {
                "type": "integer"
              },
              "first_seq": {
                "type": [
                  "integer",
                  "null"
                ]
              },
              "last_seq": {
                "type": [
                  "integer",
                  "null"
                ]
              },
              "terminal_hash": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "contiguous": {
                "type": "boolean"
              },
              "gaps": {
                "type": "array",
                "items": {
                  "type": "object"
                }
              },
              "algorithm": {
                "type": "string"
              },
              "anchors": {
                "type": "array",
                "items": {
                  "type": "object"
                }
              }
            }
          },
          "policy_explanation": {
            "type": "array",
            "items": {
              "type": "object"
            }
          },
          "identity_inventory": {
            "type": "array",
            "items": {
              "type": "object"
            }
          },
          "human_oversight": {
            "type": "array",
            "items": {
              "type": "object"
            }
          },
          "records": {
            "type": "array",
            "items": {
              "type": "object"
            }
          }
        }
      },
      "Metrics": {
        "type": "object",
        "properties": {
          "generated_at": {
            "type": "string",
            "format": "date-time"
          },
          "decisions": {
            "type": "object"
          },
          "latency": {
            "type": "object"
          },
          "anchor": {
            "type": "object"
          },
          "authority": {
            "type": "object"
          },
          "automation": {
            "type": "object"
          },
          "drift": {
            "type": "object"
          }
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "string"
          },
          "detail": {
            "type": "object"
          },
          "retry_after": {
            "type": "integer"
          }
        }
      }
    }
  }
}