{
  "openapi": "3.1.0",
  "info": {
    "title": "Rule Engine API (demo)",
    "version": "2026-11-27",
    "description": "Validates parameters extracted by an AI engine against the operator's Rule Table and returns PASS / REVIEW / FAIL with a reason for every rule.\n\n**Demo key:** `rk_demo_8Jt3VxQm2LpN` (click *Authorize*). A revoked key `rk_demo_revoked_0000` is also available for testing.\n\n**Verdicts**\n- `PASS`: meets the standard.\n- `REVIEW`: needs a human check (close to the limit, value missing, low extraction confidence, unknown unit, unreadable value, or an internal standard that asks for review).\n- `FAIL`: does not meet the standard.\n\nThe overall verdict is the worst verdict among all rules.\n\n**Rule visibility:** partners never see the Rule Table. A rule's limit appears in the response only when the operator turns on `disclose_limit` for that rule.\n\n**Rate limit:** 60 requests per minute per key. Responses carry `X-RateLimit-Limit` and `X-RateLimit-Remaining`.\n\nDemo limits come from the Korean MFDS Cosmetics Safety Standards, Article 6 (in force 2026-03-18). Rule `R-900` is a sample internal standard whose limit is hidden."
  },
  "servers": [
    {
      "url": "/"
    }
  ],
  "security": [
    {
      "ApiKeyAuth": []
    }
  ],
  "tags": [
    {
      "name": "Evaluations",
      "description": "Run the Rule Engine on extracted parameters."
    },
    {
      "name": "Rule sets",
      "description": "See which parameters a rule set expects. Limits are not exposed."
    },
    {
      "name": "System",
      "description": "Service status."
    }
  ],
  "paths": {
    "/v1/evaluations": {
      "post": {
        "tags": [
          "Evaluations"
        ],
        "operationId": "createEvaluation",
        "summary": "Evaluate extracted parameters",
        "description": "Send every parameter the AI engine extracted from one document. Use `value: null` when a value was not found. Parameters that no rule uses are listed in `ignored_parameters`.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/EvaluationRequest"
              },
              "examples": {
                "pass": {
                  "summary": "All within limits (PASS)",
                  "value": {
                    "rule_set": "kr-cosmetics-coa",
                    "product_type": "general",
                    "document_id": "COA-2026-0001",
                    "parameters": [
                      {
                        "key": "lead",
                        "value": 3.2,
                        "unit": "ppm",
                        "confidence": 0.97
                      },
                      {
                        "key": "nickel",
                        "value": 1.1,
                        "unit": "ug/g",
                        "confidence": 0.95
                      },
                      {
                        "key": "arsenic",
                        "value": "<0.5",
                        "unit": "ug/g",
                        "confidence": 0.93
                      },
                      {
                        "key": "mercury",
                        "value": 0.05,
                        "unit": "mg/kg",
                        "confidence": 0.96
                      },
                      {
                        "key": "antimony",
                        "value": 0.4,
                        "unit": "ug/g",
                        "confidence": 0.94
                      },
                      {
                        "key": "cadmium",
                        "value": "ND",
                        "unit": "ug/g",
                        "confidence": 0.9
                      },
                      {
                        "key": "total_aerobic_count",
                        "value": 120,
                        "unit": "CFU/g",
                        "confidence": 0.92
                      },
                      {
                        "key": "e_coli",
                        "value": "Not detected",
                        "confidence": 0.99
                      },
                      {
                        "key": "p_aeruginosa",
                        "value": "Not detected",
                        "confidence": 0.98
                      },
                      {
                        "key": "s_aureus",
                        "value": "Negative",
                        "confidence": 0.98
                      }
                    ]
                  }
                },
                "review": {
                  "summary": "Near limit, missing value, low confidence (REVIEW)",
                  "value": {
                    "rule_set": "kr-cosmetics-coa",
                    "product_type": "baby",
                    "document_id": "COA-2026-0002",
                    "parameters": [
                      {
                        "key": "lead",
                        "value": 19,
                        "unit": "ppm",
                        "confidence": 0.97
                      },
                      {
                        "key": "nickel",
                        "value": 1.1,
                        "unit": "ug/g",
                        "confidence": 0.95
                      },
                      {
                        "key": "arsenic",
                        "value": "<0.5",
                        "unit": "ug/g",
                        "confidence": 0.93
                      },
                      {
                        "key": "antimony",
                        "value": 0.4,
                        "unit": "ug/g",
                        "confidence": 0.94
                      },
                      {
                        "key": "cadmium",
                        "value": 1.2,
                        "unit": "ug/g",
                        "confidence": 0.62
                      },
                      {
                        "key": "total_aerobic_count",
                        "value": 480,
                        "unit": "CFU/g",
                        "confidence": 0.92
                      },
                      {
                        "key": "e_coli",
                        "value": "Not detected",
                        "confidence": 0.99
                      },
                      {
                        "key": "p_aeruginosa",
                        "value": "Not detected",
                        "confidence": 0.98
                      },
                      {
                        "key": "s_aureus",
                        "value": "Negative",
                        "confidence": 0.98
                      }
                    ]
                  }
                },
                "fail": {
                  "summary": "Over the limit and pathogen detected (FAIL)",
                  "value": {
                    "rule_set": "kr-cosmetics-coa",
                    "product_type": "general",
                    "document_id": "COA-2026-0003",
                    "parameters": [
                      {
                        "key": "lead",
                        "value": 3.2,
                        "unit": "ppm",
                        "confidence": 0.97
                      },
                      {
                        "key": "nickel",
                        "value": 30,
                        "unit": "ug/g",
                        "confidence": 0.95
                      },
                      {
                        "key": "arsenic",
                        "value": "<0.5",
                        "unit": "ug/g",
                        "confidence": 0.93
                      },
                      {
                        "key": "mercury",
                        "value": 0.05,
                        "unit": "mg/kg",
                        "confidence": 0.96
                      },
                      {
                        "key": "antimony",
                        "value": 0.4,
                        "unit": "ug/g",
                        "confidence": 0.94
                      },
                      {
                        "key": "cadmium",
                        "value": "ND",
                        "unit": "ug/g",
                        "confidence": 0.9
                      },
                      {
                        "key": "total_aerobic_count",
                        "value": 120,
                        "unit": "CFU/g",
                        "confidence": 0.92
                      },
                      {
                        "key": "e_coli",
                        "value": "Detected",
                        "confidence": 0.99
                      },
                      {
                        "key": "p_aeruginosa",
                        "value": "Not detected",
                        "confidence": 0.98
                      },
                      {
                        "key": "s_aureus",
                        "value": "Negative",
                        "confidence": 0.98
                      }
                    ]
                  }
                },
                "eye_makeup": {
                  "summary": "Same nickel value, eye make-up limit (PASS)",
                  "value": {
                    "rule_set": "kr-cosmetics-coa",
                    "product_type": "eye_makeup",
                    "document_id": "COA-2026-0004",
                    "parameters": [
                      {
                        "key": "lead",
                        "value": 3.2,
                        "unit": "ppm",
                        "confidence": 0.97
                      },
                      {
                        "key": "nickel",
                        "value": 30,
                        "unit": "ug/g",
                        "confidence": 0.95
                      },
                      {
                        "key": "arsenic",
                        "value": "<0.5",
                        "unit": "ug/g",
                        "confidence": 0.93
                      },
                      {
                        "key": "mercury",
                        "value": 0.05,
                        "unit": "mg/kg",
                        "confidence": 0.96
                      },
                      {
                        "key": "antimony",
                        "value": 0.4,
                        "unit": "ug/g",
                        "confidence": 0.94
                      },
                      {
                        "key": "cadmium",
                        "value": "ND",
                        "unit": "ug/g",
                        "confidence": 0.9
                      },
                      {
                        "key": "total_aerobic_count",
                        "value": 120,
                        "unit": "CFU/g",
                        "confidence": 0.92
                      },
                      {
                        "key": "e_coli",
                        "value": "Not detected",
                        "confidence": 0.99
                      },
                      {
                        "key": "p_aeruginosa",
                        "value": "Not detected",
                        "confidence": 0.98
                      },
                      {
                        "key": "s_aureus",
                        "value": "Negative",
                        "confidence": 0.98
                      }
                    ]
                  }
                },
                "invalid": {
                  "summary": "Malformed request (422)",
                  "value": {
                    "product_type": "general",
                    "parameters": [
                      {
                        "value": 3.2
                      },
                      {
                        "key": "lead",
                        "value": 1,
                        "confidence": 1.5
                      }
                    ]
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Evaluation result",
            "headers": {
              "X-RateLimit-Remaining": {
                "$ref": "#/components/headers/RateRemaining"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/EvaluationResponse"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/InvalidJson"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "404": {
            "description": "Rule set not found",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "example": {
                  "error": {
                    "code": "RULE_SET_NOT_FOUND",
                    "message": "Rule set \"x\" does not exist."
                  }
                }
              }
            }
          },
          "413": {
            "description": "Body larger than 64 KB",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "example": {
                  "error": {
                    "code": "PAYLOAD_TOO_LARGE",
                    "message": "Body must be 65536 bytes or less."
                  }
                }
              }
            }
          },
          "415": {
            "description": "Content-Type is not application/json",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "example": {
                  "error": {
                    "code": "UNSUPPORTED_MEDIA_TYPE",
                    "message": "Content-Type must be application/json."
                  }
                }
              }
            }
          },
          "422": {
            "$ref": "#/components/responses/ValidationError"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        }
      }
    },
    "/v1/rule-sets": {
      "get": {
        "tags": [
          "Rule sets"
        ],
        "operationId": "listRuleSets",
        "summary": "List rule sets",
        "description": "Returns the parameters each rule set expects. Limit values are never returned.",
        "responses": {
          "200": {
            "description": "Rule sets",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "data"
                  ],
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/RuleSet"
                      }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        }
      }
    },
    "/v1/rule-sets/{ruleSetId}": {
      "get": {
        "tags": [
          "Rule sets"
        ],
        "operationId": "getRuleSet",
        "summary": "Get one rule set",
        "parameters": [
          {
            "name": "ruleSetId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            },
            "example": "kr-cosmetics-coa"
          }
        ],
        "responses": {
          "200": {
            "description": "Rule set",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/RuleSet"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "404": {
            "description": "Rule set not found",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "example": {
                  "error": {
                    "code": "RULE_SET_NOT_FOUND",
                    "message": "Rule set \"x\" does not exist."
                  }
                }
              }
            }
          }
        }
      }
    },
    "/v1/health": {
      "get": {
        "tags": [
          "System"
        ],
        "operationId": "health",
        "summary": "Health check",
        "security": [],
        "responses": {
          "200": {
            "description": "Service is up",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "status": {
                      "type": "string",
                      "example": "ok"
                    },
                    "version": {
                      "type": "string",
                      "example": "2026-11-27"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "ApiKeyAuth": {
        "type": "apiKey",
        "in": "header",
        "name": "X-API-Key",
        "description": "Partner API key issued by the operator. Keys are stored only as SHA-256 hashes and can be revoked at any time."
      }
    },
    "headers": {
      "RateRemaining": {
        "description": "Requests left in the current minute",
        "schema": {
          "type": "integer"
        }
      }
    },
    "responses": {
      "InvalidJson": {
        "description": "Body is not valid JSON",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            },
            "example": {
              "error": {
                "code": "INVALID_JSON",
                "message": "Body is not valid JSON."
              }
            }
          }
        }
      },
      "Unauthorized": {
        "description": "Missing, invalid or revoked API key (`MISSING_API_KEY`, `INVALID_API_KEY`, `API_KEY_REVOKED`)",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            },
            "example": {
              "error": {
                "code": "INVALID_API_KEY",
                "message": "The API key is not valid."
              }
            }
          }
        }
      },
      "RateLimited": {
        "description": "Too many requests",
        "headers": {
          "Retry-After": {
            "schema": {
              "type": "integer"
            },
            "description": "Seconds to wait"
          }
        },
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            },
            "example": {
              "error": {
                "code": "RATE_LIMITED",
                "message": "Too many requests. The limit is 60 per minute per key."
              }
            }
          }
        }
      },
      "ValidationError": {
        "description": "Request body failed validation. `details` lists every problem with its field path.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            },
            "example": {
              "error": {
                "code": "VALIDATION_ERROR",
                "message": "Request body failed validation.",
                "details": [
                  {
                    "field": "rule_set",
                    "code": "REQUIRED",
                    "message": "rule_set is required."
                  },
                  {
                    "field": "parameters[0].key",
                    "code": "REQUIRED",
                    "message": "key is required."
                  },
                  {
                    "field": "parameters[1].confidence",
                    "code": "OUT_OF_RANGE",
                    "message": "confidence must be a number between 0 and 1."
                  }
                ]
              }
            }
          }
        }
      }
    },
    "schemas": {
      "EvaluationRequest": {
        "type": "object",
        "required": [
          "rule_set",
          "product_type",
          "parameters"
        ],
        "properties": {
          "rule_set": {
            "type": "string",
            "example": "kr-cosmetics-coa"
          },
          "product_type": {
            "type": "string",
            "description": "One of the rule set's `product_types`.",
            "example": "general"
          },
          "document_id": {
            "type": "string",
            "description": "Your own reference for the source document. Returned as is.",
            "example": "COA-2026-0001"
          },
          "parameters": {
            "type": "array",
            "minItems": 1,
            "maxItems": 200,
            "items": {
              "$ref": "#/components/schemas/ExtractedParameter"
            }
          }
        }
      },
      "ExtractedParameter": {
        "type": "object",
        "required": [
          "key",
          "value"
        ],
        "properties": {
          "key": {
            "type": "string",
            "description": "Parameter name. Keys must be unique within a request.",
            "example": "lead"
          },
          "value": {
            "description": "Number, numeric text (`\"<0.5\"` is judged as 0.5), `\"ND\"`/`\"Not detected\"`, or `null` when not found.",
            "example": 3.2,
            "type": [
              "number",
              "string",
              "boolean",
              "null"
            ]
          },
          "unit": {
            "type": "string",
            "description": "Accepted: ug/g, µg/g, ppm, mg/kg, CFU/g, CFU/mL. Defaults to the rule's unit.",
            "example": "ppm"
          },
          "confidence": {
            "type": "number",
            "minimum": 0,
            "maximum": 1,
            "description": "Extraction confidence from the AI engine. Below the rule set's `min_confidence` the rule returns REVIEW.",
            "example": 0.97
          }
        }
      },
      "Verdict": {
        "type": "string",
        "enum": [
          "PASS",
          "REVIEW",
          "FAIL"
        ]
      },
      "RuleResult": {
        "type": "object",
        "required": [
          "rule_id",
          "parameter",
          "verdict",
          "reason_code",
          "reason",
          "reason_ko"
        ],
        "properties": {
          "rule_id": {
            "type": "string",
            "example": "R-111"
          },
          "parameter": {
            "type": "string",
            "example": "nickel"
          },
          "source": {
            "type": "string",
            "example": "MFDS Cosmetics Safety Standards, Art. 6"
          },
          "verdict": {
            "$ref": "#/components/schemas/Verdict"
          },
          "reason_code": {
            "type": "string",
            "enum": [
              "WITHIN_LIMIT",
              "NEAR_LIMIT",
              "OVER_LIMIT",
              "UNDER_LIMIT",
              "NOT_DETECTED",
              "DETECTED",
              "MISSING_VALUE",
              "LOW_CONFIDENCE",
              "UNIT_MISMATCH",
              "INVALID_VALUE"
            ]
          },
          "reason": {
            "type": "string",
            "example": "Exceeds the limit (≤ 10 ug/g)."
          },
          "reason_ko": {
            "type": "string",
            "example": "기준(≤ 10 ug/g)을 넘었습니다."
          },
          "value": {
            "description": "Value as received",
            "example": 30
          },
          "unit": {
            "type": "string",
            "example": "ug/g"
          },
          "limit": {
            "type": "string",
            "description": "Present only when the operator discloses this rule's limit.",
            "example": "≤ 10 ug/g"
          }
        }
      },
      "EvaluationResponse": {
        "type": "object",
        "required": [
          "evaluation_id",
          "rule_set",
          "rule_set_version",
          "product_type",
          "verdict",
          "summary",
          "results",
          "ignored_parameters",
          "evaluated_at"
        ],
        "properties": {
          "evaluation_id": {
            "type": "string",
            "example": "ev_3k9x2m1q8z7w4v5b"
          },
          "document_id": {
            "type": [
              "string",
              "null"
            ],
            "example": "COA-2026-0003"
          },
          "rule_set": {
            "type": "string",
            "example": "kr-cosmetics-coa"
          },
          "rule_set_version": {
            "type": "string",
            "example": "2026-11-27.1"
          },
          "product_type": {
            "type": "string",
            "example": "general"
          },
          "verdict": {
            "$ref": "#/components/schemas/Verdict"
          },
          "summary": {
            "type": "object",
            "properties": {
              "pass": {
                "type": "integer"
              },
              "review": {
                "type": "integer"
              },
              "fail": {
                "type": "integer"
              }
            }
          },
          "results": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/RuleResult"
            }
          },
          "ignored_parameters": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "key": {
                  "type": "string"
                },
                "reason_code": {
                  "type": "string",
                  "enum": [
                    "UNKNOWN_PARAMETER"
                  ]
                }
              }
            }
          },
          "evaluated_at": {
            "type": "string",
            "format": "date-time"
          }
        }
      },
      "RuleSet": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "example": "kr-cosmetics-coa"
          },
          "name": {
            "type": "string"
          },
          "version": {
            "type": "string",
            "example": "2026-11-27.1"
          },
          "product_types": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "min_confidence": {
            "type": "number",
            "example": 0.8
          },
          "parameters": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "key": {
                  "type": "string"
                },
                "unit": {
                  "type": "string"
                },
                "required": {
                  "type": "boolean"
                }
              }
            }
          }
        }
      },
      "Error": {
        "type": "object",
        "required": [
          "error"
        ],
        "properties": {
          "error": {
            "type": "object",
            "required": [
              "code",
              "message"
            ],
            "properties": {
              "code": {
                "type": "string",
                "example": "VALIDATION_ERROR"
              },
              "message": {
                "type": "string"
              },
              "details": {
                "type": "array",
                "items": {
                  "type": "object",
                  "properties": {
                    "field": {
                      "type": "string"
                    },
                    "code": {
                      "type": "string"
                    },
                    "message": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}