# email.parse

Parse an RFC 5322 email into headers, addresses, MIME bodies, attachments, and thread IDs.

- REST: `POST https://api.kanagu.dev/v1/email/parse`
- MCP tool: `email.parse` 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

- `includeContent` (boolean): Include decoded attachment bytes as Base64.

Then exactly one of these shapes:

1. With text:

- `text` (string (max 1048576 chars), required): UTF-8 email, up to 1 MiB.

2. With base64:

- `base64` (string (base64, max 1398104 chars), required): Raw email bytes as Base64, up to 1 MiB decoded. Use for legacy charsets.

## Response

- `headers` (array of object, required)
- `headers[].key` (string, required)
- `headers[].originalKey` (string, required)
- `headers[].value` (string, required)
- `addresses` (object, required)
- `addresses.from` (array of object, required)
- `addresses.sender` (object)
- `addresses.replyTo` (array of object, required)
- `addresses.to` (array of object, required)
- `addresses.cc` (array of object, required)
- `addresses.bcc` (array of object, required)
- `addresses.returnPath` (string)
- `addresses.deliveredTo` (string)
- `subject` (string)
- `date` (string (date-time))
- `dateRaw` (string)
- `text` (string)
- `html` (string): Original decoded HTML. Treat as untrusted email content.
- `attachments` (array of object, required): Attachments at this message level. Parse an attached .eml to inspect its contents.
- `attachments[].name` (string | null, required)
- `attachments[].type` (string, required)
- `attachments[].size` (integer (-9007199254740991 to 9007199254740991), required)
- `attachments[].sha256` (string, required)
- `attachments[].inline` (boolean, required)
- `attachments[].cid` (string)
- `attachments[].contentBase64` (string)
- `inlineImages` (object, required): Content-ID without angle brackets mapped to zero-based attachment indexes.
- `thread` (object, required)
- `thread.messageId` (string)
- `thread.inReplyTo` (array of string, required)
- `thread.references` (array of string, required)

## Example

```sh
curl https://api.kanagu.dev/v1/email/parse \
  -H "Authorization: Bearer $KANAGU_KEY" \
  -H "Content-Type: application/json" \
  -d '{"text":"From: Maya Chen <maya@example.com>\r\nTo: Dev Team <dev@example.net>\r\nSubject: Re: Import window for December\r\nDate: Mon, 23 Nov 2026 09:30:00 -0500\r\nMessage-ID: <import-window-2@example.com>\r\nIn-Reply-To: <import-window-1@example.net>\r\nReferences: <import-window-1@example.net>\r\nMIME-Version: 1.0\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nThe next import will start at 10:00 a.m. Eastern on December 1."}'
```

```ts
const res = await fetch("https://api.kanagu.dev/v1/email/parse", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.KANAGU_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "text": "From: Maya Chen <maya@example.com>\r\nTo: Dev Team <dev@example.net>\r\nSubject: Re: Import window for December\r\nDate: Mon, 23 Nov 2026 09:30:00 -0500\r\nMessage-ID: <import-window-2@example.com>\r\nIn-Reply-To: <import-window-1@example.net>\r\nReferences: <import-window-1@example.net>\r\nMIME-Version: 1.0\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nThe next import will start at 10:00 a.m. Eastern on December 1."
  }),
});
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": {
        "includeContent": {
          "default": false,
          "description": "Include decoded attachment bytes as Base64.",
          "type": "boolean"
        },
        "text": {
          "type": "string",
          "minLength": 1,
          "maxLength": 1048576,
          "description": "UTF-8 email, up to 1 MiB."
        }
      },
      "required": [
        "text"
      ],
      "additionalProperties": false
    },
    {
      "type": "object",
      "properties": {
        "includeContent": {
          "default": false,
          "description": "Include decoded attachment bytes as Base64.",
          "type": "boolean"
        },
        "base64": {
          "type": "string",
          "minLength": 1,
          "maxLength": 1398104,
          "format": "base64",
          "contentEncoding": "base64",
          "pattern": "^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/]{3}=))?$",
          "description": "Raw email bytes as Base64, up to 1 MiB decoded. Use for legacy charsets."
        }
      },
      "required": [
        "base64"
      ],
      "additionalProperties": false
    }
  ],
  "examples": [
    {
      "text": "From: Maya Chen <maya@example.com>\r\nTo: Dev Team <dev@example.net>\r\nSubject: Re: Import window for December\r\nDate: Mon, 23 Nov 2026 09:30:00 -0500\r\nMessage-ID: <import-window-2@example.com>\r\nIn-Reply-To: <import-window-1@example.net>\r\nReferences: <import-window-1@example.net>\r\nMIME-Version: 1.0\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nThe next import will start at 10:00 a.m. Eastern on December 1."
    }
  ],
  "type": "object"
}
```

