{
  "openapi": "3.1.0",
  "info": {
    "title": "Bugerz Installation API",
    "version": "1.0.0",
    "description": "Prepare a public widget installation and return a direct setup link. The person chooses their own password or logs in and confirms. Email is a delayed fallback for unfinished new accounts. See /agents.md. This API grants no existing account or report access."
  },
  "servers": [
    {
      "url": "/",
      "description": "The server serving this document"
    }
  ],
  "paths": {
    "/api/v1/installations": {
      "post": {
        "operationId": "prepareInstallation",
        "summary": "Prepare an installation and return the person’s setup link",
        "description": "Obtain the owner email and request to install Bugerz. Give setupUrl to the person; never redeem it or choose a password. Default link delivery has a conditional 30-minute email fallback only while no account exists and setup is unfinished (operator configurable). delivery=email requests immediate email. Existing users must log in. Reuse the same random Idempotency-Key and body after timeouts. Limits: 3/email/day, 10/IP network/hour, 1000 pending service-wide. No account existence is disclosed.",
        "security": [],
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": true,
            "description": "Retain a random UUID or 32–128 random base64url characters across retries. Treat as a credential: retries return the same installation and status token.",
            "schema": {
              "type": "string",
              "pattern": "^[A-Za-z0-9_-]{32,128}$"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "email",
                  "projectName",
                  "websiteUrl"
                ],
                "properties": {
                  "email": {
                    "type": "string",
                    "format": "email",
                    "maxLength": 254
                  },
                  "projectName": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 120
                  },
                  "websiteUrl": {
                    "type": "string",
                    "format": "uri",
                    "maxLength": 2048,
                    "description": "HTTP or HTTPS URL without credentials, query or fragment. Informational; not domain verification."
                  },
                  "delivery": {
                    "type": "string",
                    "enum": [
                      "link",
                      "email"
                    ],
                    "default": "link",
                    "description": "link returns setupUrl with delayed conditional email fallback; email sends immediately. The first choice is immutable across retries."
                  }
                },
                "example": {
                  "email": "owner@example.com",
                  "projectName": "Customer website",
                  "websiteUrl": "https://example.com/"
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Installation prepared or identical retry. setupUrl is for the human to open. No email verification token, session or private management key is returned.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "ok",
                    "installation"
                  ],
                  "properties": {
                    "ok": {
                      "const": true
                    },
                    "installation": {
                      "allOf": [
                        {
                          "$ref": "#/components/schemas/Installation"
                        },
                        {
                          "type": "object",
                          "required": [
                            "statusToken",
                            "statusUrl",
                            "setupUrl"
                          ],
                          "properties": {
                            "statusToken": {
                              "type": "string",
                              "pattern": "^[A-Za-z0-9_-]{43}$",
                              "description": "Read-only status credential. Never put in website source or URLs."
                            },
                            "statusUrl": {
                              "type": "string",
                              "format": "uri"
                            },
                            "setupUrl": {
                              "type": "string",
                              "format": "uri",
                              "description": "Private direct setup link with a separate token in its URL fragment. Give to the person to open themselves. Can create a new unverified-email account or connect to an authenticated matching account; never verifies an email or bypasses an existing password. Seven-day expiry, consumed on confirmation."
                            }
                          }
                        }
                      ]
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/Error"
          },
          "409": {
            "$ref": "#/components/responses/Error"
          },
          "410": {
            "$ref": "#/components/responses/Error"
          },
          "413": {
            "$ref": "#/components/responses/Error"
          },
          "429": {
            "$ref": "#/components/responses/Error"
          },
          "503": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    },
    "/api/v1/installations/{id}": {
      "get": {
        "operationId": "getInstallationStatus",
        "summary": "Read installation activation status",
        "description": "Poll at most once per minute. Requires statusToken in Authorization: Bearer. This credential cannot claim an installation, access an account or read reports. All public installation requests share a 60/IP/15-minute throttle. Tokens are not accepted in URLs.",
        "security": [
          {
            "installationStatus": []
          }
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Installation state only.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "ok",
                    "installation"
                  ],
                  "properties": {
                    "ok": {
                      "const": true
                    },
                    "installation": {
                      "$ref": "#/components/schemas/Installation"
                    }
                  }
                }
              }
            }
          },
          "404": {
            "$ref": "#/components/responses/Error"
          },
          "429": {
            "$ref": "#/components/responses/Error"
          },
          "503": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "installationStatus": {
        "type": "http",
        "scheme": "bearer",
        "description": "Installation-scoped statusToken returned by preparation. Not a widget key or email claim token."
      }
    },
    "schemas": {
      "Installation": {
        "type": "object",
        "required": [
          "id",
          "status",
          "publicKey",
          "projectName",
          "websiteUrl",
          "expiresAt",
          "collectionEnabled",
          "emailStatus",
          "snippet",
          "delivery",
          "fallbackEmailAt"
        ],
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "status": {
            "type": "string",
            "enum": [
              "pending_claim",
              "active",
              "expired",
              "revoked"
            ]
          },
          "publicKey": {
            "type": "string",
            "pattern": "^[a-f0-9]{32}$",
            "description": "Public browser submission key; unusable before activation."
          },
          "projectName": {
            "type": "string"
          },
          "websiteUrl": {
            "type": "string",
            "format": "uri"
          },
          "expiresAt": {
            "type": "integer",
            "description": "Email activation deadline in Unix milliseconds; seven days from preparation. Active keys remain usable after this deadline."
          },
          "collectionEnabled": {
            "type": "boolean"
          },
          "emailStatus": {
            "type": "string",
            "enum": [
              "conditional",
              "not_required",
              "queued",
              "sent",
              "failed"
            ],
            "description": "For link delivery, conditional while pending and not_required after completion/expiry; never reveals account existence or actual reminder delivery. Immediate email reports SMTP job state."
          },
          "snippet": {
            "type": "string",
            "description": "Ready-to-paste script and public key initializer for this server."
          },
          "delivery": {
            "type": "string",
            "enum": [
              "link",
              "email"
            ]
          },
          "fallbackEmailAt": {
            "type": [
              "integer",
              "null"
            ],
            "description": "Conditional fallback deadline in Unix milliseconds; null for immediate email. The fallback only sends if no account exists and setup remains unfinished."
          }
        }
      }
    },
    "responses": {
      "Error": {
        "description": "400 invalid input; 404 invalid status ID/token; 409 changed idempotent request; 410 credentials invalidated by secret rotation; 413 body exceeds 8 KB; 429 throttled; 503 server configuration incomplete.",
        "content": {
          "application/json": {
            "schema": {
              "type": "object",
              "required": [
                "message"
              ],
              "properties": {
                "ok": {
                  "const": false
                },
                "message": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    }
  }
}
