{
  "openapi": "3.1.0",
  "info": {
    "title": "OpenTabacchi API",
    "version": "1.0.0",
    "summary": "Tariffe di vendita al pubblico dei tabacchi lavorati e codici a barre, come dati aperti.",
    "description": "Servizio indipendente, non affiliato né approvato dall'ADM. Fonte dei prezzi: Agenzia delle Dogane e dei Monopoli (ADM).\n\nIl dataset è pensato per essere scaricato e conservato: bastano due chiamate al giorno (`/v1/version` e, se la versione è cambiata, un file). Ogni account ha **10 letture al giorno** (giorno UTC, contano anche le risposte 304) e **100 barcode all'ora** per gli invii; i limiti valgono per la somma di tutte le chiavi dell'account. Le pagine del sito e questo file non consumano nessun limite.\n\nDocumentazione completa: https://opentabacchi.org/docs",
    "termsOfService": "https://opentabacchi.org/terms",
    "contact": {
      "name": "OpenTabacchi",
      "url": "https://opentabacchi.org/contact"
    }
  },
  "servers": [
    {
      "url": "https://opentabacchi.org",
      "description": "Produzione"
    }
  ],
  "tags": [
    {
      "name": "Dataset",
      "description": "Manifest e file della release corrente. Ogni chiamata costa una lettura."
    },
    {
      "name": "Barcode",
      "description": "Segnalazione dei codici a barre e stato delle proprie segnalazioni."
    },
    {
      "name": "Account",
      "description": "Chiave in uso e consumi."
    }
  ],
  "security": [
    {
      "bearerAuth": []
    },
    {
      "apiKeyHeader": []
    }
  ],
  "paths": {
    "/v1/me": {
      "get": {
        "tags": [
          "Account"
        ],
        "operationId": "getMe",
        "summary": "Account e consumi",
        "description": "Controlla la chiave e mostra i residui. Costa una lettura come tutte le altre.",
        "responses": {
          "200": {
            "description": "L'account della chiave e i due contatori.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Me"
                }
              }
            },
            "headers": {
              "X-RateLimit-Limit": {
                "description": "Limite del contatore di competenza (letture al giorno o barcode all'ora).",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Remaining": {
                "description": "Quanto resta nel periodo corrente.",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Reset": {
                "description": "Fine del periodo corrente, in secondi Unix (UTC).",
                "schema": {
                  "type": "integer"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "429": {
            "$ref": "#/components/responses/RateLimitedRead"
          }
        }
      }
    },
    "/v1/version": {
      "get": {
        "tags": [
          "Dataset"
        ],
        "operationId": "getVersion",
        "summary": "Manifest della release corrente",
        "description": "Da chiamare una volta al giorno. Se `version` coincide con quella già importata non c'è altro da scaricare.",
        "responses": {
          "200": {
            "description": "La release corrente.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Version"
                }
              }
            },
            "headers": {
              "X-RateLimit-Limit": {
                "description": "Limite del contatore di competenza (letture al giorno o barcode all'ora).",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Remaining": {
                "description": "Quanto resta nel periodo corrente.",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Reset": {
                "description": "Fine del periodo corrente, in secondi Unix (UTC).",
                "schema": {
                  "type": "integer"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "404": {
            "description": "Nessuna release ancora pubblicata.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "example": {
                  "error": "no_release",
                  "message": "Nessuna release ancora pubblicata."
                }
              }
            }
          },
          "429": {
            "$ref": "#/components/responses/RateLimitedRead"
          }
        }
      }
    },
    "/v1/dataset.json.gz": {
      "get": {
        "tags": [
          "Dataset"
        ],
        "operationId": "downloadJson",
        "summary": "Dataset in JSON",
        "description": "Dataset completo in JSON (compresso con gzip). Il contenuto è descritto dallo schema `DatasetJson`. Supporta `If-None-Match` con l'ETag (che coincide con `sha256` del manifest); una risposta 304 conta comunque come lettura.",
        "parameters": [
          {
            "name": "If-None-Match",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string"
            },
            "description": "ETag ricevuto in precedenza."
          }
        ],
        "responses": {
          "200": {
            "description": "File compresso con gzip.",
            "headers": {
              "X-RateLimit-Limit": {
                "description": "Limite del contatore di competenza (letture al giorno o barcode all'ora).",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Remaining": {
                "description": "Quanto resta nel periodo corrente.",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Reset": {
                "description": "Fine del periodo corrente, in secondi Unix (UTC).",
                "schema": {
                  "type": "integer"
                }
              },
              "ETag": {
                "description": "Impronta SHA-256 del file compresso, tra virgolette.",
                "schema": {
                  "type": "string"
                }
              }
            },
            "content": {
              "application/gzip": {
                "schema": {
                  "type": "string",
                  "format": "binary"
                }
              }
            }
          },
          "304": {
            "description": "Il file non è cambiato.",
            "headers": {
              "X-RateLimit-Limit": {
                "description": "Limite del contatore di competenza (letture al giorno o barcode all'ora).",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Remaining": {
                "description": "Quanto resta nel periodo corrente.",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Reset": {
                "description": "Fine del periodo corrente, in secondi Unix (UTC).",
                "schema": {
                  "type": "integer"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "404": {
            "description": "Nessuna release ancora pubblicata.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "$ref": "#/components/responses/RateLimitedRead"
          }
        }
      }
    },
    "/v1/barcodes": {
      "post": {
        "tags": [
          "Barcode"
        ],
        "operationId": "submitBarcodes",
        "summary": "Invia abbinamenti barcode e codice AAMS",
        "description": "Fino a 500 elementi per chiamata. Si contano i barcode, non le chiamate: un batch che supera il residuo orario è rifiutato per intero con 429. Non consuma le letture giornaliere. Con 3 contributori distinti d'accordo l'abbinamento è approvato, altrimenti resta «da confermare» e lo vede solo chi l'ha inviato.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/SubmitRequest"
              },
              "example": {
                "items": [
                  {
                    "barcode": "4006381333931",
                    "admCode": 12345,
                    "kind": "pack"
                  },
                  {
                    "barcode": "4006381333948",
                    "admCode": 12345,
                    "kind": "carton",
                    "units": 10
                  }
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Esito per ogni elemento, nell'ordine inviato.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SubmitResponse"
                },
                "example": {
                  "accepted": 1,
                  "unchanged": 0,
                  "rejected": 1,
                  "results": [
                    {
                      "index": 0,
                      "barcode": "4006381333931",
                      "outcome": "accepted",
                      "reason": null,
                      "status": "to_confirm"
                    },
                    {
                      "index": 1,
                      "barcode": "4006381333948",
                      "outcome": "rejected",
                      "reason": "invalid_checksum",
                      "status": null
                    }
                  ]
                }
              }
            },
            "headers": {
              "X-RateLimit-Limit": {
                "description": "Limite del contatore di competenza (letture al giorno o barcode all'ora).",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Remaining": {
                "description": "Quanto resta nel periodo corrente.",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Reset": {
                "description": "Fine del periodo corrente, in secondi Unix (UTC).",
                "schema": {
                  "type": "integer"
                }
              }
            }
          },
          "400": {
            "description": "Batch vuoto o oltre il massimo.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                },
                "example": {
                  "error": "batch_too_large",
                  "max": 500
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "429": {
            "description": "Il batch supera il residuo orario: non è stato elaborato.",
            "headers": {
              "X-RateLimit-Limit": {
                "description": "Limite del contatore di competenza (letture al giorno o barcode all'ora).",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Remaining": {
                "description": "Quanto resta nel periodo corrente.",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Reset": {
                "description": "Fine del periodo corrente, in secondi Unix (UTC).",
                "schema": {
                  "type": "integer"
                }
              },
              "Retry-After": {
                "description": "Secondi da attendere.",
                "schema": {
                  "type": "integer"
                }
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/RateLimitedSubmit"
                }
              }
            }
          }
        }
      }
    },
    "/v1/my-submissions": {
      "get": {
        "tags": [
          "Barcode"
        ],
        "operationId": "getMySubmissions",
        "summary": "Le tue segnalazioni",
        "description": "Dalla più recente, con lo stato attuale del codice a barre. Costa una lettura.",
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 1000,
              "default": 500
            }
          },
          {
            "name": "offset",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 0,
              "default": 0
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Le segnalazioni dell'account.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Submissions"
                }
              }
            },
            "headers": {
              "X-RateLimit-Limit": {
                "description": "Limite del contatore di competenza (letture al giorno o barcode all'ora).",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Remaining": {
                "description": "Quanto resta nel periodo corrente.",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Reset": {
                "description": "Fine del periodo corrente, in secondi Unix (UTC).",
                "schema": {
                  "type": "integer"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "429": {
            "$ref": "#/components/responses/RateLimitedRead"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "otb_…",
        "description": "Chiave API creata da «Il tuo account» dopo l'accesso. Si mostra una sola volta."
      },
      "apiKeyHeader": {
        "type": "apiKey",
        "in": "header",
        "name": "X-Api-Key",
        "description": "In alternativa a `Authorization: Bearer`."
      }
    },
    "responses": {
      "Unauthorized": {
        "description": "Chiave mancante, non valida o revocata, oppure account bloccato.",
        "headers": {
          "WWW-Authenticate": {
            "description": "Sempre `Bearer`.",
            "schema": {
              "type": "string"
            }
          }
        },
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            },
            "example": {
              "error": "invalid_api_key",
              "message": "Chiave API mancante o non valida."
            }
          }
        }
      },
      "RateLimitedRead": {
        "description": "Letture giornaliere esaurite (si riparte a mezzanotte UTC).",
        "headers": {
          "X-RateLimit-Limit": {
            "description": "Limite del contatore di competenza (letture al giorno o barcode all'ora).",
            "schema": {
              "type": "integer"
            }
          },
          "X-RateLimit-Remaining": {
            "description": "Quanto resta nel periodo corrente.",
            "schema": {
              "type": "integer"
            }
          },
          "X-RateLimit-Reset": {
            "description": "Fine del periodo corrente, in secondi Unix (UTC).",
            "schema": {
              "type": "integer"
            }
          },
          "Retry-After": {
            "description": "Secondi da attendere.",
            "schema": {
              "type": "integer"
            }
          }
        },
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/RateLimitedRead"
            },
            "example": {
              "error": "rate_limited",
              "message": "Limite giornaliero di letture raggiunto: conserva i dati in cache.",
              "retryAfterSeconds": 3600
            }
          }
        }
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "required": [
          "error"
        ],
        "properties": {
          "error": {
            "type": "string"
          },
          "message": {
            "type": "string"
          }
        }
      },
      "RateLimitedRead": {
        "type": "object",
        "required": [
          "error",
          "retryAfterSeconds"
        ],
        "properties": {
          "error": {
            "const": "rate_limited"
          },
          "message": {
            "type": "string"
          },
          "retryAfterSeconds": {
            "type": "integer"
          }
        }
      },
      "RateLimitedSubmit": {
        "type": "object",
        "required": [
          "error",
          "limit",
          "remaining",
          "retryAfterSeconds"
        ],
        "properties": {
          "error": {
            "const": "rate_limited"
          },
          "message": {
            "type": "string"
          },
          "limit": {
            "type": "integer"
          },
          "remaining": {
            "type": "integer"
          },
          "retryAfterSeconds": {
            "type": "integer"
          }
        }
      },
      "Quota": {
        "type": "object",
        "properties": {
          "period": {
            "enum": [
              "day",
              "hour"
            ]
          },
          "limit": {
            "type": "integer"
          },
          "used": {
            "type": "integer"
          },
          "remaining": {
            "type": "integer"
          },
          "resetsAt": {
            "type": "string",
            "format": "date-time"
          }
        }
      },
      "Me": {
        "type": "object",
        "properties": {
          "email": {
            "type": "string",
            "format": "email"
          },
          "type": {
            "enum": [
              "User",
              "Supplier",
              "Integration"
            ]
          },
          "limits": {
            "type": "object",
            "properties": {
              "read": {
                "$ref": "#/components/schemas/Quota"
              },
              "submit": {
                "$ref": "#/components/schemas/Quota"
              }
            }
          }
        }
      },
      "FileInfo": {
        "type": "object",
        "properties": {
          "url": {
            "type": "string",
            "format": "uri"
          },
          "sha256": {
            "type": "string"
          },
          "size": {
            "type": "integer",
            "description": "Byte del file compresso."
          }
        }
      },
      "Version": {
        "type": "object",
        "properties": {
          "version": {
            "type": "string",
            "description": "AAAAMMGG.n: data della tariffa in vigore e progressivo.",
            "examples": [
              "20261014.2"
            ]
          },
          "tariffDate": {
            "type": "string",
            "format": "date"
          },
          "generatedAt": {
            "type": "string",
            "format": "date-time"
          },
          "source": {
            "const": "Agenzia delle Dogane e dei Monopoli (ADM)"
          },
          "schemaVersion": {
            "type": "integer",
            "examples": [
              2
            ]
          },
          "counts": {
            "type": "object",
            "properties": {
              "products": {
                "type": "integer"
              },
              "barcodes": {
                "type": "integer"
              }
            }
          },
          "files": {
            "type": "object",
            "properties": {
              "json": {
                "$ref": "#/components/schemas/FileInfo"
              }
            }
          }
        }
      },
      "SubmitItem": {
        "type": "object",
        "required": [
          "barcode",
          "admCode"
        ],
        "properties": {
          "barcode": {
            "type": "string",
            "description": "8, 12, 13 o 14 cifre con cifra di controllo valida.",
            "examples": [
              "4006381333931"
            ]
          },
          "admCode": {
            "type": "integer",
            "description": "Codice AAMS del prodotto in tariffa."
          },
          "kind": {
            "enum": [
              "pack",
              "carton"
            ],
            "default": "pack"
          },
          "units": {
            "type": "integer",
            "minimum": 1,
            "maximum": 1000,
            "description": "Pezzi per codice: 1 per `pack`, da 2 a 1000 per `carton`."
          },
          "contributorRef": {
            "type": "string",
            "pattern": "^[A-Za-z0-9._:\\-]{1,64}$",
            "description": "Solo per gli account di tipo integrazione: identificativo opaco del punto vendita."
          }
        }
      },
      "SubmitRequest": {
        "type": "object",
        "required": [
          "items"
        ],
        "properties": {
          "items": {
            "type": "array",
            "minItems": 1,
            "maxItems": 500,
            "items": {
              "$ref": "#/components/schemas/SubmitItem"
            }
          }
        }
      },
      "SubmitResult": {
        "type": "object",
        "properties": {
          "index": {
            "type": "integer"
          },
          "barcode": {
            "type": [
              "string",
              "null"
            ]
          },
          "outcome": {
            "enum": [
              "accepted",
              "unchanged",
              "rejected"
            ]
          },
          "reason": {
            "type": [
              "string",
              "null"
            ],
            "enum": [
              "invalid_barcode",
              "invalid_checksum",
              "unsupported_barcode",
              "unknown_adm_code",
              "invalid_kind",
              "invalid_units",
              "barcode_revoked",
              "contributor_ref_not_allowed",
              "invalid_contributor_ref",
              "contributor_blocked",
              null
            ],
            "description": "Motivo del rifiuto; nullo se l'elemento non è rifiutato."
          },
          "status": {
            "type": [
              "string",
              "null"
            ],
            "enum": [
              "to_confirm",
              "approved",
              "conflict",
              "revoked",
              null
            ],
            "description": "Stato del codice a barre dopo l'invio."
          }
        }
      },
      "SubmitResponse": {
        "type": "object",
        "properties": {
          "accepted": {
            "type": "integer"
          },
          "unchanged": {
            "type": "integer"
          },
          "rejected": {
            "type": "integer"
          },
          "results": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/SubmitResult"
            }
          }
        }
      },
      "Submission": {
        "type": "object",
        "properties": {
          "barcode": {
            "type": "string"
          },
          "admCode": {
            "type": "integer"
          },
          "kind": {
            "enum": [
              "pack",
              "carton"
            ]
          },
          "units": {
            "type": "integer"
          },
          "contributorRef": {
            "type": [
              "string",
              "null"
            ]
          },
          "submittedAt": {
            "type": "string",
            "format": "date-time"
          },
          "status": {
            "enum": [
              "to_confirm",
              "approved",
              "conflict",
              "revoked",
              "outvoted"
            ],
            "description": "`outvoted`: il codice è approvato ma per un abbinamento diverso dal tuo."
          }
        }
      },
      "Submissions": {
        "type": "object",
        "properties": {
          "items": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Submission"
            }
          }
        }
      },
      "DatasetProduct": {
        "type": "object",
        "properties": {
          "admCode": {
            "type": "integer"
          },
          "name": {
            "type": "string"
          },
          "category": {
            "type": "string"
          },
          "brand": {
            "type": "string",
            "description": "Marchio ricavato dall'inizio della denominazione; non è un campo della tariffa ufficiale."
          },
          "packaging": {
            "type": "string"
          },
          "packSize": {
            "type": [
              "number",
              "null"
            ]
          },
          "packUnit": {
            "enum": [
              "pieces",
              "grams",
              null
            ]
          },
          "pricePerKg": {
            "type": "number"
          },
          "priceCents": {
            "type": "integer"
          },
          "nextPriceCents": {
            "type": [
              "integer",
              "null"
            ]
          },
          "nextPricePerKg": {
            "type": [
              "number",
              "null"
            ]
          },
          "nextEffectiveFrom": {
            "type": [
              "string",
              "null"
            ],
            "format": "date"
          },
          "status": {
            "enum": [
              "active",
              "delisted"
            ]
          }
        }
      },
      "DatasetBarcode": {
        "type": "object",
        "properties": {
          "barcode": {
            "type": "string"
          },
          "admCode": {
            "type": "integer"
          },
          "kind": {
            "enum": [
              "pack",
              "carton"
            ]
          },
          "unitsPerBarcode": {
            "type": "integer"
          }
        }
      },
      "DatasetJson": {
        "type": "object",
        "description": "Contenuto del file `dataset.json.gz` dopo la decompressione.",
        "properties": {
          "meta": {
            "type": "object",
            "properties": {
              "version": {
                "type": "string"
              },
              "tariffDate": {
                "type": "string",
                "format": "date"
              },
              "generatedAt": {
                "type": "string",
                "format": "date-time"
              },
              "source": {
                "type": "string"
              },
              "schemaVersion": {
                "type": "integer"
              }
            }
          },
          "products": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/DatasetProduct"
            }
          },
          "barcodes": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/DatasetBarcode"
            }
          }
        }
      }
    }
  }
}
