{
  "openapi": "3.0.3",
  "info": {
    "title": "GymTea Food API",
    "version": "1.0.0",
    "description": "Anonymous, read-only food search and nutrition. No API key or login is required. Search first, then follow each food's apiUrl relative to https://gymtea.app. All responses use a published snapshot; generatedAt is its generation time, not an individual food's edit time. Successful data responses advertise five-minute caching. Approximately 30 requests per 10 seconds per IP are permitted; excess requests are blocked for 10 seconds. Shared service quotas can also affect availability. Check HTTP status before parsing JSON: rate-limit and hosting errors may be non-JSON."
  },
  "servers": [{ "url": "https://gymtea.app" }],
  "security": [],
  "externalDocs": {
    "description": "Examples, units, pagination, caching, and browser access",
    "url": "https://gymtea.app/api/"
  },
  "tags": [{ "name": "Foods", "description": "Public food catalog and nutrition per 100 grams." }],
  "paths": {
    "/api/v1/foods": {
      "get": {
        "operationId": "searchFoods",
        "tags": ["Foods"],
        "summary": "Search foods by name",
        "description": "Case- and accent-insensitive substring search. Every whitespace-separated query word must occur in the food name, in any order. Results are sorted by display name and then ID. Empty matches or offsets beyond the matching set return 200 with an empty foods array. Increase offset by limit to paginate; ordering can shift after publication of a new snapshot. Unknown or repeated query parameters are rejected. GET, HEAD, and OPTIONS are supported.",
        "parameters": [
          {
            "name": "q", "in": "query", "required": true,
            "description": "Required search text: 2–128 UTF-16 code units after trimming and at most eight whitespace-separated words. These server constraints apply in addition to the string type. URL-encode the value.",
            "schema": { "type": "string" },
            "example": "ice cream"
          },
          {
            "name": "limit", "in": "query",
            "description": "Maximum number of results in this page.",
            "schema": { "type": "integer", "minimum": 1, "maximum": 50, "default": 20 }
          },
          {
            "name": "offset", "in": "query",
            "description": "Number of matching foods to skip.",
            "schema": { "type": "integer", "minimum": 0, "maximum": 10000, "default": 0 }
          }
        ],
        "responses": {
          "200": {
            "description": "A page of matching food summaries, including zero matches.",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/FoodSearchResponse" } } }
          },
          "400": { "$ref": "#/components/responses/InvalidQuery" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "503": {
            "description": "The published search catalog is temporarily unavailable (catalog_unavailable).",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ApiError" } } }
          },
          "default": { "$ref": "#/components/responses/HostingError" }
        }
      }
    },
    "/api/v1/food-catalog.json": {
      "get": {
        "operationId": "getFoodCatalog",
        "tags": ["Foods"],
        "summary": "Download all public food summaries",
        "description": "Download once and search locally when appropriate; follow apiUrl for individual food details. GET and HEAD support anonymous cross-origin reads. Use simple browser requests without credentials or custom headers; existing static files return 405 for OPTIONS preflight.",
        "responses": {
          "200": {
            "description": "All public food summaries and snapshot metadata.",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/FoodCatalog" } } }
          },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "default": { "$ref": "#/components/responses/HostingError" }
        }
      }
    },
    "/api/v1/foods/{key}.json": {
      "get": {
        "operationId": "getFood",
        "tags": ["Foods"],
        "summary": "Get a food's nutrients and serving sizes",
        "description": "Use the apiUrl returned by search or the catalog. The opaque key is the lowercase hexadecimal encoding of the food ID's UTF-8 bytes, not the food's name-based website slug or its raw ID. URLs stay stable across renames, but a removed or hidden food can return 404 after a later publication. All nutrients are per 100 grams; scale known amounts by serving grams / 100 and preserve nulls. GET and HEAD support simple cross-origin requests; static detail files return 405 for OPTIONS preflight.",
        "parameters": [
          {
            "name": "key", "in": "path", "required": true,
            "description": "Opaque hexadecimal key from a returned apiUrl. Prefer following that URL instead of constructing a key.",
            "schema": { "type": "string", "pattern": "^(?:[0-9a-f]{2})+$" }
          }
        ],
        "responses": {
          "200": {
            "description": "Public food metadata, serving amounts, and nutrition per 100 grams.",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/FoodDetail" } } }
          },
          "404": {
            "description": "The food does not exist in the current public snapshot (not_found).",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ApiError" } } }
          },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "default": { "$ref": "#/components/responses/HostingError" }
        }
      }
    }
  },
  "components": {
    "responses": {
      "InvalidQuery": {
        "description": "Missing, invalid, unknown, or repeated search parameters (invalid_query).",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ApiError" } } }
      },
      "RateLimited": {
        "description": "Per-IP rate limit or shared service quota reached. Back off before retrying. Per-IP blocks last 10 seconds; quota exhaustion can require a longer wait. The body may be HTML or another non-JSON format. No Retry-After header is guaranteed."
      },
      "HostingError": {
        "description": "Other hosting or service error. Inspect the HTTP status and Content-Type before parsing the body; JSON is not guaranteed."
      }
    },
    "schemas": {
      "FoodSummary": {
        "type": "object",
        "required": ["id", "slug", "displayName", "imageUrl", "pageUrl", "apiUrl"],
        "properties": {
          "id": { "type": "string", "description": "Stable food ID, unchanged by renaming. Store this when retaining a food reference." },
          "slug": { "type": "string", "description": "Current name-based website slug; it can change after a rename." },
          "displayName": { "type": "string" },
          "imageUrl": { "type": "string", "nullable": true, "description": "Image URL, or null if no image is available." },
          "pageUrl": { "type": "string", "description": "Absolute URL of the human-readable food page." },
          "apiUrl": { "type": "string", "pattern": "^/api/v1/foods/(?:[0-9a-f]{2})+\\.json$", "description": "Detail URL path relative to https://gymtea.app. Follow it to retrieve nutrients." }
        }
      },
      "FoodCatalog": {
        "type": "object",
        "required": ["schemaVersion", "generatedAt", "foods"],
        "properties": {
          "schemaVersion": { "type": "integer", "enum": [1] },
          "generatedAt": { "type": "string", "format": "date-time", "description": "Snapshot generation time in UTC." },
          "foods": { "type": "array", "items": { "$ref": "#/components/schemas/FoodSummary" } }
        }
      },
      "FoodSearchResponse": {
        "allOf": [
          { "$ref": "#/components/schemas/FoodCatalog" },
          {
            "type": "object",
            "required": ["query", "limit", "offset", "total"],
            "properties": {
              "query": { "type": "string", "description": "Trimmed query used for this search." },
              "limit": { "type": "integer", "minimum": 1, "maximum": 50 },
              "offset": { "type": "integer", "minimum": 0, "maximum": 10000 },
              "total": { "type": "integer", "minimum": 0, "description": "Total matching foods before pagination." }
            }
          }
        ]
      },
      "FoodDetail": {
        "allOf": [
          { "$ref": "#/components/schemas/FoodSummary" },
          {
            "type": "object",
            "required": ["schemaVersion", "generatedAt", "description", "foodCategory", "servingAmounts", "per100g"],
            "properties": {
              "schemaVersion": { "type": "integer", "enum": [1] },
              "generatedAt": { "type": "string", "format": "date-time" },
              "description": { "type": "string", "nullable": true },
              "foodCategory": { "type": "string", "nullable": true },
              "servingAmounts": { "type": "array", "items": { "$ref": "#/components/schemas/ServingAmount" } },
              "per100g": { "$ref": "#/components/schemas/NutrientsPer100g" }
            }
          }
        ]
      },
      "ServingAmount": {
        "type": "object", "required": ["name", "grams"],
        "properties": {
          "name": { "type": "string" },
          "grams": { "type": "number", "minimum": 0, "description": "Positive weight in grams for one serving." }
        }
      },
      "NutrientsPer100g": {
        "type": "object",
        "required": ["caloriesKcal", "proteinG", "carbsG", "fatG", "fiberG", "micronutrients"],
        "description": "All amounts refer to 100 grams of food. Unknown amounts are null, never fabricated zeros. The micronutrients list is the website's selected vitamins and minerals, not every stored nutrient.",
        "properties": {
          "caloriesKcal": { "type": "number", "description": "Energy in kilocalories." },
          "proteinG": { "type": "number", "description": "Protein in grams." },
          "carbsG": { "type": "number", "description": "Carbohydrate in grams." },
          "fatG": { "type": "number", "description": "Fat in grams." },
          "fiberG": { "type": "number", "nullable": true, "description": "Fiber in grams, or null if unknown." },
          "micronutrients": { "type": "array", "items": { "$ref": "#/components/schemas/Micronutrient" } }
        }
      },
      "Micronutrient": {
        "type": "object", "required": ["name", "amount", "unit"],
        "additionalProperties": false,
        "properties": {
          "name": { "type": "string", "description": "Human-readable nutrient name, such as Vitamin C or Calcium." },
          "amount": { "type": "number", "nullable": true, "description": "Amount per 100 grams in the stated unit; null means unreported." },
          "unit": { "type": "string", "nullable": true, "description": "mg or mcg for a reported micronutrient; null when unreported." }
        }
      },
      "ApiError": {
        "type": "object", "required": ["error"],
        "properties": {
          "error": { "type": "string", "enum": ["invalid_query", "not_found", "method_not_allowed", "catalog_unavailable"] },
          "message": { "type": "string", "description": "Optional explanation, currently returned for invalid queries." }
        }
      }
    }
  }
}
