# screenshot.diff

Compare public web pages or a stored screenshot baseline at a fixed viewport.

- REST: `POST https://api.kanagu.dev/v1/screenshot/diff`
- MCP tool: `screenshot.diff` at https://api.kanagu.dev/mcp
- Price: $0.01 USD per successful call. Trial: 10 calls per IP per UTC day without a key, shared by REST and MCP.
- Auth: `Authorization: Bearer <key>` for billed calls. Retries: reuse `Idempotency-Key`.

## Request body

- `url` (string (uri, max 2048 chars), required)
- `viewport` (object)
- `viewport.width` (integer (320 to 1600), required)
- `viewport.height` (integer (240 to 1200), required)
- `waitUntil` ("load" | "domcontentloaded" | "networkidle0" | "networkidle2")
- `waitForSelector` (string (max 256 chars))
- `waitMs` (integer (0 to 2000))
- `maskSelectors` (array of string (max 256 chars))
- `threshold` (number (0 to 0.5))

Then exactly one of these shapes:

1. With compareToUrl:

- `compareToUrl` (string (uri, max 2048 chars), required)

2. With baselineId:

- `baselineId` (string (uuid), required): ID of a before or after capture from a prior comparison.

## Response

- `changedPixels` (integer (-9007199254740991 to 9007199254740991), required)
- `diffRatio` (number, required)
- `width` (integer (-9007199254740991 to 9007199254740991), required)
- `height` (integer (-9007199254740991 to 9007199254740991), required)
- `rectangles` (array of object, required)
- `rectangles[].x` (integer (-9007199254740991 to 9007199254740991), required)
- `rectangles[].y` (integer (-9007199254740991 to 9007199254740991), required)
- `rectangles[].width` (integer (-9007199254740991 to 9007199254740991), required)
- `rectangles[].height` (integer (-9007199254740991 to 9007199254740991), required)
- `before` (object, required)
- `before.id` (string (uuid), required)
- `before.url` (string (uri), required)
- `before.expiresAt` (string (date-time), required)
- `after` (object, required)
- `after.id` (string (uuid), required)
- `after.url` (string (uri), required)
- `after.expiresAt` (string (date-time), required)
- `diff` (object, required)
- `diff.id` (string (uuid), required)
- `diff.url` (string (uri), required)
- `diff.expiresAt` (string (date-time), required)

## Example

```sh
curl https://api.kanagu.dev/v1/screenshot/diff \
  -H "Authorization: Bearer $KANAGU_KEY" \
  -H "Content-Type: application/json" \
  -d '{"url":"https://www.iana.org/domains/reserved","compareToUrl":"https://www.rfc-editor.org/about/","viewport":{"width":1024,"height":768},"waitUntil":"load","waitForSelector":"h1"}'
```

```ts
const res = await fetch("https://api.kanagu.dev/v1/screenshot/diff", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.KANAGU_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "url": "https://www.iana.org/domains/reserved",
    "compareToUrl": "https://www.rfc-editor.org/about/",
    "viewport": {
      "width": 1024,
      "height": 768
    },
    "waitUntil": "load",
    "waitForSelector": "h1"
  }),
});
if (!res.ok) throw new Error((await res.json()).error.message);
const result = await res.json();
```

## Errors

JSON `{"error":{"code","message"}}`.

- 400 `invalid_input`: The body does not match the request schema. The message names each field.
- 400 `invalid_json`: The body is missing or is not valid JSON.
- 400 `invalid_idempotency_key`: Idempotency-Key must be 1–128 visible ASCII characters.
- 401 `invalid_api_key`: The Bearer key is missing, malformed, invalid or inactive.
- 402 `free_allowance_exhausted`: This month's free usage is used up and the account has no subscription. Subscribe from the dashboard, or wait for the next UTC month; Retry-After gives the seconds left.
- 404 `not_found`: No operation at this path.
- 409 `idempotency_conflict`: The Idempotency-Key was already used with a different body.
- 413 `payload_too_large`: JSON bodies may be at most 64 KiB.
- 429 `trial_limit`: The anonymous trial allows 10 calls per IP per UTC day, shared by REST and MCP. Retry-After gives the seconds left.
- 500 `internal_error`: The call failed on our side. It is not billed; retry with the same Idempotency-Key.

## JSON Schema

