{
  "openapi": "3.0.3",
  "info": {
    "title": "ResearchAO Public API",
    "version": "1.0.0",
    "description": "Deep research API for programmatic access. Rate limits: 60 requests per minute per API key for research endpoints, 120 requests per minute for read-only endpoints."
  },
  "servers": [
    {
      "url": "https://ulhmhgsixjphxtgxiqup.supabase.co/functions/v1/public-api",
      "description": "Research API (requires API key after activation)"
    },
    {
      "url": "https://www.researchao.com/api/v1",
      "description": "Agent self-registration API (machine onboarding)"
    }
  ],
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "paths": {
    "/agent-registrations": {
      "post": {
        "summary": "Create provisional machine agent registration",
        "operationId": "createAgentRegistration",
        "security": [],
        "description": "Creates a short-lived provisional registration and returns an Ed25519 challenge. No API access or credits are granted. Requires Idempotency-Key. Self-registration must be enabled server-side.",
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": true,
            "schema": { "type": "string" }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "agent_name",
                  "requested_plan",
                  "public_key",
                  "terms_version"
                ],
                "properties": {
                  "agent_name": { "type": "string" },
                  "description": { "type": "string" },
                  "requested_plan": {
                    "type": "string",
                    "enum": ["starter", "researcher", "pro"]
                  },
                  "public_key": {
                    "type": "object",
                    "required": ["kty", "crv", "x"],
                    "properties": {
                      "kty": { "type": "string", "enum": ["OKP"] },
                      "crv": { "type": "string", "enum": ["Ed25519"] },
                      "x": { "type": "string" }
                    }
                  },
                  "owner_contact_email": { "type": "string", "format": "email" },
                  "terms_version": { "type": "string", "example": "2026-08-01" }
                }
              }
            }
          }
        },
        "responses": {
          "201": { "description": "Registration created with challenge" },
          "429": { "description": "registration_rate_limited" },
          "503": { "description": "Self-registration disabled or unavailable" }
        }
      }
    },
    "/agent-registrations/{registration_id}/verify": {
      "post": {
        "summary": "Verify challenge signature",
        "operationId": "verifyAgentRegistration",
        "security": [],
        "parameters": [
          {
            "name": "registration_id",
            "in": "path",
            "required": true,
            "schema": { "type": "string", "format": "uuid" }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["signature"],
                "properties": {
                  "signature": { "type": "string", "description": "base64url Ed25519 signature" },
                  "algorithm": { "type": "string", "enum": ["Ed25519"] }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Temporary registration credential returned once"
          },
          "401": { "description": "invalid_signature" }
        }
      }
    },
    "/agent-registrations/{registration_id}": {
      "get": {
        "summary": "Registration status",
        "operationId": "getAgentRegistration",
        "security": [{ "registrationAuth": [] }],
        "parameters": [
          {
            "name": "registration_id",
            "in": "path",
            "required": true,
            "schema": { "type": "string", "format": "uuid" }
          }
        ],
        "responses": {
          "200": { "description": "Safe registration status" }
        }
      }
    },
    "/agent-registrations/{registration_id}/checkout": {
      "post": {
        "summary": "Create Stripe Checkout session",
        "operationId": "createAgentRegistrationCheckout",
        "security": [{ "registrationAuth": [] }],
        "description": "Returns a Stripe Checkout URL for a human to authorise payment. Plan Price IDs are selected server-side. No final API key is issued here.",
        "parameters": [
          {
            "name": "registration_id",
            "in": "path",
            "required": true,
            "schema": { "type": "string", "format": "uuid" }
          }
        ],
        "responses": {
          "200": { "description": "Checkout URL" },
          "402": { "description": "payment_required" }
        }
      }
    },
    "/agent-registrations/{registration_id}/activate": {
      "post": {
        "summary": "Exchange for final API credential",
        "operationId": "activateAgentRegistration",
        "security": [{ "registrationAuth": [] }],
        "description": "Requires verified payment via Stripe webhook. Returns rao_agent_live_ credential once. Repeated calls return credential_already_issued.",
        "parameters": [
          {
            "name": "registration_id",
            "in": "path",
            "required": true,
            "schema": { "type": "string", "format": "uuid" }
          }
        ],
        "responses": {
          "200": { "description": "Final API key returned once" },
          "402": { "description": "payment_required" },
          "409": { "description": "credential_already_issued" }
        }
      }
    },
    "/v1/research": {
      "post": {
        "summary": "Create research",
        "operationId": "createResearch",
        "description": "Submit a new deep research run. Rate limited to 60 requests per minute.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["input"],
                "properties": {
                  "input": {
                    "type": "string",
                    "description": "The research question or topic"
                  },
                  "title": {
                    "type": "string",
                    "description": "Optional title for the research"
                  },
                  "research_effort": {
                    "type": "string",
                    "enum": ["quick", "standard", "deep"],
                    "default": "standard"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "202": {
            "description": "Research accepted and queued"
          },
          "429": {
            "description": "Rate limit exceeded"
          }
        }
      },
      "get": {
        "summary": "List research runs",
        "operationId": "listResearch",
        "description": "List all research runs for the authenticated user.",
        "responses": {
          "200": {
            "description": "List of runs"
          }
        }
      }
    },
    "/v1/research/{run_id}": {
      "get": {
        "summary": "Get research run",
        "operationId": "getResearch",
        "parameters": [
          {
            "name": "run_id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Research run details"
          }
        }
      }
    },
    "/v1/research/{run_id}/messages": {
      "post": {
        "summary": "Follow-up message",
        "operationId": "researchMessage",
        "description": "Send a follow-up message on an existing research run.",
        "parameters": [
          {
            "name": "run_id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "202": {
            "description": "Message accepted"
          }
        }
      }
    },
    "/v1/schedules": {
      "get": {
        "summary": "List schedules",
        "operationId": "listSchedules",
        "description": "List all research schedules for the authenticated user.",
        "responses": {
          "200": {
            "description": "List of schedules"
          }
        }
      },
      "post": {
        "summary": "Create schedule",
        "operationId": "createSchedule",
        "description": "Create a new research schedule (one-time, daily, weekly, or monthly).",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["name", "input", "schedule_type"],
                "properties": {
                  "name": {
                    "type": "string"
                  },
                  "input": {
                    "type": "string"
                  },
                  "schedule_type": {
                    "type": "string",
                    "enum": ["once", "daily", "weekly", "monthly"]
                  },
                  "timezone": {
                    "type": "string",
                    "default": "UTC"
                  },
                  "local_time": {
                    "type": "string",
                    "description": "HH:MM format"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Schedule created"
          }
        }
      }
    },
    "/v1/usage": {
      "get": {
        "summary": "Read usage",
        "operationId": "getUsage",
        "description": "Get credit balance and usage statistics.",
        "responses": {
          "200": {
            "description": "Credit usage"
          }
        }
      }
    },
    "/v1/settings/timezone": {
      "get": {
        "summary": "Get timezone",
        "operationId": "getTimezone",
        "description": "Get the account timezone setting. Requires settings:timezone:read scope.",
        "responses": {
          "200": {
            "description": "Current timezone",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "request_id": {
                      "type": "string"
                    },
                    "timezone": {
                      "type": "string",
                      "example": "America/New_York"
                    }
                  }
                }
              }
            }
          }
        }
      },
      "patch": {
        "summary": "Update timezone",
        "operationId": "updateTimezone",
        "description": "Update the account timezone setting. Requires settings:timezone:update scope. Must be a valid IANA timezone.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["timezone"],
                "properties": {
                  "timezone": {
                    "type": "string",
                    "example": "Europe/London",
                    "description": "Valid IANA timezone identifier"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Timezone updated successfully"
          },
          "400": {
            "description": "Invalid timezone"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "ResearchAO API key: rao_live_... or rao_agent_live_..."
      },
      "registrationAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "Temporary registration credential: rao_reg_... (issued after challenge verification)"
      }
    }
  }
}
