# carf.validate

Check UK CARF XML v1.5 against the OECD XSD, HMRC file rules and supported TIN formats. Billed per user processed after XSD validation, up to 10,000.

- REST: `POST https://api.kanagu.dev/v1/carf/validate`
- MCP tool: `carf.validate` at https://api.kanagu.dev/mcp
- Price: $0.10 USD per user, on successful calls only. 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

- `history` (array of string): Complete prior accepted CARF XML, oldest first. For correct, supply the files before previousXml.
- `xml` (string, required)
- `filename` (string (max 255 chars))

## Response

- `valid` (boolean, required)
- `reportableUsers` (integer (0 to 10000), required)
- `errors` (array of object, required)
- `errors[].rule` (string, required)
- `errors[].path` (string, required)
- `errors[].line` (integer (≤ 9007199254740991))
- `errors[].expected` (string, required)
- `errors[].actual` (string, required)
- `errors[].source` (string, required)
- `needsReview` (array of object, required)
- `needsReview[].rule` (string, required)
- `needsReview[].path` (string, required)
- `needsReview[].line` (integer (≤ 9007199254740991))
- `needsReview[].expected` (string, required)
- `needsReview[].actual` (string, required)
- `needsReview[].source` (string, required)
- `schemaVersion` ("1.5", required)

## Example

```sh
curl https://api.kanagu.dev/v1/carf/validate \
  -H "Authorization: Bearer $KANAGU_KEY" \
  -H "Content-Type: application/json" \
  -d '{"xml":"<?xml version=\"1.0\" encoding=\"UTF-8\"?><CARF_OECD xmlns=\"urn:oecd:ties:carf:v1\" version=\"1.5\"><MessageSpec><SendingEntityIN>XRCASP000001234</SendingEntityIN><TransmittingCountry>GB</TransmittingCountry><ReceivingCountry>GB</ReceivingCountry><MessageType>CARF</MessageType><MessageRefId>GB2026GB-XRCASP000001234-nil</MessageRefId><MessageTypeIndic>CARF703</MessageTypeIndic><ReportingPeriod>2026-12-31</ReportingPeriod><Timestamp>2027-05-01T09:00:00</Timestamp></MessageSpec></CARF_OECD>","filename":"GB2026GB-XRCASP000001234-nil.xml"}'
```

```ts
const res = await fetch("https://api.kanagu.dev/v1/carf/validate", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.KANAGU_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "xml": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><CARF_OECD xmlns=\"urn:oecd:ties:carf:v1\" version=\"1.5\"><MessageSpec><SendingEntityIN>XRCASP000001234</SendingEntityIN><TransmittingCountry>GB</TransmittingCountry><ReceivingCountry>GB</ReceivingCountry><MessageType>CARF</MessageType><MessageRefId>GB2026GB-XRCASP000001234-nil</MessageRefId><MessageTypeIndic>CARF703</MessageTypeIndic><ReportingPeriod>2026-12-31</ReportingPeriod><Timestamp>2027-05-01T09:00:00</Timestamp></MessageSpec></CARF_OECD>",
    "filename": "GB2026GB-XRCASP000001234-nil.xml"
  }),
});
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",
  "type": "object",
  "properties": {
    "history": {
      "description": "Complete prior accepted CARF XML, oldest first. For correct, supply the files before previousXml.",
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1
      }
    },
    "xml": {
      "type": "string",
      "minLength": 1
    },
    "filename": {
      "type": "string",
      "minLength": 1,
      "maxLength": 255
    }
  },
  "required": [
    "xml"
  ],
  "additionalProperties": false,
  "examples": [
    {
      "xml": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><CARF_OECD xmlns=\"urn:oecd:ties:carf:v1\" version=\"1.5\"><MessageSpec><SendingEntityIN>XRCASP000001234</SendingEntityIN><TransmittingCountry>GB</TransmittingCountry><ReceivingCountry>GB</ReceivingCountry><MessageType>CARF</MessageType><MessageRefId>GB2026GB-XRCASP000001234-nil</MessageRefId><MessageTypeIndic>CARF703</MessageTypeIndic><ReportingPeriod>2026-12-31</ReportingPeriod><Timestamp>2027-05-01T09:00:00</Timestamp></MessageSpec></CARF_OECD>",
      "filename": "GB2026GB-XRCASP000001234-nil.xml"
    }
  ]
}
```

Response:
```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "valid": {
      "type": "boolean"
    },
    "reportableUsers": {
      "type": "integer",
      "minimum": 0,
      "maximum": 10000
    },
    "errors": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "rule": {
            "type": "string"
          },
          "path": {
            "type": "string"
          },
          "line": {
            "type": "integer",
            "exclusiveMinimum": 0,
            "maximum": 9007199254740991
          },
          "expected": {
            "type": "string"
          },
          "actual": {
            "type": "string"
          },
          "source": {
            "type": "string"
          }
        },
        "required": [
          "rule",
          "path",
          "expected",
          "actual",
          "source"
        ],
        "additionalProperties": false
      }
    },
    "needsReview": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "rule": {
            "type": "string"
          },
          "path": {
            "type": "string"
          },
          "line": {
            "type": "integer",
            "exclusiveMinimum": 0,
            "maximum": 9007199254740991
          },
          "expected": {
            "type": "string"
          },
          "actual": {
            "type": "string"
          },
          "source": {
            "type": "string"
          }
        },
        "required": [
          "rule",
          "path",
          "expected",
          "actual",
          "source"
        ],
        "additionalProperties": false
      }
    },
    "schemaVersion": {
      "type": "string",
      "const": "1.5"
    }
  },
  "required": [
    "valid",
    "reportableUsers",
    "errors",
    "needsReview",
    "schemaVersion"
  ],
  "additionalProperties": false
}
```
