{
  "openapi": "3.1.0",
  "info": {
    "title": "Redpen API",
    "version": "1.0.0",
    "description": "REST API for Redpen — host HTML documents, manage versions, and collect element-anchored comments. All endpoints require an API key."
  },
  "servers": [
    {
      "url": "https://app.redpens.app",
      "description": "Production"
    }
  ],
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "sk_live_<random>",
        "description": "API key with scopes: upload, read, comment"
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "object",
            "properties": {
              "code": {
                "type": "string"
              },
              "message": {
                "type": "string"
              }
            }
          }
        }
      },
      "Document": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "slug": {
            "type": "string"
          },
          "title": {
            "type": "string",
            "nullable": true
          },
          "filename": {
            "type": "string",
            "nullable": true
          },
          "visibility": {
            "type": "string",
            "enum": [
              "authed",
              "link"
            ]
          },
          "createdAt": {
            "type": "integer",
            "description": "Unix ms"
          },
          "updatedAt": {
            "type": "integer",
            "description": "Unix ms"
          },
          "archivedAt": {
            "type": "integer",
            "nullable": true
          },
          "expiresAt": {
            "type": "integer",
            "nullable": true
          },
          "isPermanent": {
            "type": "integer",
            "enum": [
              0,
              1
            ]
          },
          "url": {
            "type": "string",
            "description": "Full content URL"
          }
        }
      },
      "Comment": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "documentId": {
            "type": "string"
          },
          "parentId": {
            "type": "string",
            "nullable": true
          },
          "selector": {
            "type": "string",
            "nullable": true
          },
          "elementText": {
            "type": "string",
            "nullable": true
          },
          "body": {
            "type": "string"
          },
          "authorId": {
            "type": "string"
          },
          "createdAt": {
            "type": "string",
            "format": "date-time"
          },
          "resolvedAt": {
            "type": "string",
            "format": "date-time",
            "nullable": true
          },
          "isCurrentVersion": {
            "type": "boolean"
          },
          "attachment": {
            "nullable": true,
            "type": "object",
            "description": "Image attachment (top-level comments only; paste/drag). Served from the content origin.",
            "properties": {
              "url": {
                "type": "string"
              },
              "mime": {
                "type": "string"
              },
              "width": {
                "type": "integer",
                "nullable": true
              },
              "height": {
                "type": "integer",
                "nullable": true
              }
            }
          },
          "reproduceNote": {
            "type": "string",
            "nullable": true,
            "description": "Optional hint for reviewers on how to reach the UI state. ≤300 chars. Top-level comments only."
          },
          "breadcrumbs": {
            "nullable": true,
            "type": "array",
            "description": "Interaction steps recorded before the comment was placed. ≤8 items. Top-level comments only.",
            "items": {
              "type": "object",
              "properties": {
                "selector": {
                  "type": "string"
                },
                "text": {
                  "type": "string"
                }
              }
            }
          },
          "replies": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Comment"
            }
          }
        }
      }
    }
  },
  "paths": {
    "/api/documents": {
      "get": {
        "summary": "List documents",
        "description": "Lists the authenticated user's non-archived documents, newest first.",
        "tags": [
          "Documents"
        ],
        "parameters": [],
        "responses": {
          "200": {
            "description": "Document list",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "documents": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Document"
                      }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Not authenticated"
          },
          "403": {
            "description": "Scope required: read"
          }
        }
      },
      "post": {
        "summary": "Upload a document",
        "description": "Upload an HTML document. Accepts JSON body `{html, title?, visibility?}`, multipart/form-data with a `file` field, or raw text/html body. If a document with the same filename already exists for this owner, a new version is created instead.",
        "tags": [
          "Documents"
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "html"
                ],
                "properties": {
                  "html": {
                    "type": "string"
                  },
                  "title": {
                    "type": "string"
                  },
                  "filename": {
                    "type": "string"
                  },
                  "visibility": {
                    "type": "string",
                    "enum": [
                      "authed",
                      "link"
                    ],
                    "default": "authed",
                    "description": "'authed' (default) requires sign-in. 'link' is PUBLIC: anyone with the URL, no sign-in, including people outside the company."
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Deduplicated (same content already uploaded)",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "url": {
                      "type": "string"
                    },
                    "slug": {
                      "type": "string"
                    },
                    "documentId": {
                      "type": "string"
                    },
                    "versionId": {
                      "type": "string"
                    },
                    "hash": {
                      "type": "string"
                    },
                    "deduped": {
                      "type": "boolean"
                    },
                    "versioned": {
                      "type": "boolean"
                    },
                    "warnings": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      },
                      "description": "Advisory warnings about relative asset references or <base href> tags."
                    }
                  }
                }
              }
            }
          },
          "201": {
            "description": "Created or versioned",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "url": {
                      "type": "string"
                    },
                    "slug": {
                      "type": "string"
                    },
                    "documentId": {
                      "type": "string"
                    },
                    "versionId": {
                      "type": "string"
                    },
                    "hash": {
                      "type": "string"
                    },
                    "deduped": {
                      "type": "boolean"
                    },
                    "versioned": {
                      "type": "boolean"
                    },
                    "visibility": {
                      "type": "string",
                      "enum": [
                        "authed",
                        "link"
                      ]
                    },
                    "warnings": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      },
                      "description": "Advisory warnings about relative asset references or <base href> tags. Never blocks the upload."
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Validation error"
          },
          "401": {
            "description": "Not authenticated"
          },
          "403": {
            "description": "Scope required: upload"
          },
          "413": {
            "description": "File too large (15 MB limit)"
          }
        }
      }
    },
    "/api/documents/{slug}": {
      "get": {
        "summary": "Get a document",
        "tags": [
          "Documents"
        ],
        "parameters": [
          {
            "name": "slug",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Document with versions"
          },
          "404": {
            "description": "Not found"
          }
        }
      },
      "patch": {
        "summary": "Update visibility",
        "tags": [
          "Documents"
        ],
        "parameters": [
          {
            "name": "slug",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "visibility": {
                    "type": "string",
                    "enum": [
                      "authed",
                      "link"
                    ],
                    "description": "'authed' (default) requires sign-in. 'link' is PUBLIC: anyone with the URL, no sign-in, including people outside the company."
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Updated"
          },
          "404": {
            "description": "Not found"
          }
        }
      },
      "delete": {
        "summary": "Delete a document (session only)",
        "description": "Hard-deletes the document, its versions, and its comments. R2 objects are left in place (content-addressed, may be shared). Session auth only — API keys are rejected.",
        "tags": [
          "Documents"
        ],
        "parameters": [
          {
            "name": "slug",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "{\"deleted\":true}"
          },
          "403": {
            "description": "Session required; API keys not accepted"
          },
          "404": {
            "description": "Not found"
          }
        }
      }
    },
    "/api/documents/{slug}/versions": {
      "get": {
        "summary": "List versions",
        "tags": [
          "Versions"
        ],
        "parameters": [
          {
            "name": "slug",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Version list with isCurrent flag"
          },
          "404": {
            "description": "Not found"
          }
        }
      },
      "post": {
        "summary": "Add a new version",
        "tags": [
          "Versions"
        ],
        "parameters": [
          {
            "name": "slug",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "html"
                ],
                "properties": {
                  "html": {
                    "type": "string"
                  },
                  "title": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Deduplicated (same content as current version)",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "slug": {
                      "type": "string"
                    },
                    "url": {
                      "type": "string"
                    },
                    "versionId": {
                      "type": "string"
                    },
                    "hash": {
                      "type": "string"
                    },
                    "deduped": {
                      "type": "boolean"
                    },
                    "warnings": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      },
                      "description": "Advisory warnings about relative asset references or <base href> tags."
                    }
                  }
                }
              }
            }
          },
          "201": {
            "description": "New version created",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "slug": {
                      "type": "string"
                    },
                    "url": {
                      "type": "string"
                    },
                    "documentId": {
                      "type": "string"
                    },
                    "versionId": {
                      "type": "string"
                    },
                    "hash": {
                      "type": "string"
                    },
                    "deduped": {
                      "type": "boolean"
                    },
                    "warnings": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      },
                      "description": "Advisory warnings about relative asset references or <base href> tags. Never blocks the upload."
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Not found"
          }
        }
      }
    },
    "/api/documents/{slug}/archive": {
      "post": {
        "summary": "Archive a document",
        "description": "Archived documents return HTTP 410. Reversible via /unarchive.",
        "tags": [
          "Lifecycle"
        ],
        "parameters": [
          {
            "name": "slug",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "{\"ok\":true,\"archived\":true}"
          },
          "404": {
            "description": "Not found"
          }
        }
      }
    },
    "/api/documents/{slug}/unarchive": {
      "post": {
        "summary": "Unarchive a document",
        "tags": [
          "Lifecycle"
        ],
        "parameters": [
          {
            "name": "slug",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "{\"ok\":true,\"archived\":false}"
          },
          "404": {
            "description": "Not found"
          }
        }
      }
    },
    "/api/documents/{slug}/expiry": {
      "post": {
        "summary": "Set/extend expiry",
        "tags": [
          "Lifecycle"
        ],
        "parameters": [
          {
            "name": "slug",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "action": {
                    "type": "string",
                    "enum": [
                      "extend",
                      "permanent",
                      "custom"
                    ]
                  },
                  "expiresAt": {
                    "type": "integer",
                    "description": "Unix ms — required when action=custom"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Updated expiry"
          }
        }
      }
    },
    "/api/documents/{slug}/export": {
      "get": {
        "summary": "Export document data",
        "description": "Returns full JSON bundle: metadata + all versions (HTML inlined) + all comments.",
        "tags": [
          "Documents"
        ],
        "parameters": [
          {
            "name": "slug",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Export bundle"
          },
          "404": {
            "description": "Not found"
          }
        }
      }
    },
    "/api/comments": {
      "get": {
        "summary": "List comments",
        "tags": [
          "Comments"
        ],
        "parameters": [
          {
            "name": "slug",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Threaded comment list",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "slug": {
                      "type": "string"
                    },
                    "currentVersionId": {
                      "type": "string",
                      "nullable": true
                    },
                    "count": {
                      "type": "integer"
                    },
                    "unresolvedCount": {
                      "type": "integer"
                    },
                    "comments": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Comment"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      "post": {
        "summary": "Post a comment or reply",
        "tags": [
          "Comments"
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "slug",
                  "body"
                ],
                "properties": {
                  "slug": {
                    "type": "string"
                  },
                  "body": {
                    "type": "string",
                    "maxLength": 5000
                  },
                  "selector": {
                    "type": "string",
                    "description": "CSS selector (top-level only)"
                  },
                  "rect": {
                    "type": "object",
                    "properties": {
                      "x": {
                        "type": "number"
                      },
                      "y": {
                        "type": "number"
                      },
                      "w": {
                        "type": "number"
                      },
                      "h": {
                        "type": "number"
                      }
                    },
                    "description": "Bounding rect (top-level only)"
                  },
                  "elementText": {
                    "type": "string",
                    "description": "First 80 chars of element text"
                  },
                  "parentId": {
                    "type": "string",
                    "description": "Set for replies; omit for top-level"
                  },
                  "reproduceNote": {
                    "type": "string",
                    "maxLength": 300,
                    "description": "Optional hint for reviewers on how to reach the UI state (top-level only)."
                  },
                  "breadcrumbs": {
                    "type": "array",
                    "maxItems": 8,
                    "description": "Optional interaction steps recorded before picking (top-level only).",
                    "items": {
                      "type": "object",
                      "required": [
                        "selector",
                        "text"
                      ],
                      "properties": {
                        "selector": {
                          "type": "string",
                          "maxLength": 300
                        },
                        "text": {
                          "type": "string",
                          "maxLength": 60
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Comment created"
          },
          "400": {
            "description": "Validation error"
          },
          "404": {
            "description": "Document not found"
          }
        }
      }
    },
    "/api/review": {
      "get": {
        "summary": "Read feedback for iteration (agent loop)",
        "description": "Agent-shaped feedback digest: open threads by default, each with a source-findable anchor (quote + reproduceNote + breadcrumbs) and a `cursor` to pass back as `since` for idempotent polling. Loop: get_review → edit → new version → resolve.",
        "tags": [
          "Comments"
        ],
        "parameters": [
          {
            "name": "slug",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "status",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "enum": [
                "open",
                "resolved",
                "all"
              ],
              "default": "open"
            }
          },
          {
            "name": "since",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            },
            "description": "ISO 8601 or epoch-ms. Return only threads with new activity after this (pass the previous response's cursor)."
          }
        ],
        "responses": {
          "200": {
            "description": "Review digest",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "slug": {
                      "type": "string"
                    },
                    "title": {
                      "type": "string",
                      "nullable": true
                    },
                    "url": {
                      "type": "string"
                    },
                    "currentVersionId": {
                      "type": "string",
                      "nullable": true
                    },
                    "filter": {
                      "type": "object",
                      "properties": {
                        "status": {
                          "type": "string",
                          "enum": [
                            "open",
                            "resolved",
                            "all"
                          ]
                        },
                        "since": {
                          "type": "string",
                          "format": "date-time",
                          "nullable": true
                        }
                      }
                    },
                    "totalThreads": {
                      "type": "integer"
                    },
                    "openThreads": {
                      "type": "integer"
                    },
                    "returned": {
                      "type": "integer"
                    },
                    "cursor": {
                      "type": "string",
                      "format": "date-time",
                      "nullable": true,
                      "description": "Pass back as ?since= next call."
                    },
                    "generatedAt": {
                      "type": "string",
                      "format": "date-time"
                    },
                    "note": {
                      "type": "string"
                    },
                    "threads": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "id": {
                            "type": "string"
                          },
                          "status": {
                            "type": "string",
                            "enum": [
                              "open",
                              "resolved"
                            ]
                          },
                          "onCurrentVersion": {
                            "type": "boolean"
                          },
                          "versionId": {
                            "type": "string"
                          },
                          "author": {
                            "type": "object",
                            "properties": {
                              "email": {
                                "type": "string"
                              },
                              "name": {
                                "type": "string",
                                "nullable": true
                              }
                            }
                          },
                          "createdAt": {
                            "type": "string",
                            "format": "date-time"
                          },
                          "lastActivityAt": {
                            "type": "string",
                            "format": "date-time"
                          },
                          "comment": {
                            "type": "string"
                          },
                          "anchor": {
                            "type": "object",
                            "properties": {
                              "quote": {
                                "type": "string",
                                "nullable": true,
                                "description": "Element text — find THIS in source."
                              },
                              "reproduceNote": {
                                "type": "string",
                                "nullable": true
                              },
                              "breadcrumbs": {
                                "type": "array",
                                "nullable": true,
                                "items": {
                                  "type": "object",
                                  "properties": {
                                    "selector": {
                                      "type": "string"
                                    },
                                    "text": {
                                      "type": "string"
                                    }
                                  }
                                }
                              },
                              "selector": {
                                "type": "string",
                                "nullable": true,
                                "description": "Rendered-DOM selector, not a source locator."
                              }
                            }
                          },
                          "replies": {
                            "type": "array",
                            "items": {
                              "type": "object",
                              "properties": {
                                "author": {
                                  "type": "object"
                                },
                                "body": {
                                  "type": "string"
                                },
                                "createdAt": {
                                  "type": "string",
                                  "format": "date-time"
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Invalid status"
          },
          "404": {
            "description": "Document not found"
          }
        }
      }
    },
    "/api/comments/{id}/attachments": {
      "post": {
        "summary": "Attach an image to a comment",
        "description": "Attach ONE image (paste/drag screenshot) to a top-level comment. multipart/form-data with a 'file' field, or a raw image body. Validated by magic bytes (png/jpeg/webp/gif; SVG rejected), max 5 MB. Stored content-addressed in R2, served from the content origin gated by doc visibility.",
        "tags": [
          "Comments"
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "content": {
            "multipart/form-data": {
              "schema": {
                "type": "object",
                "required": [
                  "file"
                ],
                "properties": {
                  "file": {
                    "type": "string",
                    "format": "binary"
                  },
                  "width": {
                    "type": "integer"
                  },
                  "height": {
                    "type": "integer"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Attached",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "id": {
                      "type": "string"
                    },
                    "url": {
                      "type": "string"
                    },
                    "mime": {
                      "type": "string"
                    },
                    "width": {
                      "type": "integer",
                      "nullable": true
                    },
                    "height": {
                      "type": "integer",
                      "nullable": true
                    },
                    "size": {
                      "type": "integer"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Not a supported image, or comment is a reply"
          },
          "403": {
            "description": "Not author/owner, or scope/CSRF missing"
          },
          "409": {
            "description": "Comment already has an attachment"
          },
          "413": {
            "description": "Image exceeds 5 MB"
          }
        }
      }
    },
    "/api/comments/{id}": {
      "delete": {
        "summary": "Delete a comment",
        "description": "Deletes a comment. If it is a top-level comment, its replies are also deleted. Only the comment author or document owner may delete. Returns 404 for unknown id or unauthorized callers (no existence leak).",
        "tags": [
          "Comments"
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "{\"deleted\":true}"
          },
          "401": {
            "description": "Not authenticated"
          },
          "403": {
            "description": "Scope required: comment, or CSRF header missing"
          },
          "404": {
            "description": "Not found or not authorized"
          }
        }
      }
    },
    "/api/comments/{id}/resolve": {
      "post": {
        "summary": "Resolve a comment",
        "tags": [
          "Comments"
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "{\"ok\":true,\"resolved\":true}"
          }
        }
      }
    },
    "/api/comments/{id}/unresolve": {
      "post": {
        "summary": "Unresolve a comment",
        "tags": [
          "Comments"
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "{\"ok\":true,\"resolved\":false}"
          }
        }
      }
    },
    "/api/dashboard": {
      "get": {
        "summary": "Dashboard aggregate",
        "description": "All documents owned by the authenticated user with version and comment counts.",
        "tags": [
          "Dashboard"
        ],
        "responses": {
          "200": {
            "description": "Dashboard data",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "documents": {
                      "type": "array",
                      "items": {
                        "allOf": [
                          {
                            "$ref": "#/components/schemas/Document"
                          },
                          {
                            "type": "object",
                            "properties": {
                              "versionCount": {
                                "type": "integer"
                              },
                              "commentCount": {
                                "type": "integer"
                              },
                              "unresolvedCount": {
                                "type": "integer"
                              }
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}