{
  "openapi": "3.0.3",
  "info": {
    "title": "Datamedia WhatsApp Billing API",
    "version": "1.0.0",
    "description": "API server-to-server untuk billing. Langsung terhubung Meta WhatsApp Cloud API. Gunakan HTTPS di produksi. Endpoint send-template mengikuti penamaan MeeChat, bukan kompatibilitas penuh. Nomor internasional wajib; pastikan persetujuan pelanggan."
  },
  "servers": [
    {
      "url": "/"
    }
  ],
  "security": [
    {
      "BearerAuth": []
    },
    {
      "ApiKey": []
    }
  ],
  "paths": {
    "/api/v1/notifications": {
      "post": {
        "summary": "Kirim template BODY berurutan",
        "description": "external_id wajib dan idempotent lintas kedua endpoint. Payload berbeda untuk ID sama menghasilkan 409. Tidak ada pengiriman ulang otomatis. Hanya parameter BODY berurutan; header, media, tombol dinamis, dan parameter bernama tidak didukung.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "external_id",
                  "to",
                  "template",
                  "language"
                ],
                "properties": {
                  "external_id": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 128,
                    "pattern": "^[a-zA-Z0-9._:-]+$"
                  },
                  "to": {
                    "type": "string",
                    "pattern": "^\\+?[1-9]\\d{7,14}$"
                  },
                  "template": {
                    "type": "string",
                    "pattern": "^[a-z0-9_]+$"
                  },
                  "language": {
                    "type": "string",
                    "pattern": "^[a-z]{2,3}(?:_[A-Z]{2})?$"
                  },
                  "parameters": {
                    "type": "array",
                    "maxItems": 30,
                    "items": {
                      "type": "string",
                      "minLength": 1,
                      "maxLength": 1024,
                      "pattern": "^[^\\r\\n\\t]+$"
                    },
                    "default": []
                  },
                  "header": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 1024,
                    "pattern": "^[^\r\n\t]+$",
                    "description": "Teks untuk parameter header template, jika template memakai {{1}} pada header."
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "ID yang sama sudah diproses; periksa status dan duplicate.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Message"
                }
              }
            }
          },
          "201": {
            "description": "Meta menerima permintaan, belum berarti terkirim.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Message"
                }
              }
            }
          },
          "400": {
            "description": "Input tidak valid"
          },
          "401": {
            "description": "Token tidak valid"
          },
          "409": {
            "description": "ID sudah dipakai untuk payload berbeda"
          },
          "413": {
            "description": "Payload melebihi 1 MiB"
          },
          "415": {
            "description": "Content-Type harus application/json"
          },
          "502": {
            "description": "Pengiriman failed atau unknown; jangan otomatis buat ID baru.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Message"
                }
              }
            }
          },
          "503": {
            "description": "Konfigurasi Meta belum lengkap"
          }
        }
      }
    },
    "/api/v1/send-template": {
      "post": {
        "summary": "Kirim template BODY berurutan (pola MeeChat)",
        "description": "external_id wajib dan idempotent lintas kedua endpoint. Payload berbeda untuk ID sama menghasilkan 409. Tidak ada pengiriman ulang otomatis. Hanya parameter BODY berurutan; header, media, tombol dinamis, dan parameter bernama tidak didukung.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "external_id",
                  "phone_number",
                  "name_template",
                  "language_code"
                ],
                "properties": {
                  "external_id": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 128,
                    "pattern": "^[a-zA-Z0-9._:-]+$"
                  },
                  "phone_number": {
                    "type": "string",
                    "pattern": "^\\+?[1-9]\\d{7,14}$"
                  },
                  "name_template": {
                    "type": "string",
                    "pattern": "^[a-z0-9_]+$"
                  },
                  "language_code": {
                    "type": "string",
                    "pattern": "^[a-z]{2,3}(?:_[A-Z]{2})?$"
                  },
                  "body_params": {
                    "type": "array",
                    "maxItems": 30,
                    "items": {
                      "type": "string",
                      "minLength": 1,
                      "maxLength": 1024,
                      "pattern": "^[^\\r\\n\\t]+$"
                    },
                    "default": []
                  },
                  "header_params": {
                    "type": "array",
                    "maxItems": 1,
                    "items": {
                      "type": "string",
                      "minLength": 1,
                      "maxLength": 1024,
                      "pattern": "^[^\r\n\t]+$"
                    },
                    "description": "Satu teks header, jika template memakai {{1}} pada header."
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "ID yang sama sudah diproses; periksa status dan duplicate.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Message"
                }
              }
            }
          },
          "201": {
            "description": "Meta menerima permintaan, belum berarti terkirim.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Message"
                }
              }
            }
          },
          "400": {
            "description": "Input tidak valid"
          },
          "401": {
            "description": "Token tidak valid"
          },
          "409": {
            "description": "ID sudah dipakai untuk payload berbeda"
          },
          "413": {
            "description": "Payload melebihi 1 MiB"
          },
          "415": {
            "description": "Content-Type harus application/json"
          },
          "502": {
            "description": "Pengiriman failed atau unknown; jangan otomatis buat ID baru.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Message"
                }
              }
            }
          },
          "503": {
            "description": "Konfigurasi Meta belum lengkap"
          }
        }
      }
    },
    "/api/v1/notifications/{external_id}": {
      "get": {
        "summary": "Cek status transaksi",
        "parameters": [
          {
            "name": "external_id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "minLength": 1,
              "maxLength": 128,
              "pattern": "^[a-zA-Z0-9._:-]+$"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Status tersimpan; failed/unknown juga HTTP 200.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Message"
                }
              }
            }
          },
          "401": {
            "description": "Token tidak valid"
          },
          "404": {
            "description": "ID tidak ditemukan"
          }
        }
      }
    },
    "/api/v1/templates": {
      "get": {
        "summary": "Daftar template APPROVED dari Meta",
        "description": "100 hasil per halaman. Gunakan next_cursor sebagai parameter after untuk halaman berikutnya.",
        "responses": {
          "200": {
            "description": "Template Meta",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "templates": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "additionalProperties": true
                      }
                    },
                    "has_more": {
                      "type": "boolean"
                    },
                    "next_cursor": {
                      "type": "string",
                      "nullable": true
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Token tidak valid"
          },
          "502": {
            "description": "Meta tidak merespons atau menolak"
          },
          "503": {
            "description": "Konfigurasi Meta belum lengkap"
          },
          "504": {
            "description": "Timeout Meta"
          }
        },
        "parameters": [
          {
            "name": "after",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "maxLength": 2048
            }
          }
        ]
      }
    }
  },
  "components": {
    "securitySchemes": {
      "BearerAuth": {
        "type": "http",
        "scheme": "bearer"
      },
      "ApiKey": {
        "type": "apiKey",
        "in": "header",
        "name": "X-Api-Key",
        "description": "Isi Bearer TOKEN. Authorization mendapat prioritas jika kedua header dikirim."
      }
    },
    "schemas": {
      "Message": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "external_id": {
            "type": "string"
          },
          "whatsapp_message_id": {
            "type": "string",
            "nullable": true
          },
          "to": {
            "type": "string"
          },
          "status": {
            "type": "string",
            "enum": [
              "pending",
              "accepted",
              "sent",
              "delivered",
              "read",
              "failed",
              "unknown"
            ]
          },
          "error": {
            "type": "string",
            "nullable": true
          },
          "created_at": {
            "type": "integer",
            "format": "int64",
            "description": "Unix milliseconds"
          },
          "duplicate": {
            "type": "boolean"
          }
        }
      }
    }
  }
}
