{
  "openapi": "3.1.0",
  "info": {
    "title": "XQustoms Tariff API v2",
    "version": "2.1.0",
    "description": "Unified envelope over the v1 tariff engine. /analyze bills one quota unit.\n\nOperations: Every response carries x-request-id (send your own x-request-id, 8–64 chars [A-Za-z0-9_-], to correlate) and x-api-version. Gateway failures return JSON {error, message, request_id}: 502 UPSTREAM_UNAVAILABLE and 504 UPSTREAM_TIMEOUT are safe to retry with backoff; 413 PAYLOAD_TOO_LARGE above 2 MB. Every JSON body also contains request_id.\n\n/analyze 2.1: duties = {duty_rate, duty_expression, rate_source, vat, tr_origin, preferences, measures, us_duty_layers}; documents = {required, flags}; landed_cost = the v1 calculation object; compliance = {supply_chain, origin_notes, de_minimis}. classification.chain was removed (it was always null); use /api/v1/hs-lookup for the family tree context.\n\nScopes: keys carry scopes tariff:classify, tariff:lookup, tariff:calculate, tariff:feedback and tariff:ask (reserved; no public route requires it yet). Each operation names its scope in x-required-scope. Legacy permissions map as tariff:read = classify + lookup + feedback and tariff:calculate = calculate; keys issued before the scope rollout keep full access. A key without the scope gets 403 INSUFFICIENT_SCOPE {error, required_scope}; a key that may not send feedback gets 403 FEEDBACK_NOT_ALLOWED.\n\nRate limit: only keys with a configured per-minute limit are limited. Above it the answer is 429 RATE_LIMITED {error, retry_after_seconds} with a Retry-After header (seconds); wait and retry. Denied and successful logged calls count, 429 answers do not.\n\nTest keys: keys with the prefix xqt_test_ run the same engine on the same data, are never billed and consume no quota (meta.quota_remaining is null). Their answers carry meta.test_mode = true (where a meta object exists) and the header x-xqustoms-test: true.\n\nAttribution: optional request headers X-Integration and X-End-Customer-Ref (max 128 characters, control characters removed) are stored with the usage entry for reporting. They never change the result. Do not send personal data."
  },
  "servers": [
    {
      "url": "https://xqustoms.com/api/v2/tariff"
    }
  ],
  "components": {
    "securitySchemes": {
      "ApiKeyAuth": {
        "type": "apiKey",
        "in": "header",
        "name": "x-api-key"
      }
    },
    "parameters": {
      "EndCustomerRef": {
        "name": "X-End-Customer-Ref",
        "in": "header",
        "required": false,
        "schema": {
          "type": "string",
          "maxLength": 128
        },
        "description": "Optional. Your own reference for the end customer this call is made for; stored with the usage entry. No personal data."
      },
      "Integration": {
        "name": "X-Integration",
        "in": "header",
        "required": false,
        "schema": {
          "type": "string",
          "maxLength": 128
        },
        "description": "Optional. Name of the calling integration or channel; stored with the usage entry and used for usage reports."
      }
    },
    "responses": {
      "RateLimited": {
        "description": "429 RATE_LIMITED – per-minute limit of this key reached. Retry after the number of seconds in Retry-After.",
        "headers": {
          "Retry-After": {
            "schema": {
              "type": "integer",
              "minimum": 1
            },
            "description": "Seconds to wait before retrying."
          }
        },
        "content": {
          "application/json": {
            "schema": {
              "type": "object",
              "required": [
                "error",
                "retry_after_seconds"
              ],
              "properties": {
                "error": {
                  "type": "string",
                  "enum": [
                    "RATE_LIMITED"
                  ]
                },
                "retry_after_seconds": {
                  "type": "integer",
                  "minimum": 1
                }
              }
            }
          }
        }
      }
    }
  },
  "paths": {
    "/analyze": {
      "post": {
        "summary": "Unified analysis (classification, duties, documents, optional landed cost) — 1 quota unit",
        "security": [
          {
            "ApiKeyAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "destination_country"
                ],
                "properties": {
                  "hs_code": {
                    "type": "string",
                    "pattern": "^\\d{4,12}$"
                  },
                  "description": {
                    "type": "string",
                    "description": "DE/EN/TR product text; national declarable candidates are resolved from destination_country. No auto-classification; confirm hs_code in a second call.",
                    "minLength": 2,
                    "maxLength": 200
                  },
                  "destination_country": {
                    "type": "string"
                  },
                  "origin_country": {
                    "type": "string"
                  },
                  "departure_country": {
                    "type": "string"
                  },
                  "customs_value": {
                    "type": "number",
                    "description": "If set, landed_cost is computed"
                  },
                  "currency": {
                    "type": "string"
                  },
                  "freight": {
                    "type": "number"
                  },
                  "insurance": {
                    "type": "number"
                  },
                  "net_mass_kg": {
                    "type": "number"
                  },
                  "declaration_date": {
                    "type": "string",
                    "format": "date"
                  },
                  "direction": {
                    "type": "string",
                    "enum": [
                      "import",
                      "export"
                    ],
                    "default": "import"
                  },
                  "include": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "enum": [
                        "ancestry",
                        "customs_descriptions",
                        "attributes"
                      ]
                    },
                    "description": "Extra blocks for a confirmed hs_code; default all three. Candidates always carry match_percent (relative, not a legal probability), recommendation and reason."
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "status OK | INCOMPLETE | CLARIFICATION_REQUIRED | NO_CANDIDATES; missing values are null and listed in missing[]"
          },
          "401": {
            "description": "API_KEY_REQUIRED"
          },
          "403": {
            "description": "API_KEY_INVALID Restricted keys only: INSUFFICIENT_SCOPE {error, required_scope} or FEEDBACK_NOT_ALLOWED."
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        },
        "parameters": [
          {
            "$ref": "#/components/parameters/EndCustomerRef"
          },
          {
            "$ref": "#/components/parameters/Integration"
          }
        ],
        "x-required-scope": "tariff:lookup (+ tariff:calculate when customs_value is sent, tariff:classify when only description is sent)",
        "description": "Required scope: tariff:lookup (+ tariff:calculate when customs_value is sent, tariff:classify when only description is sent)."
      }
    },
    "/search": {
      "post": {
        "summary": "v2 alias of /api/v1/search (same body, same billing, adds api_version)",
        "security": [
          {
            "ApiKeyAuth": []
          }
        ],
        "responses": {
          "200": {
            "description": "See /api/v1/search"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "403": {
            "description": "403 API_KEY_INVALID / QUOTA_ERROR (existing). Restricted keys only: INSUFFICIENT_SCOPE {error, required_scope} or FEEDBACK_NOT_ALLOWED."
          }
        },
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "query": {
                    "type": "string",
                    "description": "",
                    "minLength": 2,
                    "maxLength": 200
                  },
                  "jurisdiction": {
                    "type": "string",
                    "description": "National tariff jurisdiction. EU country is selected separately.",
                    "enum": [
                      "EU",
                      "UK",
                      "US",
                      "TR",
                      "CA"
                    ]
                  },
                  "limit": {
                    "type": "integer",
                    "description": "",
                    "minimum": 1,
                    "maximum": 50,
                    "default": 20
                  },
                  "min_digits": {
                    "type": "integer",
                    "description": "Legacy search only (when jurisdiction is omitted).",
                    "minimum": 2,
                    "maximum": 12,
                    "default": 2
                  },
                  "expand": {
                    "type": "boolean",
                    "description": "Use cached multilingual query expansion; false skips expansion.",
                    "default": true
                  },
                  "destination_country": {
                    "type": "string",
                    "minLength": 2,
                    "maxLength": 2,
                    "description": "Destination ISO country: DE resolves to EU TARIC, US to HTSUS. Unsupported countries return 422; conflicting jurisdiction returns 400."
                  },
                  "declarable_only": {
                    "type": "boolean",
                    "default": false,
                    "description": "Return only national declarable candidates; confirmation is still required."
                  },
                  "query_language": {
                    "type": "string",
                    "enum": [
                      "tr",
                      "en",
                      "de"
                    ],
                    "description": "Optional. Language of text_in_query_language and ancestry texts. Omitted: detected from the query (single words may be misdetected). Does not change codes or ranking country."
                  }
                },
                "required": [
                  "query"
                ]
              },
              "example": {
                "destination_country": "DE",
                "query": "pamuklu tişört",
                "declarable_only": true,
                "limit": 5
              }
            }
          }
        },
        "parameters": [
          {
            "$ref": "#/components/parameters/EndCustomerRef"
          },
          {
            "$ref": "#/components/parameters/Integration"
          }
        ],
        "x-required-scope": "tariff:classify",
        "description": "Required scope: tariff:classify."
      }
    },
    "/calculate": {
      "post": {
        "summary": "v2 alias of /api/v1/calculate (same body, same billing, adds api_version)",
        "security": [
          {
            "ApiKeyAuth": []
          }
        ],
        "responses": {
          "200": {
            "description": "See /api/v1/calculate"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "403": {
            "description": "403 API_KEY_INVALID / QUOTA_ERROR (existing). Restricted keys only: INSUFFICIENT_SCOPE {error, required_scope} or FEEDBACK_NOT_ALLOWED."
          }
        },
        "parameters": [
          {
            "$ref": "#/components/parameters/EndCustomerRef"
          },
          {
            "$ref": "#/components/parameters/Integration"
          }
        ],
        "x-required-scope": "tariff:calculate",
        "description": "Required scope: tariff:calculate."
      }
    },
    "/validate": {
      "post": {
        "summary": "v2 alias of /api/v1/validate-code (same body, same billing, adds api_version)",
        "security": [
          {
            "ApiKeyAuth": []
          }
        ],
        "responses": {
          "200": {
            "description": "See /api/v1/validate-code"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "403": {
            "description": "403 API_KEY_INVALID / QUOTA_ERROR (existing). Restricted keys only: INSUFFICIENT_SCOPE {error, required_scope} or FEEDBACK_NOT_ALLOWED."
          }
        },
        "parameters": [
          {
            "$ref": "#/components/parameters/EndCustomerRef"
          },
          {
            "$ref": "#/components/parameters/Integration"
          }
        ],
        "x-required-scope": "tariff:classify",
        "description": "Required scope: tariff:classify."
      }
    },
    "/batch": {
      "post": {
        "summary": "v2 alias of /api/v1/bulk-calculate (same body, same billing, adds api_version)",
        "security": [
          {
            "ApiKeyAuth": []
          }
        ],
        "responses": {
          "200": {
            "description": "See /api/v1/bulk-calculate"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "403": {
            "description": "403 API_KEY_INVALID / QUOTA_ERROR (existing). Restricted keys only: INSUFFICIENT_SCOPE {error, required_scope} or FEEDBACK_NOT_ALLOWED."
          }
        },
        "parameters": [
          {
            "$ref": "#/components/parameters/EndCustomerRef"
          },
          {
            "$ref": "#/components/parameters/Integration"
          }
        ],
        "x-required-scope": "tariff:calculate",
        "description": "Required scope: tariff:calculate."
      }
    },
    "/lookup": {
      "post": {
        "summary": "v2 alias of /api/v1/lookup (same body, same billing, adds api_version)",
        "security": [
          {
            "ApiKeyAuth": []
          }
        ],
        "responses": {
          "200": {
            "description": "See /api/v1/lookup"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "403": {
            "description": "403 API_KEY_INVALID / QUOTA_ERROR (existing). Restricted keys only: INSUFFICIENT_SCOPE {error, required_scope} or FEEDBACK_NOT_ALLOWED."
          }
        },
        "parameters": [
          {
            "$ref": "#/components/parameters/EndCustomerRef"
          },
          {
            "$ref": "#/components/parameters/Integration"
          }
        ],
        "x-required-scope": "tariff:lookup",
        "description": "Required scope: tariff:lookup."
      }
    },
    "/sources": {
      "get": {
        "summary": "Official data sources (free)",
        "responses": {
          "200": {
            "description": "Source list"
          }
        }
      }
    },
    "/health": {
      "get": {
        "operationId": "health",
        "summary": "Service health (no API key, no quota)",
        "security": [],
        "responses": {
          "200": {
            "description": "ok: engine reachable"
          },
          "503": {
            "description": "degraded: engine unreachable"
          }
        }
      }
    }
  }
}
