{
  "openapi": "3.1.0",
  "info": {
    "title": "SmartMarket API",
    "version": "1.0.0",
    "summary": "Grocery catalogue and baskets for Miami area supermarkets.",
    "description": "Search a catalogue of more than forty thousand grocery products across eleven Miami supermarkets including Publix, Walmart, Costco, Sam's Club, Aldi and BJ's Wholesale, compare the same item between shops, and build a basket a shopper can check out.\n\nThe search understands meaning rather than only keywords, so `something for a barbecue` works as well as a brand name. It is served from an index held in memory and answers in tens of milliseconds.\n\n**Authentication.** Every endpoint takes an API key in the `X-API-Key` header. Ask SmartZees for one.\n\n**Rate limits.** Reads are limited to 120 requests a minute per key, basket writes to 20. Over the limit you get a 429 with a JSON body saying so.\n\n**What this API cannot do.** It cannot place an order or take payment. A basket is built here and the shopper completes the purchase on the SmartMarket website. That boundary is deliberate.",
    "contact": {
      "name": "SmartZees",
      "url": "https://smartzees.com"
    }
  },
  "servers": [
    {
      "url": "https://marketz.mcp.smartzees.com",
      "description": "Production"
    }
  ],
  "tags": [
    {
      "name": "Catalogue",
      "description": "Finding things. Read only."
    },
    {
      "name": "Basket",
      "description": "Collecting things a shopper intends to buy. The shopper checks out on the website."
    }
  ],
  "security": [
    {
      "ApiKey": []
    }
  ],
  "paths": {
    "/v1/products/search": {
      "get": {
        "tags": ["Catalogue"],
        "summary": "Find products by describing them",
        "description": "Ranks the catalogue against a description of what somebody wants. Understands meaning, so `lactose free milk` and `something for a barbecue` both work. Results are best match first and each one names the shop selling it.",
        "operationId": "searchProducts",
        "parameters": [
          {
            "name": "q",
            "in": "query",
            "required": true,
            "description": "What the shopper wants, in their own words.",
            "schema": { "type": "string", "minLength": 1 },
            "example": "lactose free whole milk"
          },
          {
            "name": "limit",
            "in": "query",
            "required": false,
            "description": "How many to return. Clamped to 50 however large a number is sent.",
            "schema": { "type": "integer", "default": 10, "minimum": 1, "maximum": 50 }
          },
          {
            "name": "category",
            "in": "query",
            "required": false,
            "description": "Narrow to one category name from /v1/products/categories.",
            "schema": { "type": "string" },
            "example": "DAIRY"
          }
        ],
        "responses": {
          "200": {
            "description": "Matching products, best match first. An empty array means nothing matched, which is an answer rather than an error.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": { "$ref": "#/components/schemas/Product" }
                }
              }
            }
          },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "401": { "$ref": "#/components/responses/NoKey" },
          "429": { "$ref": "#/components/responses/TooFast" }
        }
      }
    },
    "/v1/products/categories": {
      "get": {
        "tags": ["Catalogue"],
        "summary": "List the categories that hold stock",
        "description": "Only categories with at least one product in stock, and the count with each. A list offering empty aisles invites a filter guaranteed to return nothing.",
        "operationId": "listCategories",
        "responses": {
          "200": {
            "description": "Categories, largest first.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": { "$ref": "#/components/schemas/Category" }
                }
              }
            }
          },
          "401": { "$ref": "#/components/responses/NoKey" },
          "429": { "$ref": "#/components/responses/TooFast" }
        }
      }
    },
    "/v1/stores": {
      "get": {
        "tags": ["Catalogue"],
        "summary": "List the shops in the catalogue",
        "operationId": "listStores",
        "responses": {
          "200": {
            "description": "The supermarkets whose stock this catalogue covers.",
            "content": {
              "application/json": {
                "schema": { "type": "array", "items": { "$ref": "#/components/schemas/Store" } }
              }
            }
          },
          "401": { "$ref": "#/components/responses/NoKey" },
          "429": { "$ref": "#/components/responses/TooFast" }
        }
      }
    },
    "/v1/cart": {
      "get": {
        "tags": ["Basket"],
        "summary": "Read a basket",
        "description": "The basket belonging to a session id, with the shop's own subtotal, tax and total. Use these figures rather than adding the lines up yourself: a basket that disagrees with the checkout page by a cent is worse than no basket.",
        "operationId": "getBasket",
        "parameters": [
          {
            "name": "session_id",
            "in": "query",
            "required": true,
            "description": "The basket's id. Mint one as 32 random hex characters and keep it; it is the only way back to this basket.",
            "schema": { "type": "string", "pattern": "^[0-9a-f]{32}$" },
            "example": "9f3a21c4b8e04d7a1f6c2e8b3d5a7091"
          }
        ],
        "responses": {
          "200": {
            "description": "The basket. An unknown session id returns an empty one rather than a 404.",
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/Basket" } }
            }
          },
          "401": { "$ref": "#/components/responses/NoKey" },
          "429": { "$ref": "#/components/responses/TooFast" }
        }
      }
    },
    "/v1/cart/items": {
      "post": {
        "tags": ["Basket"],
        "summary": "Put a product in a basket",
        "description": "Adds one product to the basket belonging to a session id, creating the basket if it is new. Returns the whole basket as it now stands.\n\nWhen the basket is ready, send the shopper to `https://marketz.smartzees.com/chat?cart={session_id}` and the site opens it for them. Nothing is ordered and no payment is taken until they check out there.",
        "operationId": "addToBasket",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/AddToBasket" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "The basket, including the line just added.",
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/Basket" } }
            }
          },
          "400": {
            "description": "That one product could not be added: out of stock, withdrawn, or no such id. The message names which. The rest of the basket is untouched.",
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/Error" } }
            }
          },
          "401": { "$ref": "#/components/responses/NoKey" },
          "429": { "$ref": "#/components/responses/TooFast" }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "ApiKey": {
        "type": "apiKey",
        "in": "header",
        "name": "X-API-Key",
        "description": "Issued by SmartZees. Keys begin sz_live_."
      }
    },
    "schemas": {
      "Product": {
        "type": "object",
        "properties": {
          "id": { "type": "integer", "description": "Use this to add the product to a basket.", "example": 10412 },
          "name": { "type": "string", "example": "Lactaid Whole Milk 96oz" },
          "price_per_unit": { "type": "number", "example": 7.69 },
          "store_name": { "type": "string", "example": "Costco Kendall" },
          "category": { "type": "string", "example": "DAIRY" },
          "stock": { "type": "number", "description": "Treat as a yes or no. The figure is a placeholder often enough that quoting it as fact would be wrong.", "example": 12 },
          "description": { "type": "string" },
          "image_url": { "type": "string", "format": "uri" },
          "similarity": { "type": "number", "description": "How closely this matched, 0 to 1. Internal ranking detail.", "example": 0.91 }
        }
      },
      "Category": {
        "type": "object",
        "properties": {
          "name": { "type": "string", "example": "DAIRY" },
          "products": { "type": "integer", "description": "How many in stock.", "example": 812 }
        }
      },
      "Store": {
        "type": "object",
        "properties": {
          "id": { "type": "integer" },
          "name": { "type": "string", "example": "Sabor Tropical Supermarket" }
        }
      },
      "AddToBasket": {
        "type": "object",
        "required": ["session_id", "item_id"],
        "properties": {
          "session_id": { "type": "string", "pattern": "^[0-9a-f]{32}$", "example": "9f3a21c4b8e04d7a1f6c2e8b3d5a7091" },
          "item_id": { "type": "integer", "description": "A product id from a search result.", "example": 10412 },
          "quantity": { "type": "integer", "default": 1, "minimum": 1, "example": 2 }
        }
      },
      "BasketLine": {
        "type": "object",
        "properties": {
          "item_id": { "type": "integer" },
          "name": { "type": "string" },
          "unit": { "type": "string" },
          "unit_price": { "type": "number" },
          "quantity": { "type": "integer" },
          "subtotal": { "type": "number" }
        }
      },
      "Basket": {
        "type": "object",
        "properties": {
          "session_id": { "type": "string" },
          "items": { "type": "array", "items": { "$ref": "#/components/schemas/BasketLine" } },
          "subtotal": { "type": "number", "description": "Goods only, before tax." },
          "tax": { "type": "number" },
          "total": { "type": "number", "description": "What the shopper pays." },
          "count": { "type": "integer", "description": "Total items, not lines." }
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "error": { "type": "string" },
          "detail": { "type": "string" }
        }
      }
    },
    "responses": {
      "BadRequest": {
        "description": "The request was not usable. The message says why.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "NoKey": {
        "description": "No API key, or one that is not current.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "TooFast": {
        "description": "Over the rate limit for this key. Reads 120 a minute, basket writes 20.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      }
    }
  }
}
