{
  "openapi": "3.1.0",
  "info": {
    "title": "FormsFort API",
    "version": "0.1.0",
    "description": "Public submission, agent registration, and form control-plane APIs for FormsFort."
  },
  "servers": [
    {
      "url": "https://api.formsfort.dev"
    }
  ],
  "paths": {
    "/auth.md": {
      "get": {
        "summary": "Fetch agent registration instructions",
        "responses": {
          "200": {
            "description": "Markdown auth.md file",
            "content": {
              "text/markdown": {
                "schema": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    },
    "/.well-known/api-catalog": {
      "get": {
        "summary": "Fetch the FormsFort API catalog",
        "responses": {
          "200": {
            "description": "API catalog linkset",
            "content": {
              "application/linkset+json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiCatalog"
                }
              }
            }
          }
        }
      }
    },
    "/.well-known/oauth-protected-resource": {
      "get": {
        "summary": "Fetch protected resource metadata",
        "responses": {
          "200": {
            "description": "Protected resource metadata",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProtectedResourceMetadata"
                }
              }
            }
          }
        }
      }
    },
    "/.well-known/oauth-authorization-server": {
      "get": {
        "summary": "Fetch authorization server metadata",
        "responses": {
          "200": {
            "description": "Authorization server metadata",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/AuthorizationServerMetadata"
                }
              }
            }
          }
        }
      }
    },
    "/agent/auth": {
      "post": {
        "summary": "Register an agent-controlled setup session",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/AgentAuthRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Agent credential issued",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/AgentAuthResponse"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    },
    "/submit": {
      "post": {
        "summary": "Submit a public form with access_key in the body",
        "parameters": [
          {
            "name": "X-FormsFort-Dry-Run",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string",
              "enum": ["true", "1"]
            },
            "description": "Validate access key, fields, and origin without queueing email."
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Submission"
              }
            },
            "application/x-www-form-urlencoded": {
              "schema": {
                "$ref": "#/components/schemas/Submission"
              }
            },
            "multipart/form-data": {
              "schema": {
                "$ref": "#/components/schemas/Submission"
              }
            }
          }
        },
        "responses": {
          "200": {
            "$ref": "#/components/responses/SubmissionAccepted"
          },
          "303": {
            "description": "Plain HTML form redirect"
          },
          "400": {
            "$ref": "#/components/responses/Error"
          },
          "429": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    },
    "/submit/{accessKey}": {
      "post": {
        "summary": "Submit a public form with access key in the path",
        "parameters": [
          {
            "name": "X-FormsFort-Dry-Run",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string",
              "enum": ["true", "1"]
            },
            "description": "Validate access key, fields, and origin without queueing email."
          },
          {
            "name": "accessKey",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/SubmissionWithoutAccessKey"
              }
            },
            "multipart/form-data": {
              "schema": {
                "$ref": "#/components/schemas/SubmissionWithoutAccessKey"
              }
            }
          }
        },
        "responses": {
          "200": {
            "$ref": "#/components/responses/SubmissionAccepted"
          },
          "303": {
            "description": "Plain HTML form redirect"
          },
          "400": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    },
    "/v1/forms": {
      "get": {
        "summary": "List forms",
        "security": [
          {
            "FormsFortBearer": []
          },
          {
            "FormsFortSessionCookie": []
          }
        ],
        "responses": {
          "200": {
            "description": "Visible forms",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["forms"],
                  "properties": {
                    "forms": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Form"
                      }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Error"
          }
        }
      },
      "post": {
        "summary": "Create a form",
        "security": [
          {
            "FormsFortBearer": []
          },
          {
            "FormsFortSessionCookie": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateForm"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Created form with one-time access key",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["form"],
                  "properties": {
                    "form": {
                      "$ref": "#/components/schemas/CreatedForm"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/Error"
          },
          "401": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    },
    "/v1/forms/{formId}/recipient-verification": {
      "get": {
        "summary": "Read recipient verification status",
        "security": [
          {
            "FormsFortBearer": []
          },
          {
            "FormsFortSessionCookie": []
          }
        ],
        "parameters": [
          {
            "name": "formId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Recipient verification status",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["verification"],
                  "properties": {
                    "verification": {
                      "$ref": "#/components/schemas/RecipientVerification"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "404": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    },
    "/v1/forms/{formId}/recipient-verifications": {
      "post": {
        "summary": "Send or resend recipient verification email",
        "security": [
          {
            "FormsFortBearer": []
          },
          {
            "FormsFortSessionCookie": []
          }
        ],
        "parameters": [
          {
            "name": "formId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "201": {
            "description": "Recipient verification email queued",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["verification"],
                  "properties": {
                    "verification": {
                      "$ref": "#/components/schemas/RecipientVerification"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "404": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    },
    "/v1/forms/{formId}/integrations/{type}": {
      "patch": {
        "summary": "Update a form integration",
        "security": [
          {
            "FormsFortBearer": []
          },
          {
            "FormsFortSessionCookie": []
          }
        ],
        "parameters": [
          {
            "name": "formId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          },
          {
            "name": "type",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "enum": ["webhook", "slack", "discord", "telegram", "google_sheets"]
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/IntegrationUpdate"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Updated integration"
          },
          "400": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    },
    "/v1/forms/{formId}/integrations/{type}/test": {
      "post": {
        "summary": "Queue a safe integration test",
        "security": [
          {
            "FormsFortBearer": []
          },
          {
            "FormsFortSessionCookie": []
          }
        ],
        "parameters": [
          {
            "name": "formId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          },
          {
            "name": "type",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "enum": ["webhook", "slack", "discord", "telegram", "google_sheets"]
            }
          }
        ],
        "responses": {
          "201": {
            "description": "Integration test queued"
          },
          "400": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "FormsFortBearer": {
        "type": "http",
        "scheme": "bearer"
      },
      "FormsFortSessionCookie": {
        "type": "apiKey",
        "in": "cookie",
        "name": "better-auth.session_token"
      }
    },
    "responses": {
      "SubmissionAccepted": {
        "description": "Submission accepted",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/SubmissionResponse"
            }
          }
        }
      },
      "Error": {
        "description": "Error response",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            }
          }
        }
      }
    },
    "schemas": {
      "ApiCatalog": {
        "type": "object",
        "required": ["linkset"],
        "properties": {
          "linkset": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/LinksetEntry"
            }
          }
        }
      },
      "AgentAuthRequest": {
        "type": "object",
        "required": ["type", "email"],
        "properties": {
          "type": {
            "type": "string",
            "enum": ["user_claimed"]
          },
          "email": {
            "type": "string",
            "format": "email"
          },
          "name": {
            "type": "string"
          },
          "password": {
            "type": "string",
            "minLength": 12
          },
          "requested_credential_type": {
            "type": "string",
            "enum": ["session_token"]
          }
        }
      },
      "AgentAuthResponse": {
        "type": "object",
        "required": ["credential_type", "credential", "scopes"],
        "properties": {
          "registration_id": {
            "type": "string"
          },
          "registration_type": {
            "type": "string"
          },
          "credential_type": {
            "type": "string",
            "enum": ["session_token"]
          },
          "credential": {
            "type": "string"
          },
          "credential_expires": {
            "type": ["string", "null"],
            "format": "date-time"
          },
          "scopes": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "verification_required": {
            "type": "boolean"
          },
          "capabilities": {
            "$ref": "#/components/schemas/AgentCapabilities"
          }
        }
      },
      "AgentCapabilities": {
        "type": "object",
        "properties": {
          "domainRestrictionsDefault": {
            "type": "boolean"
          },
          "dryRunSubmissions": {
            "type": "boolean"
          },
          "recipientVerificationResend": {
            "type": "boolean"
          },
          "recipientVerificationStatus": {
            "type": "boolean"
          }
        }
      },
      "AuthorizationServerMetadata": {
        "type": "object",
        "additionalProperties": true
      },
      "CreateForm": {
        "type": "object",
        "required": ["name", "recipientEmail"],
        "properties": {
          "workspaceId": {
            "type": "string",
            "format": "uuid"
          },
          "workspaceName": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "recipientEmail": {
            "type": "string",
            "format": "email"
          },
          "allowedDomains": {
            "description": "Exact hostnames are available on all plans. Wildcard subdomain patterns such as *.example.com require advanced domain policies.",
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "subjectDefault": {
            "type": "string"
          },
          "fromNameDefault": {
            "type": "string"
          },
          "redirectDefault": {
            "type": "string"
          },
          "validation": {
            "$ref": "#/components/schemas/FieldValidationSettings"
          },
          "replyToMode": {
            "type": "string",
            "enum": ["submitter", "default", "none"]
          }
        }
      },
      "CreatedForm": {
        "allOf": [
          {
            "$ref": "#/components/schemas/Form"
          },
          {
            "type": "object",
            "required": ["accessKey"],
            "properties": {
              "accessKey": {
                "type": "string"
              }
            }
          }
        ]
      },
      "ErrorResponse": {
        "type": "object",
        "required": ["message"],
        "properties": {
          "success": {
            "type": "boolean"
          },
          "message": {
            "type": "string"
          },
          "requestId": {
            "type": "string"
          },
          "errors": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/SubmissionValidationError"
            }
          }
        }
      },
      "FieldValidationSettings": {
        "type": "object",
        "properties": {
          "fields": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/FieldValidationRule"
            }
          }
        }
      },
      "FieldValidationRule": {
        "type": "object",
        "required": ["field"],
        "properties": {
          "field": {
            "type": "string"
          },
          "required": {
            "type": "boolean"
          },
          "type": {
            "type": "string",
            "enum": ["email", "url", "number"]
          },
          "minLength": {
            "type": "integer",
            "minimum": 0
          },
          "maxLength": {
            "type": "integer",
            "minimum": 0
          },
          "regex": {
            "type": "string",
            "description": "Safe JavaScript regex subset. Groups, alternation, backreferences, lookarounds, and wildcard repetition are rejected."
          },
          "file": {
            "type": "object",
            "properties": {
              "required": {
                "type": "boolean"
              },
              "allowedMimeTypes": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              }
            }
          }
        }
      },
      "Form": {
        "type": "object",
        "required": ["id", "name", "recipientEmail", "recipientVerified", "accessKeyPrefix"],
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "workspaceId": {
            "type": "string",
            "format": "uuid"
          },
          "name": {
            "type": "string"
          },
          "recipientEmail": {
            "type": "string",
            "format": "email"
          },
          "recipientVerified": {
            "type": "boolean"
          },
          "accessKeyPrefix": {
            "type": "string"
          },
          "allowedDomains": {
            "description": "Exact hostnames and paid wildcard subdomain patterns configured for browser Origin or Referer checks.",
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "subjectDefault": {
            "type": "string"
          },
          "fromNameDefault": {
            "type": "string"
          },
          "validation": {
            "$ref": "#/components/schemas/FieldValidationSettings"
          }
        }
      },
      "IntegrationUpdate": {
        "type": "object",
        "properties": {
          "enabled": {
            "type": "boolean"
          },
          "targetUrl": {
            "type": "string",
            "format": "uri"
          },
          "chatId": {
            "type": "string"
          },
          "spreadsheetId": {
            "type": "string"
          },
          "sheetName": {
            "type": "string"
          }
        }
      },
      "LinksetEntry": {
        "type": "object",
        "required": ["anchor"],
        "additionalProperties": true,
        "properties": {
          "anchor": {
            "type": "string",
            "format": "uri"
          },
          "service-desc": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/LinksetLink"
            }
          },
          "service-doc": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/LinksetLink"
            }
          },
          "service-meta": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/LinksetLink"
            }
          },
          "status": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/LinksetLink"
            }
          }
        }
      },
      "LinksetLink": {
        "type": "object",
        "required": ["href"],
        "additionalProperties": true,
        "properties": {
          "href": {
            "type": "string",
            "format": "uri"
          },
          "title": {
            "type": "string"
          },
          "type": {
            "type": "string"
          }
        }
      },
      "ProtectedResourceMetadata": {
        "type": "object",
        "required": ["resource", "resource_name", "authorization_servers", "scopes_supported"],
        "properties": {
          "resource": {
            "type": "string",
            "format": "uri"
          },
          "resource_name": {
            "type": "string"
          },
          "authorization_servers": {
            "type": "array",
            "items": {
              "type": "string",
              "format": "uri"
            }
          },
          "scopes_supported": {
            "type": "array",
            "items": {
              "type": "string"
            }
          }
        }
      },
      "RecipientVerification": {
        "type": "object",
        "required": ["formId", "recipientEmail", "status"],
        "properties": {
          "formId": {
            "type": "string",
            "format": "uuid"
          },
          "recipientEmail": {
            "type": "string",
            "format": "email"
          },
          "status": {
            "type": "string",
            "enum": ["pending", "verified"]
          },
          "expiresAt": {
            "type": "string",
            "format": "date-time"
          },
          "deliveryId": {
            "type": "string"
          },
          "nextStep": {
            "type": "string"
          }
        }
      },
      "Submission": {
        "type": "object",
        "required": ["access_key"],
        "additionalProperties": true,
        "properties": {
          "access_key": {
            "type": "string"
          },
          "email": {
            "type": "string",
            "format": "email"
          },
          "subject": {
            "type": "string"
          },
          "from_name": {
            "type": "string"
          },
          "replyto": {
            "type": "string",
            "format": "email"
          },
          "redirect": {
            "type": "string",
            "format": "uri"
          },
          "botcheck": {
            "type": ["string", "boolean"]
          },
          "webhook": {
            "type": "string",
            "format": "uri"
          }
        }
      },
      "SubmissionWithoutAccessKey": {
        "type": "object",
        "additionalProperties": true,
        "properties": {
          "email": {
            "type": "string",
            "format": "email"
          },
          "subject": {
            "type": "string"
          },
          "from_name": {
            "type": "string"
          },
          "replyto": {
            "type": "string",
            "format": "email"
          },
          "redirect": {
            "type": "string",
            "format": "uri"
          },
          "botcheck": {
            "type": ["string", "boolean"]
          },
          "webhook": {
            "type": "string",
            "format": "uri"
          }
        }
      },
      "SubmissionValidationError": {
        "type": "object",
        "required": ["field", "rule", "message"],
        "properties": {
          "field": {
            "type": "string"
          },
          "rule": {
            "type": "string",
            "enum": [
              "email",
              "file_required",
              "file_type",
              "max_length",
              "min_length",
              "number",
              "regex",
              "required",
              "url"
            ]
          },
          "message": {
            "type": "string"
          }
        }
      },
      "SubmissionResponse": {
        "type": "object",
        "required": ["success", "message"],
        "properties": {
          "success": {
            "type": "boolean"
          },
          "message": {
            "type": "string"
          },
          "requestId": {
            "type": "string"
          },
          "deliveryId": {
            "type": "string"
          },
          "dryRun": {
            "type": "boolean"
          },
          "errors": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/SubmissionValidationError"
            }
          },
          "validation": {
            "type": "object",
            "properties": {
              "deliveryQueued": {
                "type": "boolean"
              },
              "fieldNames": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "recipientVerified": {
                "type": "boolean"
              }
            }
          },
          "body": {
            "type": "object",
            "additionalProperties": true
          }
        }
      }
    }
  }
}
