{
  "openapi": "3.1.0",
  "info": {
    "title": "FormRelay API",
    "version": "1.0.0",
    "description": "Form backend untuk website statis. Endpoint publik POST /f/{key} menerima kiriman form (form HTML, multipart, atau JSON). API privat /v1 (paket Pro dan Agency) membaca dan mengelola kiriman. Status: live. Notifikasi email aktif; WhatsApp, Telegram, dan pembayaran QRIS belum tersedia.",
    "contact": { "url": "https://formrelay.id/bantuan" }
  },
  "servers": [
    { "url": "https://formrelay.id", "description": "Endpoint pengiriman form (publik)" },
    { "url": "https://api.formrelay.id", "description": "API privat v1" }
  ],
  "tags": [
    { "name": "Pengiriman", "description": "Publik. Kunci form ada di URL dan bukan rahasia." },
    { "name": "API kiriman", "description": "Privat. Butuh kunci API Bearer (Pro dan Agency)." }
  ],
  "paths": {
    "/f/{key}": {
      "post": {
        "tags": ["Pengiriman"],
        "operationId": "submitForm",
        "summary": "Kirim data form",
        "description": "Kunci form dibuat manusia di dashboard (https://formrelay.id/masuk). Jangan mengarang kunci. Kirim header Accept: application/json untuk balasan JSON; tanpa itu pengunjung dialihkan ke halaman konfirmasi atau _redirect. Field tak dikenal ikut disimpan sebagai data biasa. Field khusus: _redirect, _subject, _from_name, _notify, _gotcha (honeypot, harus kosong), cf-turnstile-response.",
        "parameters": [
          {
            "name": "key",
            "in": "path",
            "required": true,
            "description": "Kunci form, 8 karakter huruf kecil dan angka, mis. a1b2c3d4. Tidak peka huruf besar/kecil.",
            "schema": { "type": "string", "examples": ["a1b2c3d4"] }
          },
          {
            "name": "Accept",
            "in": "header",
            "required": false,
            "schema": { "type": "string", "examples": ["application/json"] }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/Submission" },
              "example": { "nama": "Budi", "email": "budi@tokokopi.id", "pesan": "Mau tanya harga paket" }
            },
            "application/x-www-form-urlencoded": { "schema": { "$ref": "#/components/schemas/Submission" } },
            "multipart/form-data": {
              "schema": { "$ref": "#/components/schemas/Submission" },
              "description": "Upload file butuh paket Pro atau Agency (maks 5 file, 5 MB per file)."
            }
          }
        },
        "responses": {
          "200": {
            "description": "Kiriman diterima dan disimpan.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/SubmitOk" },
                "example": { "ok": true, "id": "sub_01HZX3K9Q2", "notified": ["email"] }
              }
            }
          },
          "400": { "$ref": "#/components/responses/Error" },
          "403": { "$ref": "#/components/responses/Error" },
          "404": { "$ref": "#/components/responses/Error" },
          "413": { "$ref": "#/components/responses/Error" },
          "422": { "$ref": "#/components/responses/Error" },
          "429": {
            "description": "Terlalu sering atau kuota terlampaui. Hormati header Retry-After (detik).",
            "headers": { "Retry-After": { "schema": { "type": "integer" } } },
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
          }
        }
      },
      "options": {
        "tags": ["Pengiriman"],
        "operationId": "submitFormPreflight",
        "summary": "Preflight CORS",
        "description": "Semua balasan /f/* membawa CORS untuk semua asal, jadi fetch dari browser berfungsi.",
        "parameters": [{ "name": "key", "in": "path", "required": true, "schema": { "type": "string" } }],
        "responses": { "204": { "description": "Preflight diizinkan." } }
      }
    },
    "/v1/forms": {
      "get": {
        "tags": ["API kiriman"],
        "operationId": "listForms",
        "summary": "Daftar form milik akun",
        "security": [{ "bearerAuth": [] }],
        "servers": [{ "url": "https://api.formrelay.id" }],
        "responses": {
          "200": {
            "description": "Daftar form.",
            "content": { "application/json": { "schema": { "type": "object", "properties": { "data": { "type": "array", "items": { "$ref": "#/components/schemas/Form" } } } } } }
          },
          "401": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/v1/forms/{key}/submissions": {
      "get": {
        "tags": ["API kiriman"],
        "operationId": "listSubmissions",
        "summary": "Daftar kiriman sebuah form",
        "security": [{ "bearerAuth": [] }],
        "servers": [{ "url": "https://api.formrelay.id" }],
        "parameters": [
          { "name": "key", "in": "path", "required": true, "schema": { "type": "string" } },
          { "name": "limit", "in": "query", "schema": { "type": "integer", "minimum": 1, "maximum": 100, "default": 25 } },
          { "name": "cursor", "in": "query", "description": "next_cursor dari balasan sebelumnya.", "schema": { "type": "string" } },
          { "name": "since", "in": "query", "description": "Waktu ISO 8601.", "schema": { "type": "string", "format": "date-time" } },
          { "name": "status", "in": "query", "schema": { "type": "string", "enum": ["masuk", "spam", "arsip"], "default": "masuk" } }
        ],
        "responses": {
          "200": {
            "description": "Halaman kiriman.",
            "content": { "application/json": { "schema": { "type": "object", "properties": {
              "data": { "type": "array", "items": { "$ref": "#/components/schemas/StoredSubmission" } },
              "next_cursor": { "type": ["string", "null"] } } } } }
          },
          "401": { "$ref": "#/components/responses/Error" },
          "404": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/v1/submissions/{id}": {
      "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string", "examples": ["sub_01HZX3K9Q2"] } }],
      "get": {
        "tags": ["API kiriman"],
        "operationId": "getSubmission",
        "summary": "Ambil satu kiriman",
        "security": [{ "bearerAuth": [] }],
        "servers": [{ "url": "https://api.formrelay.id" }],
        "responses": {
          "200": { "description": "Kiriman.", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/StoredSubmission" } } } },
          "401": { "$ref": "#/components/responses/Error" },
          "404": { "$ref": "#/components/responses/Error" }
        }
      },
      "patch": {
        "tags": ["API kiriman"],
        "operationId": "updateSubmissionStatus",
        "summary": "Ubah status kiriman",
        "security": [{ "bearerAuth": [] }],
        "servers": [{ "url": "https://api.formrelay.id" }],
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": { "type": "object", "required": ["status"], "properties": { "status": { "type": "string", "enum": ["masuk", "spam", "arsip"] } } } } }
        },
        "responses": {
          "200": { "description": "Status diubah.", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/StoredSubmission" } } } },
          "401": { "$ref": "#/components/responses/Error" },
          "404": { "$ref": "#/components/responses/Error" }
        }
      },
      "delete": {
        "tags": ["API kiriman"],
        "operationId": "deleteSubmission",
        "summary": "Hapus kiriman (permanen)",
        "security": [{ "bearerAuth": [] }],
        "servers": [{ "url": "https://api.formrelay.id" }],
        "responses": {
          "200": { "description": "Dihapus." },
          "401": { "$ref": "#/components/responses/Error" },
          "404": { "$ref": "#/components/responses/Error" }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "Kunci API dari dashboard (menu Akun), awalan fr_live_. Simpan di variabel lingkungan server, jangan di kode browser."
      }
    },
    "schemas": {
      "Submission": {
        "type": "object",
        "description": "Field bebas (nama atribut name di form). Dikenali otomatis: email (Reply-To dan balasan otomatis), nama/name, pesan/message. Batas: 256 KB tanpa file, 100 field, 20.000 karakter per field.",
        "additionalProperties": { "type": "string" },
        "properties": {
          "_redirect": { "type": "string", "format": "uri", "description": "Halaman tujuan setelah berhasil. HTTPS saja." },
          "_subject": { "type": "string" },
          "_from_name": { "type": "string" },
          "_notify": { "type": "string", "description": "Dipisah koma: email,whatsapp,telegram." },
          "_gotcha": { "type": "string", "description": "Honeypot. Harus kosong atau tidak ada; jika terisi, kiriman ditolak sebagai spam." },
          "cf-turnstile-response": { "type": "string" }
        }
      },
      "SubmitOk": {
        "type": "object",
        "required": ["ok", "id"],
        "properties": {
          "ok": { "const": true },
          "id": { "type": "string" },
          "notified": { "type": "array", "items": { "type": "string" } },
          "quota": { "type": "string", "description": "notifikasi_ditunda bila kuota notifikasi habis; kiriman tetap tersimpan." }
        }
      },
      "Form": {
        "type": "object",
        "properties": {
          "key": { "type": "string" },
          "name": { "type": "string" },
          "status": { "type": "string" },
          "submissions_this_month": { "type": "integer" }
        }
      },
      "StoredSubmission": {
        "type": "object",
        "properties": {
          "id": { "type": "string" },
          "created_at": { "type": "string", "format": "date-time" },
          "status": { "type": "string", "enum": ["masuk", "spam", "arsip"] },
          "data": { "type": "object", "additionalProperties": true }
        }
      },
      "Error": {
        "type": "object",
        "required": ["ok", "error"],
        "properties": {
          "ok": { "const": false },
          "error": {
            "type": "string",
            "description": "Kode galat.",
            "enum": [
              "data_tidak_valid", "domain_tidak_diizinkan", "form_ditangguhkan", "form_nonaktif",
              "fitur_pro_diperlukan", "kuota_habis", "kunci_tidak_ditemukan", "kiriman_terlalu_besar",
              "spam_terdeteksi", "phishing_terdeteksi", "tipe_file_tidak_diizinkan", "upload_gagal",
              "terlalu_sering", "kuota_terlampaui"
            ]
          },
          "message": { "type": "string" }
        }
      }
    },
    "responses": {
      "Error": {
        "description": "Galat. Lihat field error untuk kodenya (docs: https://formrelay.id/docs/api).",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      }
    }
  },
  "externalDocs": { "description": "Dokumentasi (Indonesia)", "url": "https://formrelay.id/docs/" }
}
