{
  "openapi": "3.1.0",
  "info": {
    "title": "ofershap.github.io Engagement API",
    "version": "1.0.0",
    "description": "Anonymous engagement counters (views and likes) for articles on ofershap.github.io. No authentication. Requests must come from the site origin (CORS enforced). Rate limited per IP.",
    "contact": {
      "name": "Ofer Shapira",
      "url": "https://ofershap.github.io/about/en/"
    }
  },
  "servers": [
    {
      "url": "https://blog-engagement.ofers.workers.dev",
      "description": "Cloudflare Workers production"
    }
  ],
  "paths": {
    "/health": {
      "get": {
        "operationId": "getHealth",
        "summary": "Service health check",
        "responses": {
          "200": {
            "description": "Service is up",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "version": {
                      "type": "string"
                    },
                    "counting_since": {
                      "type": "string",
                      "format": "date"
                    }
                  },
                  "required": [
                    "ok",
                    "version"
                  ]
                }
              }
            }
          }
        }
      }
    },
    "/stats": {
      "get": {
        "operationId": "getStats",
        "summary": "Read engagement counters for one article",
        "parameters": [
          {
            "name": "key",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string"
            },
            "description": "Article path as served on the site, e.g. /posts/some-article/. Only published article paths are accepted."
          },
          {
            "name": "client",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string",
              "pattern": "^[a-f0-9]{32}$"
            },
            "description": "Random 32-hex client identifier generated and stored by the reader browser; used only to deduplicate views and likes."
          }
        ],
        "responses": {
          "200": {
            "description": "Counters for the article",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Stats"
                }
              }
            }
          },
          "400": {
            "description": "Invalid parameters",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Origin header missing or not allowed",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Unknown path",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Rate limited (30 requests per minute per IP)",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      },
      "post": {
        "operationId": "recordEngagement",
        "summary": "Record a view, like or unlike for one article",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "key": {
                    "type": "string",
                    "description": "Article path, same rule as the GET parameter."
                  },
                  "client": {
                    "type": "string",
                    "pattern": "^[a-f0-9]{32}$"
                  },
                  "action": {
                    "type": "string",
                    "enum": [
                      "view",
                      "like",
                      "unlike"
                    ]
                  }
                },
                "required": [
                  "key",
                  "client",
                  "action"
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Updated counters",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Stats"
                }
              }
            }
          },
          "400": {
            "description": "Invalid JSON or parameters",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Origin header missing or not allowed",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "405": {
            "description": "Method not allowed",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "413": {
            "description": "Body over 1 KB",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Rate limited",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Stats": {
        "type": "object",
        "properties": {
          "likes": {
            "type": "integer",
            "minimum": 0
          },
          "liked": {
            "type": "boolean",
            "description": "Whether this client id already liked the article."
          },
          "views": {
            "type": "integer",
            "minimum": 0,
            "description": "Present only after the article passes the public view threshold; omitted below it."
          }
        },
        "required": [
          "likes",
          "liked"
        ]
      },
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "string"
          }
        },
        "required": [
          "error"
        ]
      }
    }
  }
}