{
  "openapi": "3.1.0",
  "info": {
    "title": "HS Mate Classification API",
    "version": "1.0.0",
    "summary": "6-digit HS classification with US, EU and UK duty estimates.",
    "description": "Send a plain-English product description and receive an HS code, its official description, a confidence score and band, a duty rate estimate for the destination, alternative candidates and the reasoning. Authentication is a bearer key: trial keys start with hsm_live_ and carry 100 classifications. Every 2xx response includes X-Credits-Remaining. Both POST endpoints accept an optional Idempotency-Key header. This API is advisory and does not replace a binding customs ruling.",
    "contact": {
      "name": "HS Mate support",
      "email": "support@hsmate.com",
      "url": "https://hsmate.com/developers"
    },
    "license": {
      "name": "Proprietary",
      "identifier": "LicenseRef-HSMate-Commercial"
    }
  },
  "servers": [
    {
      "url": "https://hsmate.com",
      "description": "Production"
    }
  ],
  "tags": [
    { "name": "Keys", "description": "Trial key issuing." },
    { "name": "Classification", "description": "Single and batch HS classification." },
    { "name": "Account", "description": "Credit and plan status." }
  ],
  "paths": {
    "/api/v1/keys/trial": {
      "post": {
        "tags": ["Keys"],
        "operationId": "createTrialKey",
        "summary": "Issue a free trial key",
        "description": "Issues a key with 100 classifications. One trial key per email address. No authentication required.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/TrialRequest" },
              "example": { "email": "you@company.com" }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Key issued.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/TrialKey" },
                "example": { "key": "hsm_live_example_key", "credits": 100 }
              }
            }
          },
          "400": {
            "description": "invalid_input: the email is missing or malformed.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Error" },
                "example": {
                  "error": "invalid_input",
                  "message": "A valid email address is required."
                }
              }
            }
          },
          "403": {
            "description": "forbidden: trial issuing is not allowed for this network or origin.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Error" },
                "example": {
                  "error": "forbidden",
                  "message": "Trial keys are not available from this network."
                }
              }
            }
          },
          "409": {
            "description": "trial_already_issued: a trial key already exists for this email.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Error" },
                "example": {
                  "error": "trial_already_issued",
                  "message": "A trial key has already been issued for this email."
                }
              }
            }
          },
          "429": {
            "description": "rate_limited: too many trial requests from this network.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Error" },
                "example": { "error": "rate_limited", "message": "Too many requests. Retry later." }
              }
            }
          },
          "503": {
            "description": "trial_unavailable: trial issuing is paused.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Error" },
                "example": {
                  "error": "trial_unavailable",
                  "message": "Trial keys are temporarily unavailable."
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/classify": {
      "post": {
        "tags": ["Classification"],
        "operationId": "classifyProduct",
        "summary": "Classify one product",
        "description": "Returns the 6-digit HS code, description, confidence, duty rate estimate, alternative candidates and reasoning. Spends one credit. Send an Idempotency-Key to make retries safe.",
        "security": [{ "bearerAuth": [] }],
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": false,
            "description": "Any unique string. Repeating a request with the same key replays the first response instead of spending another credit.",
            "schema": { "type": "string", "maxLength": 255 },
            "example": "classify-sku-1-2026-11-26"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/ClassifyRequest" },
              "example": { "q": "stainless steel vacuum flask", "country": "us" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Classification result.",
            "headers": {
              "X-Credits-Remaining": {
                "description": "Credits left on the key after this request.",
                "schema": { "type": "integer", "minimum": 0 },
                "example": 99
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "description": "A classification, or a review when confidence is below 0.6 (no credit charged).",
                  "oneOf": [
                    { "$ref": "#/components/schemas/ClassifyResult" },
                    { "$ref": "#/components/schemas/ClassifyReview" }
                  ]
                },
                "example": {
                  "code": "9617.00",
                  "description": "Vacuum flasks and other vacuum vessels, complete with cases; parts thereof",
                  "confidence": 0.87,
                  "confidence_band": "high",
                  "duty_rate_estimate": "6.5%",
                  "candidates": [
                    {
                      "code": "9617.00",
                      "description": "Vacuum flasks and other vacuum vessels, complete with cases",
                      "confidence": 0.87
                    },
                    {
                      "code": "7323.93",
                      "description": "Articles of stainless steel, for household use",
                      "confidence": 0.06
                    }
                  ],
                  "reasoning": "Double-walled stainless steel vessel with a screw lid and no electrical parts. Heading 9617 covers vacuum vessels."
                }
              }
            }
          },
          "400": {
            "description": "invalid_input: missing q, q too long, or country not one of us, eu, uk.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Error" },
                "example": {
                  "error": "invalid_input",
                  "message": "country must be one of us, eu, uk."
                }
              }
            }
          },
          "401": {
            "description": "invalid_key: bearer token missing, malformed or revoked.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Error" },
                "example": { "error": "invalid_key", "message": "Provide a valid Bearer key." }
              }
            }
          },
          "402": {
            "description": "insufficient_credits: the key has no credits left.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Error" },
                "example": {
                  "error": "insufficient_credits",
                  "message": "Buy a pack to continue classifying."
                }
              }
            }
          },
          "429": {
            "description": "rate_limited: too many requests from this key.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Error" },
                "example": { "error": "rate_limited", "message": "Too many requests. Retry later." }
              }
            }
          },
          "502": {
            "description": "classification_failed: the classifier could not answer. Safe to retry with an Idempotency-Key.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Error" },
                "example": {
                  "error": "classification_failed",
                  "message": "Classification failed. Retry."
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/batch/classify": {
      "post": {
        "tags": ["Classification"],
        "operationId": "classifyBatch",
        "summary": "Classify up to 50 products",
        "description": "Classifies 1 to 50 items in one request. Each item spends one credit; per-item failures are reported with ok: false and do not abort the batch. Send an Idempotency-Key to make retries safe.",
        "security": [{ "bearerAuth": [] }],
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": false,
            "description": "Any unique string. Repeating a request with the same key replays the first response instead of spending more credits.",
            "schema": { "type": "string", "maxLength": 255 },
            "example": "batch-2026-11-26-a"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/BatchRequest" },
              "example": {
                "items": [
                  { "id": "sku-1", "q": "stainless steel vacuum flask", "country": "us" },
                  { "id": "sku-2", "q": "cotton knitted t-shirt, women, printed", "country": "eu" }
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Batch result, one entry per submitted item, in the same order.",
            "headers": {
              "X-Credits-Remaining": {
                "description": "Credits left on the key after this request.",
                "schema": { "type": "integer", "minimum": 0 },
                "example": 98
              }
            },
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/BatchResult" },
                "example": {
                  "results": [
                    {
                      "id": "sku-1",
                      "ok": true,
                      "code": "9617.00",
                      "description": "Vacuum flasks and other vacuum vessels, complete with cases",
                      "confidence": 0.87,
                      "confidence_band": "high",
                      "duty_rate_estimate": "6.5%",
                      "reasoning": "Insulated stainless steel vessel, no electrical parts."
                    },
                    { "id": "sku-2", "ok": false, "error": "no_match" }
                  ],
                  "credits_charged": 2,
                  "credits_remaining": 98
                }
              }
            }
          },
          "400": {
            "description": "invalid_input: items missing, more than 50 items, or an item is malformed.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Error" },
                "example": {
                  "error": "invalid_input",
                  "message": "items must contain between 1 and 50 entries."
                }
              }
            }
          },
          "401": {
            "description": "invalid_key: bearer token missing, malformed or revoked.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Error" },
                "example": { "error": "invalid_key", "message": "Provide a valid Bearer key." }
              }
            }
          },
          "402": {
            "description": "insufficient_credits: the key cannot cover the batch.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Error" },
                "example": {
                  "error": "insufficient_credits",
                  "message": "Not enough credits for this batch."
                }
              }
            }
          },
          "429": {
            "description": "rate_limited: too many requests from this key.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Error" },
                "example": { "error": "rate_limited", "message": "Too many requests. Retry later." }
              }
            }
          },
          "502": {
            "description": "classification_failed: the classifier could not answer. Safe to retry with an Idempotency-Key.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Error" },
                "example": {
                  "error": "classification_failed",
                  "message": "Classification failed. Retry."
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/usage": {
      "get": {
        "tags": ["Account"],
        "operationId": "getUsage",
        "summary": "Credit and plan status",
        "description": "Returns the plan, credits total, used and remaining, and period_end, which is null for a prepaid pack and a date for a monthly plan.",
        "security": [{ "bearerAuth": [] }],
        "responses": {
          "200": {
            "description": "Current usage.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Usage" },
                "example": {
                  "plan": "pack",
                  "credits_total": 500,
                  "credits_used": 12,
                  "credits_remaining": 488,
                  "period_end": null
                }
              }
            }
          },
          "401": {
            "description": "invalid_key: bearer token missing, malformed or revoked.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Error" },
                "example": { "error": "invalid_key", "message": "Provide a valid Bearer key." }
              }
            }
          },
          "429": {
            "description": "rate_limited: too many requests from this key.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Error" },
                "example": { "error": "rate_limited", "message": "Too many requests. Retry later." }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "hsm_live_<token>",
        "description": "API key issued by POST /api/v1/keys/trial or purchased as a pack. Send as Authorization: Bearer hsm_live_..."
      }
    },
    "schemas": {
      "Country": {
        "type": "string",
        "enum": ["us", "eu", "uk"],
        "default": "us",
        "description": "Destination customs regime used for the duty estimate."
      },
      "ConfidenceBand": {
        "type": "string",
        "enum": ["high", "medium", "low"]
      },
      "ClassifyReview": {
        "type": "object",
        "description": "Returned instead of a code when confidence is below 0.6. No credit is charged.",
        "required": ["needs_review", "confidence", "confidence_band", "candidates", "reasoning"],
        "properties": {
          "needs_review": { "const": true },
          "confidence": { "type": "number", "minimum": 0, "maximum": 1, "example": 0.45 },
          "confidence_band": { "$ref": "#/components/schemas/ConfidenceBand" },
          "candidates": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/Candidate" }
          },
          "reasoning": { "type": "string" },
          "credits_remaining": { "type": "integer", "minimum": 0 }
        }
      },
      "TrialRequest": {
        "type": "object",
        "required": ["email"],
        "properties": {
          "email": {
            "type": "string",
            "format": "email",
            "description": "One trial key per email address."
          }
        }
      },
      "TrialKey": {
        "type": "object",
        "required": ["key", "credits"],
        "properties": {
          "key": {
            "type": "string",
            "description": "Secret bearer token. Store it server-side and never expose it in client code.",
            "example": "hsm_live_example_key"
          },
          "credits": {
            "type": "integer",
            "description": "Classifications available on the new key.",
            "example": 100
          }
        }
      },
      "ClassifyRequest": {
        "type": "object",
        "required": ["q"],
        "properties": {
          "q": {
            "type": "string",
            "minLength": 1,
            "description": "Product description in plain English. Material, function and use decide the heading, so include them.",
            "example": "stainless steel vacuum flask"
          },
          "country": { "$ref": "#/components/schemas/Country" }
        }
      },
      "Candidate": {
        "type": "object",
        "required": ["code", "description", "confidence"],
        "properties": {
          "code": { "type": "string", "description": "HS code, 6 digits.", "example": "9617.00" },
          "description": { "type": "string" },
          "confidence": { "type": "number", "minimum": 0, "maximum": 1 }
        }
      },
      "ClassifyResult": {
        "type": "object",
        "required": [
          "code",
          "description",
          "confidence",
          "confidence_band",
          "duty_rate_estimate",
          "candidates",
          "reasoning"
        ],
        "properties": {
          "code": {
            "type": "string",
            "description": "Recommended HS code at 6 digits.",
            "example": "9617.00"
          },
          "description": { "type": "string" },
          "confidence": {
            "type": "number",
            "minimum": 0,
            "maximum": 1,
            "description": "Model confidence in the recommended code.",
            "example": 0.87
          },
          "confidence_band": { "$ref": "#/components/schemas/ConfidenceBand" },
          "duty_rate_estimate": {
            "type": "string",
            "description": "Ad valorem duty rate for the destination, as published in the tariff schedule. Excludes anti-dumping and countervailing duties, quotas and preferential rates.",
            "example": "6.5%"
          },
          "candidates": {
            "type": "array",
            "description": "Ranked alternatives, best first. The first entry is the recommended code.",
            "items": { "$ref": "#/components/schemas/Candidate" }
          },
          "reasoning": {
            "type": "string",
            "description": "Short explanation of the heading choice."
          }
        }
      },
      "BatchItem": {
        "type": "object",
        "required": ["id", "q"],
        "properties": {
          "id": {
            "type": "string",
            "description": "Caller-chosen identifier, echoed back in the result.",
            "example": "sku-1"
          },
          "q": { "type": "string", "minLength": 1 },
          "country": { "$ref": "#/components/schemas/Country" }
        }
      },
      "BatchRequest": {
        "type": "object",
        "required": ["items"],
        "properties": {
          "items": {
            "type": "array",
            "minItems": 1,
            "maxItems": 50,
            "items": { "$ref": "#/components/schemas/BatchItem" }
          }
        }
      },
      "BatchItemOk": {
        "type": "object",
        "required": [
          "id",
          "ok",
          "code",
          "description",
          "confidence",
          "confidence_band",
          "duty_rate_estimate",
          "reasoning"
        ],
        "properties": {
          "id": { "type": "string" },
          "ok": { "const": true },
          "code": { "type": "string", "example": "9617.00" },
          "description": { "type": "string" },
          "confidence": { "type": "number", "minimum": 0, "maximum": 1 },
          "confidence_band": { "$ref": "#/components/schemas/ConfidenceBand" },
          "duty_rate_estimate": { "type": "string", "example": "6.5%" },
          "reasoning": { "type": "string" }
        }
      },
      "BatchItemError": {
        "type": "object",
        "required": ["id", "ok", "error"],
        "properties": {
          "id": { "type": "string" },
          "ok": { "const": false },
          "error": {
            "type": "string",
            "description": "Per-item failure reason. needs_review means the engine could not reach 0.6 confidence; that item is not charged and carries the candidates.",
            "example": "no_match"
          },
          "confidence": { "type": "number", "minimum": 0, "maximum": 1 },
          "candidates": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/Candidate" }
          },
          "reasoning": { "type": "string" }
        }
      },
      "BatchResult": {
        "type": "object",
        "required": ["results", "credits_charged", "credits_remaining"],
        "properties": {
          "results": {
            "type": "array",
            "items": {
              "oneOf": [
                { "$ref": "#/components/schemas/BatchItemOk" },
                { "$ref": "#/components/schemas/BatchItemError" }
              ]
            }
          },
          "credits_charged": {
            "type": "integer",
            "description": "Credits spent by this batch, one per submitted item.",
            "example": 2
          },
          "credits_remaining": {
            "type": "integer",
            "example": 98
          }
        }
      },
      "Usage": {
        "type": "object",
        "required": ["plan", "credits_total", "credits_used", "credits_remaining", "period_end"],
        "properties": {
          "plan": {
            "type": "string",
            "description": "Plan attached to the key.",
            "example": "pack"
          },
          "credits_total": { "type": "integer", "example": 500 },
          "credits_used": { "type": "integer", "example": 12 },
          "credits_remaining": { "type": "integer", "example": 488 },
          "period_end": {
            "type": ["string", "null"],
            "format": "date-time",
            "description": "End of the billing period for a monthly plan. Null for a prepaid pack, which does not expire."
          }
        }
      },
      "Error": {
        "type": "object",
        "required": ["error"],
        "properties": {
          "error": {
            "type": "string",
            "description": "Stable machine-readable code.",
            "enum": [
              "invalid_input",
              "invalid_key",
              "insufficient_credits",
              "rate_limited",
              "classification_failed",
              "trial_already_issued",
              "forbidden",
              "trial_unavailable"
            ]
          },
          "message": {
            "type": "string",
            "description": "Human-readable explanation. Do not branch on this field."
          }
        }
      }
    }
  }
}
