# csv.check

Check a CSV against a target schema and return diagnostics and a normalized preview.

- REST: `POST https://api.kanagu.dev/v1/csv/check`
- MCP tool: `csv.check` at https://api.kanagu.dev/mcp
- Price: $0.03 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

- `columns` (array of object, required): Target columns, in source order for headerless files.
- `columns[].name` (string (max 64 chars), required)
- `columns[].type` ("string" | "integer" | "number" | "boolean" | "date", required)
- `columns[].required` (boolean)
- `columns[].enum` (array of string): Allowed trimmed source values.
- `columns[].regex` (string (max 256 chars)): RE2 pattern searched within the trimmed source value.
- `columns[].locale` (string (max 40 chars)): BCP 47 number or date locale, default en-US.
- `columns[].dateOrder` ("YMD" | "DMY" | "MDY"): Override date component order.
- `delimiter` ("," | ";" | "\t" | "|"): Auto-detect by default.
- `encoding` ("utf-8" | "utf-16le" | "utf-16be" | "windows-1252" | "shift_jis" | "iso-2022-jp"): Raw file encoding. Auto-detects BOM, UTF-8, and Windows-1252.
- `header` (boolean): Auto-detect by matching names in the first record.

Then exactly one of these shapes:

1. With text:

- `text` (string (max 5242880 chars), required): UTF-8 CSV text, at most 5 MiB.

2. With base64:

- `base64` (string (base64, max 6990508 chars), required): File bytes as Base64, at most 5 MiB decoded.

## Response

- `valid` (boolean, required)
- `encoding` ("utf-8" | "utf-16le" | "utf-16be" | "windows-1252" | "shift_jis" | "iso-2022-jp", required)
- `delimiter` ("," | ";" | "\t" | "|", required)
- `header` (boolean, required)
- `rows` (integer (-9007199254740991 to 9007199254740991), required): Data records processed, excluding the header.
- `diagnostics` (array of object, required)
- `diagnostics[].code` ("required" | "not_in_enum" | "regex_mismatch" | "invalid_type" | "missing_column" | "duplicate_column" | … (9 values), required)
- `diagnostics[].column` (string)
- `diagnostics[].sourceColumn` (integer (-9007199254740991 to 9007199254740991))
- `diagnostics[].rows` (array of integer (-9007199254740991 to 9007199254740991), required): One-based CSV record numbers, including the header when present.
- `parseError` (object)
- `parseError.code` (string, required)
- `parseError.line` (integer (-9007199254740991 to 9007199254740991), required)
- `preview` (array of object, required): The first ten data records. String previews are capped at 64 characters.
- `preview[].row` (integer (-9007199254740991 to 9007199254740991), required)
- `preview[].values` (object, required)

## Example

```sh
curl https://api.kanagu.dev/v1/csv/check \
  -H "Authorization: Bearer $KANAGU_KEY" \
  -H "Content-Type: application/json" \
  -d '{"text":"invoice_id;issued_on;amount;status\r\nINV-00812;21.11.2026;1.242,50;paid\r\nINV-00813;31.11.2026;200,00;open\r\nINV-00814;23.11.2026;12,1,5;unknown","columns":[{"name":"invoice_id","type":"string","required":true,"regex":"^INV-[0-9]{5}$"},{"name":"issued_on","type":"date","locale":"de-DE","required":true},{"name":"amount","type":"number","locale":"de-DE","required":true},{"name":"status","type":"string","enum":["open","paid"]}]}'
```

