# POST /ai/generate-document

**Resource:** [AI](./ai.md)  
**Scopes:** `ai:use`  
**Write operation:** yes

Generate a document template with AI -- proposals, contracts, reports, invoices, letters. Produces structured sections (cover page, text blocks, tables, signature blocks) with {{variable}} tokens for auto-filling. By default (persist=true), creates the document_templates row and all document_template_sections rows and returns the new template_id. Costs credits (ai_tool_generate).

## Parameters

| Name | In | Type | Required | Description |
|------|----|------|----------|-------------|
| `prompt` | body | string | yes | Plain-language description of the document. Be specific about purpose, tone, and major sections. |
| `persist` | body | boolean | no | If true (default), creates the document template and its sections in the workspace. If false, returns only the generated structure. |
| `name` | body | string | no | Override the AI-generated document name. |

## Response example

```json
{
  "data": {
    "template": {
      "id": "uuid",
      "name": "Service Proposal",
      "description": "...",
      "category": "proposal",
      "page_size": "A4",
      "created_at": "2026-04-19T..."
    },
    "section_count": 7,
    "persisted": true
  },
  "meta": {
    "credits_remaining": 9200
  }
}
```

---
Base URL: `https://api.trustpager.com/functions/v1/api/v1` — Auth: `Authorization: Bearer YOUR_API_KEY`