Response:
```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "headers": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "key": {
            "type": "string"
          },
          "originalKey": {
            "type": "string"
          },
          "value": {
            "type": "string"
          }
        },
        "required": [
          "key",
          "originalKey",
          "value"
        ],
        "additionalProperties": false
      }
    },
    "addresses": {
      "type": "object",
      "properties": {
        "from": {
          "type": "array",
          "items": {
            "anyOf": [
              {
                "type": "object",
                "properties": {
                  "name": {
                    "type": "string"
                  },
                  "address": {
                    "type": "string"
                  }
                },
                "required": [
                  "name",
                  "address"
                ],
                "additionalProperties": false
              },
              {
                "type": "object",
                "properties": {
                  "name": {
                    "type": "string"
                  },
                  "group": {
                    "type": "array",
                    "items": {
                      "type": "object",
                      "properties": {
                        "name": {
                          "type": "string"
                        },
                        "address": {
                          "type": "string"
                        }
                      },
                      "required": [
                        "name",
                        "address"
                      ],
                      "additionalProperties": false
                    }
                  }
                },
                "required": [
                  "name",
                  "group"
                ],
                "additionalProperties": false
              }
            ]
          }
        },
        "sender": {
          "anyOf": [
            {
              "type": "object",
              "properties": {
                "name": {
                  "type": "string"
                },
                "address": {
                  "type": "string"
                }
              },
              "required": [
                "name",
                "address"
              ],
              "additionalProperties": false
            },
            {
              "type": "object",
              "properties": {
                "name": {
                  "type": "string"
                },
                "group": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "properties": {
                      "name": {
                        "type": "string"
                      },
                      "address": {
                        "type": "string"
                      }
                    },
                    "required": [
                      "name",
                      "address"
                    ],
                    "additionalProperties": false
                  }
                }
              },
              "required": [
                "name",
                "group"
              ],
              "additionalProperties": false
            }
          ]
        },
        "replyTo": {
          "type": "array",
          "items": {
            "anyOf": [
              {
                "type": "object",
                "properties": {
                  "name": {
                    "type": "string"
                  },
                  "address": {
                    "type": "string"
                  }
                },
                "required": [
                  "name",
                  "address"
                ],
                "additionalProperties": false
              },
              {
                "type": "object",
                "properties": {
                  "name": {
                    "type": "string"
                  },
                  "group": {
                    "type": "array",
                    "items": {
                      "type": "object",
                      "properties": {
                        "name": {
                          "type": "string"
                        },
                        "address": {
                          "type": "string"
                        }
                      },
                      "required": [
                        "name",
                        "address"
                      ],
                      "additionalProperties": false
                    }
                  }
                },
                "required": [
                  "name",
                  "group"
                ],
                "additionalProperties": false
              }
            ]
          }
        },
        "to": {
          "type": "array",
          "items": {
            "anyOf": [
              {
                "type": "object",
                "properties": {
                  "name": {
                    "type": "string"
                  },
                  "address": {
                    "type": "string"
                  }
                },
                "required": [
                  "name",
                  "address"
                ],
                "additionalProperties": false
              },
              {
                "type": "object",
                "properties": {
                  "name": {
                    "type": "string"
                  },
                  "group": {
                    "type": "array",
                    "items": {
                      "type": "object",
                      "properties": {
                        "name": {
                          "type": "string"
                        },
                        "address": {
                          "type": "string"
                        }
                      },
                      "required": [
                        "name",
                        "address"
                      ],
                      "additionalProperties": false
                    }
                  }
                },
                "required": [
                  "name",
                  "group"
                ],
                "additionalProperties": false
              }
            ]
          }
        },
        "cc": {
          "type": "array",
          "items": {
            "anyOf": [
              {
                "type": "object",
                "properties": {
                  "name": {
                    "type": "string"
                  },
                  "address": {
                    "type": "string"
                  }
                },
                "required": [
                  "name",
                  "address"
                ],
                "additionalProperties": false
              },
              {
                "type": "object",
                "properties": {
                  "name": {
                    "type": "string"
                  },
                  "group": {
                    "type": "array",
                    "items": {
                      "type": "object",
                      "properties": {
                        "name": {
                          "type": "string"
                        },
                        "address": {
                          "type": "string"
                        }
                      },
                      "required": [
                        "name",
                        "address"
                      ],
                      "additionalProperties": false
                    }
                  }
                },
                "required": [
                  "name",
                  "group"
                ],
                "additionalProperties": false
              }
            ]
          }
        },
        "bcc": {
          "type": "array",
          "items": {
            "anyOf": [
              {
                "type": "object",
                "properties": {
                  "name": {
                    "type": "string"
                  },
                  "address": {
                    "type": "string"
                  }
                },
                "required": [
                  "name",
                  "address"
                ],
                "additionalProperties": false
              },
              {
                "type": "object",
                "properties": {
                  "name": {
                    "type": "string"
                  },
                  "group": {
                    "type": "array",
                    "items": {
                      "type": "object",
                      "properties": {
                        "name": {
                          "type": "string"
                        },
                        "address": {
                          "type": "string"
                        }
                      },
                      "required": [
                        "name",
                        "address"
                      ],
                      "additionalProperties": false
                    }
                  }
                },
                "required": [
                  "name",
                  "group"
                ],
                "additionalProperties": false
              }
            ]
          }
        },
        "returnPath": {
          "type": "string"
        },
        "deliveredTo": {
          "type": "string"
        }
      },
      "required": [
        "from",
        "replyTo",
        "to",
        "cc",
        "bcc"
      ],
      "additionalProperties": false
    },
    "subject": {
      "type": "string"
    },
    "date": {
      "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))$"
    },
    "dateRaw": {
      "type": "string"
    },
    "text": {
      "type": "string"
    },
    "html": {
      "description": "Original decoded HTML. Treat as untrusted email content.",
      "type": "string"
    },
    "attachments": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "name": {
            "type": [
              "string",
              "null"
            ]
          },
          "type": {
            "type": "string"
          },
          "size": {
            "type": "integer",
            "minimum": -9007199254740991,
            "maximum": 9007199254740991
          },
          "sha256": {
            "type": "string"
          },
          "inline": {
            "type": "boolean"
          },
          "cid": {
            "type": "string"
          },
          "contentBase64": {
            "type": "string"
          }
        },
        "required": [
          "name",
          "type",
          "size",
          "sha256",
          "inline"
        ],
        "additionalProperties": false
      },
      "description": "Attachments at this message level. Parse an attached .eml to inspect its contents."
    },
    "inlineImages": {
      "type": "object",
      "propertyNames": {
        "type": "string"
      },
      "additionalProperties": {
        "type": "array",
        "items": {
          "type": "integer",
          "minimum": -9007199254740991,
          "maximum": 9007199254740991
        }
      },
      "description": "Content-ID without angle brackets mapped to zero-based attachment indexes."
    },
    "thread": {
      "type": "object",
      "properties": {
        "messageId": {
          "type": "string"
        },
        "inReplyTo": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "references": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      },
      "required": [
        "inReplyTo",
        "references"
      ],
      "additionalProperties": false
    }
  },
  "required": [
    "headers",
    "addresses",
    "attachments",
    "inlineImages",
    "thread"
  ],
  "additionalProperties": false
}
```