```ts
const res = await fetch("https://api.kanagu.dev/v1/csv/check", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.KANAGU_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "text": "invoice_id;issued_on;amount;status\r\nINV-00812;21.11.2026;1.242,50;paid\r\nINV-00813;31.11.2026;200,00;open\r\nINV-00814;23.11.2026;12,1,5;unknown",
    "columns": [
      {
        "name": "invoice_id",
        "type": "string",
        "required": true,
        "regex": "^INV-[0-9]{5}$"
      },
      {
        "name": "issued_on",
        "type": "date",
        "locale": "de-DE",
        "required": true
      },
      {
        "name": "amount",
        "type": "number",
        "locale": "de-DE",
        "required": true
      },
      {
        "name": "status",
        "type": "string",
        "enum": [
          "open",
          "paid"
        ]
      }
    ]
  }),
});
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": {
        "columns": {
          "minItems": 1,
          "maxItems": 32,
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "name": {
                "type": "string",
                "minLength": 1,
                "maxLength": 64
              },
              "type": {
                "type": "string",
                "enum": [
                  "string",
                  "integer",
                  "number",
                  "boolean",
                  "date"
                ]
              },
              "required": {
                "default": false,
                "type": "boolean"
              },
              "enum": {
                "description": "Allowed trimmed source values.",
                "maxItems": 200,
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "regex": {
                "description": "RE2 pattern searched within the trimmed source value.",
                "type": "string",
                "maxLength": 256
              },
              "locale": {
                "description": "BCP 47 number or date locale, default en-US.",
                "type": "string",
                "maxLength": 40
              },
              "dateOrder": {
                "description": "Override date component order.",
                "type": "string",
                "enum": [
                  "YMD",
                  "DMY",
                  "MDY"
                ]
              }
            },
            "required": [
              "name",
              "type"
            ],
            "additionalProperties": false
          },
          "description": "Target columns, in source order for headerless files."
        },
        "delimiter": {
          "description": "Auto-detect by default.",
          "type": "string",
          "enum": [
            ",",
            ";",
            "\t",
            "|"
          ]
        },
        "encoding": {
          "description": "Raw file encoding. Auto-detects BOM, UTF-8, and Windows-1252.",
          "type": "string",
          "enum": [
            "utf-8",
            "utf-16le",
            "utf-16be",
            "windows-1252",
            "shift_jis",
            "iso-2022-jp"
          ]
        },
        "header": {
          "description": "Auto-detect by matching names in the first record.",
          "type": "boolean"
        },
        "text": {
          "type": "string",
          "minLength": 1,
          "maxLength": 5242880,
          "description": "UTF-8 CSV text, at most 5 MiB."
        }
      },
      "required": [
        "columns",
        "text"
      ],
      "additionalProperties": false
    },
    {
      "type": "object",
      "properties": {
        "columns": {
          "minItems": 1,
          "maxItems": 32,
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "name": {
                "type": "string",
                "minLength": 1,
                "maxLength": 64
              },
              "type": {
                "type": "string",
                "enum": [
                  "string",
                  "integer",
                  "number",
                  "boolean",
                  "date"
                ]
              },
              "required": {
                "default": false,
                "type": "boolean"
              },
              "enum": {
                "description": "Allowed trimmed source values.",
                "maxItems": 200,
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "regex": {
                "description": "RE2 pattern searched within the trimmed source value.",
                "type": "string",
                "maxLength": 256
              },
              "locale": {
                "description": "BCP 47 number or date locale, default en-US.",
                "type": "string",
                "maxLength": 40
              },
              "dateOrder": {
                "description": "Override date component order.",
                "type": "string",
                "enum": [
                  "YMD",
                  "DMY",
                  "MDY"
                ]
              }
            },
            "required": [
              "name",
              "type"
            ],
            "additionalProperties": false
          },
          "description": "Target columns, in source order for headerless files."
        },
        "delimiter": {
          "description": "Auto-detect by default.",
          "type": "string",
          "enum": [
            ",",
            ";",
            "\t",
            "|"
          ]
        },
        "encoding": {
          "description": "Raw file encoding. Auto-detects BOM, UTF-8, and Windows-1252.",
          "type": "string",
          "enum": [
            "utf-8",
            "utf-16le",
            "utf-16be",
            "windows-1252",
            "shift_jis",
            "iso-2022-jp"
          ]
        },
        "header": {
          "description": "Auto-detect by matching names in the first record.",
          "type": "boolean"
        },
        "base64": {
          "type": "string",
          "minLength": 1,
          "maxLength": 6990508,
          "format": "base64",
          "contentEncoding": "base64",
          "pattern": "^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/]{3}=))?$",
          "description": "File bytes as Base64, at most 5 MiB decoded."
        }
      },
      "required": [
        "columns",
        "base64"
      ],
      "additionalProperties": false
    }
  ],
  "examples": [
    {
      "text": "invoice_id;issued_on;amount;status\r\nINV-00812;21.11.2026;1.242,50;paid\r\nINV-00813;31.11.2026;200,00;open\r\nINV-00814;23.11.2026;12,1,5;unknown",
      "columns": [
        {
          "name": "invoice_id",
          "type": "string",
          "required": true,
          "regex": "^INV-[0-9]{5}$"
        },
        {
          "name": "issued_on",
          "type": "date",
          "locale": "de-DE",
          "required": true
        },
        {
          "name": "amount",
          "type": "number",
          "locale": "de-DE",
          "required": true
        },
        {
          "name": "status",
          "type": "string",
          "enum": [
            "open",
            "paid"
          ]
        }
      ]
    }
  ],
  "type": "object"
}
```

Response:
```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "valid": {
      "type": "boolean"
    },
    "encoding": {
      "type": "string",
      "enum": [
        "utf-8",
        "utf-16le",
        "utf-16be",
        "windows-1252",
        "shift_jis",
        "iso-2022-jp"
      ]
    },
    "delimiter": {
      "type": "string",
      "enum": [
        ",",
        ";",
        "\t",
        "|"
      ]
    },
    "header": {
      "type": "boolean"
    },
    "rows": {
      "type": "integer",
      "minimum": -9007199254740991,
      "maximum": 9007199254740991,
      "description": "Data records processed, excluding the header."
    },
    "diagnostics": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "code": {
            "type": "string",
            "enum": [
              "required",
              "not_in_enum",
              "regex_mismatch",
              "invalid_type",
              "missing_column",
              "duplicate_column",
              "unexpected_column",
              "extra_cells",
              "empty_csv"
            ]
          },
          "column": {
            "type": "string"
          },
          "sourceColumn": {
            "type": "integer",
            "minimum": -9007199254740991,
            "maximum": 9007199254740991
          },
          "rows": {
            "type": "array",
            "items": {
              "type": "integer",
              "minimum": -9007199254740991,
              "maximum": 9007199254740991
            },
            "description": "One-based CSV record numbers, including the header when present."
          }
        },
        "required": [
          "code",
          "rows"
        ],
        "additionalProperties": false
      }
    },
    "parseError": {
      "type": "object",
      "properties": {
        "code": {
          "type": "string"
        },
        "line": {
          "type": "integer",
          "minimum": -9007199254740991,
          "maximum": 9007199254740991
        }
      },
      "required": [
        "code",
        "line"
      ],
      "additionalProperties": false
    },
    "preview": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "row": {
            "type": "integer",
            "minimum": -9007199254740991,
            "maximum": 9007199254740991
          },
          "values": {
            "type": "object",
            "propertyNames": {
              "type": "string"
            },
            "additionalProperties": {
              "type": [
                "string",
                "number",
                "boolean",
                "null"
              ]
            }
          }
        },
        "required": [
          "row",
          "values"
        ],
        "additionalProperties": false
      },
      "description": "The first ten data records. String previews are capped at 64 characters."
    }
  },
  "required": [
    "valid",
    "encoding",
    "delimiter",
    "header",
    "rows",
    "diagnostics",
    "preview"
  ],
  "additionalProperties": false
}
```