Request:
```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "oneOf": [
    {
      "type": "object",
      "properties": {
        "url": {
          "type": "string",
          "maxLength": 2048,
          "format": "uri"
        },
        "viewport": {
          "default": {
            "width": 1280,
            "height": 720
          },
          "type": "object",
          "properties": {
            "width": {
              "type": "integer",
              "minimum": 320,
              "maximum": 1600
            },
            "height": {
              "type": "integer",
              "minimum": 240,
              "maximum": 1200
            }
          },
          "required": [
            "width",
            "height"
          ],
          "additionalProperties": false
        },
        "waitUntil": {
          "default": "domcontentloaded",
          "type": "string",
          "enum": [
            "load",
            "domcontentloaded",
            "networkidle0",
            "networkidle2"
          ]
        },
        "waitForSelector": {
          "type": "string",
          "minLength": 1,
          "maxLength": 256
        },
        "waitMs": {
          "default": 200,
          "type": "integer",
          "minimum": 0,
          "maximum": 2000
        },
        "maskSelectors": {
          "default": [],
          "maxItems": 20,
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1,
            "maxLength": 256
          }
        },
        "threshold": {
          "default": 0.1,
          "type": "number",
          "minimum": 0,
          "maximum": 0.5
        },
        "compareToUrl": {
          "type": "string",
          "maxLength": 2048,
          "format": "uri"
        }
      },
      "required": [
        "url",
        "compareToUrl"
      ],
      "additionalProperties": false
    },
    {
      "type": "object",
      "properties": {
        "url": {
          "type": "string",
          "maxLength": 2048,
          "format": "uri"
        },
        "viewport": {
          "default": {
            "width": 1280,
            "height": 720
          },
          "type": "object",
          "properties": {
            "width": {
              "type": "integer",
              "minimum": 320,
              "maximum": 1600
            },
            "height": {
              "type": "integer",
              "minimum": 240,
              "maximum": 1200
            }
          },
          "required": [
            "width",
            "height"
          ],
          "additionalProperties": false
        },
        "waitUntil": {
          "default": "domcontentloaded",
          "type": "string",
          "enum": [
            "load",
            "domcontentloaded",
            "networkidle0",
            "networkidle2"
          ]
        },
        "waitForSelector": {
          "type": "string",
          "minLength": 1,
          "maxLength": 256
        },
        "waitMs": {
          "default": 200,
          "type": "integer",
          "minimum": 0,
          "maximum": 2000
        },
        "maskSelectors": {
          "default": [],
          "maxItems": 20,
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1,
            "maxLength": 256
          }
        },
        "threshold": {
          "default": 0.1,
          "type": "number",
          "minimum": 0,
          "maximum": 0.5
        },
        "baselineId": {
          "type": "string",
          "format": "uuid",
          "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$",
          "description": "ID of a before or after capture from a prior comparison."
        }
      },
      "required": [
        "url",
        "baselineId"
      ],
      "additionalProperties": false
    }
  ],
  "examples": [
    {
      "url": "https://www.iana.org/domains/reserved",
      "compareToUrl": "https://www.rfc-editor.org/about/",
      "viewport": {
        "width": 1024,
        "height": 768
      },
      "waitUntil": "load",
      "waitForSelector": "h1"
    }
  ],
  "type": "object"
}
```

Response:
```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "changedPixels": {
      "type": "integer",
      "minimum": -9007199254740991,
      "maximum": 9007199254740991
    },
    "diffRatio": {
      "type": "number"
    },
    "width": {
      "type": "integer",
      "minimum": -9007199254740991,
      "maximum": 9007199254740991
    },
    "height": {
      "type": "integer",
      "minimum": -9007199254740991,
      "maximum": 9007199254740991
    },
    "rectangles": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "x": {
            "type": "integer",
            "minimum": -9007199254740991,
            "maximum": 9007199254740991
          },
          "y": {
            "type": "integer",
            "minimum": -9007199254740991,
            "maximum": 9007199254740991
          },
          "width": {
            "type": "integer",
            "minimum": -9007199254740991,
            "maximum": 9007199254740991
          },
          "height": {
            "type": "integer",
            "minimum": -9007199254740991,
            "maximum": 9007199254740991
          }
        },
        "required": [
          "x",
          "y",
          "width",
          "height"
        ],
        "additionalProperties": false
      }
    },
    "before": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string",
          "format": "uuid",
          "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$"
        },
        "url": {
          "type": "string",
          "format": "uri"
        },
        "expiresAt": {
          "type": "string",
          "format": "date-time",
          "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z))$"
        }
      },
      "required": [
        "id",
        "url",
        "expiresAt"
      ],
      "additionalProperties": false
    },
    "after": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string",
          "format": "uuid",
          "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$"
        },
        "url": {
          "type": "string",
          "format": "uri"
        },
        "expiresAt": {
          "type": "string",
          "format": "date-time",
          "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z))$"
        }
      },
      "required": [
        "id",
        "url",
        "expiresAt"
      ],
      "additionalProperties": false
    },
    "diff": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string",
          "format": "uuid",
          "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$"
        },
        "url": {
          "type": "string",
          "format": "uri"
        },
        "expiresAt": {
          "type": "string",
          "format": "date-time",
          "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z))$"
        }
      },
      "required": [
        "id",
        "url",
        "expiresAt"
      ],
      "additionalProperties": false
    }
  },
  "required": [
    "changedPixels",
    "diffRatio",
    "width",
    "height",
    "rectangles",
    "before",
    "after",
    "diff"
  ],
  "additionalProperties": false
}
```
