# AI

> Draft email text with the AI gateway.

URL: https://docs.workwae.com/reference/ai

Guides: [AI](/guides/ai).

| Method | Path | Operation | Scope |
| --- | --- | --- | --- |
| `POST` | `/v1/ai/draft` | [Draft email text with AI](#draft-email-text-with-ai) | `write` |

## Draft email text with AI

`POST /v1/ai/draft`

TypeScript SDK method: `workwae.ai.draft()`

Writes an email body from instructions, optionally answering a message or reworking a draft, in plain text or Markdown. Nothing is sent or saved: the text is for a person to edit. Needs the write scope; drafts are held to a per-minute limit (beyond it, rate_limited with Retry-After). A workspace without a subscription (the free tier) may write only as many drafts a month as its included amount (plan_limit_ai), and one that is not its owner's Free workspace and has no plan may write none (plan_required).

Required scope: `write`.

### Request body (required)

Content type `application/json`.

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `instructions` | string | yes | 1 to 2000 characters. |
| `context` | object | no |  |
| `context.accountId` | string (uuid) | no | Example `"6f1c2a4e-8b3d-4e5f-9a7b-1c2d3e4f5a6b"`. |
| `context.replyToMessageId` | string (uuid) | no | Example `"79eec251-5175-4a10-bc8b-c3dfdadd90c7"`. |
| `context.currentDraft` | string | no | At most 20000 characters. |
| `format` | "text" \| "markdown" | yes |  |

### Responses

- **200** OK (application/json, object)

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `text` | string | yes |  |

- **Errors**, by status and `code` (see [Errors](/guides/errors)):
  - 400: `invalid_input`, `invalid_idempotency_key`, `ai_disabled`, `ai_refused`
  - 401: `unauthenticated`, `api_key_expired`
  - 402: `plan_required`, `spend_cap_reached`
  - 403: `missing_scope`, `role_forbidden`, `account_token_forbidden`, `account_scope_forbidden`, `mcp_only_key`, `plan_module_unavailable`, `plan_limit_ai`, `provider_forbidden`
  - 404: `provider_not_found`
  - 409: `idempotency_key_in_progress`, `account_paused`, `account_disabled`, `account_reauth_required`, `account_misconfigured`, `mail_disabled`
  - 413: `payload_too_large`
  - 422: `idempotency_key_reused`, `unknown_account`, `unknown_message`
  - 429: `rate_limited`, `too_many_requests`, `provider_throttled`
  - 500: `internal_error`
  - 502: `provider_unavailable`, `ai_unavailable`, `ai_suspended`
  - 503: `overloaded`
