# Extract document data

Extract text, headings, links, or summary metadata from one document. All
routes require a bearer token and use `POST` with a JSON body.

Every request requires `bucket_id` (a non-nil dashed UUID), active bucket
Management membership and the exact action capability. Select a bucket
through [Buckets and membership](https://m7.org/docs/api/api.document.m7.org/buckets.md). For organization-owned
buckets, retain the same [workspace selection](https://m7.org/docs/api/api.document.m7.org/workspaces.md) on submission
and every job request.

## Routes

| Route | Required fields | Result | `result_format` |
| --- | --- | --- | --- |
| `/v1/document/extract/text` | `bucket_id`, `source`, `input_format` | Plain extracted text. | `text` |
| `/v1/document/extract/toc` | `bucket_id`, `source`, `input_format` | Heading rows with `level`, `text`, and `anchor`. | `json_structured` |
| `/v1/document/extract/links` | `bucket_id`, `source`, `input_format` | Link rows with `href` and `text`. | `json_structured` |
| `/v1/document/extract/meta` | `bucket_id`, `source`, `input_format` | `characters`, `words`, `headings`, `links`, and `first_heading`. | `json_structured` |

## Request fields

| Field | Required | Type | Description |
| --- | --- | --- | --- |
| `bucket_id` | Yes | UUID | Selected authorized bucket. |
| `source` | Yes | String | Non-empty document content. Leading and trailing whitespace is removed before processing. |
| `input_format` | Yes | String | Supported input format `name` or dashed UUID. |
| `bbcode` | No | Object | BBCode parsing options. Takes precedence over `bbcode_options`. |
| `bbcode_options` | No | Object | BBCode parsing options if `bbcode` is absent. |
| `job_ttl` | No | Integer | Retention for the extraction job record in seconds. Defaults to `3600` and is constrained to `60` through `86400`. |
| `metadata` | No | Object | Caller metadata stored with the retained job record; it does not alter extraction. |
| `filename` | No | String | Source filename stored with the retained job record. |
| `locale` | No | String | Source locale stored with the retained job record. |
| `options` | No | Object | Additional data stored with the retained job record; current extraction handlers do not apply this object. |

An empty `source` fails with `source is required`; an unsupported source format
fails with `unsupported input_format`.

Every successful route returns `input_format`, `result`, `result_format`,
`warnings`, `issues`, and `metadata` in `data`.

## Extract a table of contents

```bash
curl -sS https://api.document.service.m7.org/v1/document/extract/toc \
  -H 'Authorization: Bearer ACCESS_TOKEN' \
  -H 'Content-Type: application/json' \
  -d '{
    "bucket_id": "BUCKET_UUID",
    "source": "# Introduction\n\n## Installation",
    "input_format": "markdown"
  }'
```

```json
{
  "status": 1,
  "comment": "EXTRACT_TOC",
  "data": {
    "input_format": "markdown",
    "result": [
      {
        "level": 1,
        "text": "Introduction",
        "anchor": "introduction"
      },
      {
        "level": 2,
        "text": "Installation",
        "anchor": "installation"
      }
    ],
    "result_format": "json_structured",
    "warnings": [],
    "issues": [],
    "metadata": {
      "result_format": "json_structured"
    }
  }
}
```
