{
  "openapi": "3.1.0",
  "info": {
    "title": "Caeloryn API",
    "version": "1.0.0",
    "summary": "Read-only access to a Caeloryn owner's shops: sales, best sellers, credit book, appointments and devices.",
    "description": "Every call reads with the permissions of the person who signed in. Dates are YYYY-MM-DD; a range can be up to 400 days. Amounts are decimal numbers in the shop's currency (KWD has 3 decimals). Limit: 120 requests a minute per person. Try it without an account with the sandbox token `caeloryn_sandbox`.",
    "contact": { "name": "Caeloryn support", "email": "support@caeloryn.net", "url": "https://mcp.caeloryn.net" },
    "termsOfService": "https://mcp.caeloryn.net/#api-rules"
  },
  "servers": [{ "url": "https://mcp.caeloryn.net" }],
  "security": [{ "oauth": ["read"] }, { "sandbox": [] }],
  "components": {
    "securitySchemes": {
      "oauth": {
        "type": "oauth2",
        "description": "OAuth 2.1 with PKCE (S256). Register your app with POST /register (RFC 7591). Access tokens last up to 50 minutes; refresh with grant_type=refresh_token.",
        "flows": { "authorizationCode": { "authorizationUrl": "https://mcp.caeloryn.net/authorize", "tokenUrl": "https://mcp.caeloryn.net/token", "refreshUrl": "https://mcp.caeloryn.net/token", "scopes": { "read": "Read the shops the person can see" } } }
      },
      "sandbox": { "type": "http", "scheme": "bearer", "description": "Use the bearer token caeloryn_sandbox to read two demo shops." }
    },
    "parameters": {
      "shopId": { "name": "shop_id", "in": "path", "required": true, "schema": { "type": "string", "format": "uuid" }, "description": "A shop_id from GET /v1/shops" },
      "from": { "name": "from", "in": "query", "schema": { "type": "string", "format": "date" }, "description": "First day. Defaults to `to`." },
      "to": { "name": "to", "in": "query", "schema": { "type": "string", "format": "date" }, "description": "Last day. Defaults to today." }
    },
    "schemas": {
      "Error": { "type": "object", "properties": { "error": { "type": "object", "properties": { "status": { "type": "integer" }, "message": { "type": "string" } } } } },
      "Shop": { "type": "object", "properties": {
        "shop_id": { "type": "string", "format": "uuid" }, "name": { "type": "string" }, "role": { "type": "string", "enum": ["owner", "team"] },
        "product": { "type": "string", "examples": ["Grocery POS", "Pharmacy POS", "Restaurant POS", "Booking"] },
        "currency": { "type": ["string", "null"], "examples": ["KWD"] }, "plan": { "type": "string" }, "status": { "type": "string" }, "expires_at": { "type": ["string", "null"], "format": "date" } } },
      "Ranked": { "type": "object", "properties": { "name_en": { "type": ["string", "null"] }, "name_ar": { "type": ["string", "null"] }, "qty": { "type": ["integer", "null"] }, "revenue": { "type": "number" } } },
      "Sales": { "type": "object", "properties": {
        "shop": { "type": "string" }, "currency": { "type": "string" }, "from": { "type": "string", "format": "date" }, "to": { "type": "string", "format": "date" }, "days_with_sales": { "type": "integer" },
        "totals": { "type": "object", "properties": {
          "net_sales": { "type": "number" }, "profit": { "type": "number" }, "cash": { "type": "number" }, "card": { "type": "number" }, "on_credit": { "type": "number" },
          "app_orders": { "type": "number" }, "service_charge": { "type": "number" }, "discounts": { "type": "number" }, "refunds": { "type": "number" },
          "sales_count": { "type": "integer" }, "refund_count": { "type": "integer" }, "void_count": { "type": "integer" }, "average_sale": { "type": ["number", "null"] } } },
        "top_products": { "type": "array", "items": { "$ref": "#/components/schemas/Ranked" } },
        "top_categories": { "type": "array", "items": { "$ref": "#/components/schemas/Ranked" } },
        "till_closes": { "type": "array", "items": { "type": "object", "properties": { "date": { "type": "string", "format": "date" }, "closed_at": { "type": "string", "format": "date-time" }, "expected": { "type": "number" }, "counted": { "type": "number" }, "difference": { "type": "number" }, "reason": { "type": ["string", "null"] } } } },
        "days": { "type": "array", "items": { "type": "object", "description": "One line per day: date plus the same fields as totals (no average_sale)." } },
        "note": { "type": "string" } } }
    }
  },
  "paths": {
    "/v1/me": { "get": { "summary": "The signed-in person", "responses": { "200": { "description": "OK", "content": { "application/json": { "schema": { "type": "object", "properties": { "data": { "type": "object", "properties": { "user_id": { "type": "string" }, "email": { "type": "string" } } } } } } } } } } },
    "/v1/shops": { "get": { "summary": "Shops the person can see", "responses": { "200": { "description": "OK", "content": { "application/json": { "schema": { "type": "object", "properties": { "data": { "type": "object", "properties": { "shops": { "type": "array", "items": { "$ref": "#/components/schemas/Shop" } } } } } } } } } } } },
    "/v1/shops/{shop_id}/sales": { "get": { "summary": "Sales for a date range", "parameters": [{ "$ref": "#/components/parameters/shopId" }, { "$ref": "#/components/parameters/from" }, { "$ref": "#/components/parameters/to" }, { "name": "days", "in": "query", "schema": { "type": "string", "enum": ["true", "false"] }, "description": "false leaves out the daily lines" }],
      "responses": { "200": { "description": "OK", "content": { "application/json": { "schema": { "type": "object", "properties": { "data": { "$ref": "#/components/schemas/Sales" } } } } } }, "400": { "description": "Bad dates or range over 400 days", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } }, "404": { "description": "No such shop, or not visible to this person", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } } } } },
    "/v1/shops/{shop_id}/products/top": { "get": { "summary": "Best-selling products and categories", "parameters": [{ "$ref": "#/components/parameters/shopId" }, { "$ref": "#/components/parameters/from" }, { "$ref": "#/components/parameters/to" }, { "name": "limit", "in": "query", "schema": { "type": "integer", "minimum": 1, "maximum": 50, "default": 10 } }], "responses": { "200": { "description": "OK" } } } },
    "/v1/shops/{shop_id}/customers": { "get": { "summary": "The credit book (owner only)", "parameters": [{ "$ref": "#/components/parameters/shopId" }], "responses": { "200": { "description": "OK", "content": { "application/json": { "schema": { "type": "object", "properties": { "data": { "type": "object", "properties": { "customers": { "type": "array", "items": { "type": "object", "properties": { "customer_id": { "type": "integer" }, "name": { "type": "string" }, "phone": { "type": ["string", "null"] }, "balance": { "type": "number" }, "balance_minor": { "type": "integer" } } } } } } } } } } }, "403": { "description": "Not the owner" } } } },
    "/v1/shops/{shop_id}/customers/{customer_id}/activity": { "get": { "summary": "One credit customer's purchases and payments (owner only)", "parameters": [{ "$ref": "#/components/parameters/shopId" }, { "name": "customer_id", "in": "path", "required": true, "schema": { "type": "integer" } }], "responses": { "200": { "description": "OK" }, "403": { "description": "Not the owner" } } } },
    "/v1/shops/{shop_id}/bookings": { "get": { "summary": "Appointments (Booking). Customer first name only.", "parameters": [{ "$ref": "#/components/parameters/shopId" }, { "$ref": "#/components/parameters/from" }, { "$ref": "#/components/parameters/to" }], "responses": { "200": { "description": "OK" } } } },
    "/v1/shops/{shop_id}/devices": { "get": { "summary": "Linked phones and tablets (owner only)", "parameters": [{ "$ref": "#/components/parameters/shopId" }], "responses": { "200": { "description": "OK" } } } }
  }
}
