---
title: "文書の変更"
method: PATCH
path: "/knowledge/documents/{document_id}"
tags: ["Documents"]
---

# 文書の変更

`PATCH /knowledge/documents/{document_id}`

## Request

- Base URL: `https://api.leafchat.app/api/v1`
- URL: `https://api.leafchat.app/api/v1/knowledge/documents/{document_id}`
- Auth: HTTP bearer

次の 3 つの組のうち 1 つだけを送る。組を混ぜると 400。

- 本文：content と content_type、または edits（どちらか一方）。filename は任意。If-Match が必須（無ければ 428）

- フォルダ：folder_id

- ソース：source_id（同期のソースへ移すときは external_id も）

## Path parameters

- `document_id` string, required

## Headers

- `if-match` string

## Request body

- PatchDocument
  - `content` string
  - `content_type` 'text/markdown' | 'text/plain' | 'application/json'
  - `edits` object[]
    - `old_text` string, required
    - `new_text` string, required
  - `filename` string
  - `folder_id` string
  - `source_id` string
  - `external_id` string, nullable: 外部 ID は、ソースの中で一意の文字列です。UTF-8 で 512 バイトまで、Unicode の NFC にそろえ、`/` で区切れます。空の区切り・`.`・`..`・区切りの前後の空白・制御文字・`\` は使えません。例：`faq/shipping`、`渋谷店/営業時間.md`

## Response `200`

フォルダ・ソースを変えた（文書を返す）。本文が同じで版を作らなかったときは DocumentAccepted（created: false）

- union
  - Document
    - `id` string, required
    - `source_id` string, required
    - `external_id` string, nullable, required
    - `folder_id` string, nullable, required: null はフォルダに割り当てる前に登録した文書。サイト別の出し分けを有効にすると既定のフォルダに入る
    - `filename` string, required
    - `mime` string, nullable, required
    - `size` integer, nullable, required
    - `status` 'processing' | 'pending' | 'ready' | 'error', required
    - `current_revision` RevisionRef, required
      - `id` string, required
      - `number` integer, required
      - `state` 'queued' | 'processing' | 'indexing' | 'live' | 'failed' | 'superseded' | 'abandoned', required
      - `error_message` string, nullable, required
      - `created_at` string, date-time, required
    - `head_revision` RevisionRef, required
      - `id` string, required
      - `number` integer, required
      - `state` 'queued' | 'processing' | 'indexing' | 'live' | 'failed' | 'superseded' | 'abandoned', required
      - `error_message` string, nullable, required
      - `created_at` string, date-time, required
    - `created_at` string, date-time, required
    - `updated_at` string, date-time, required: 内容の更新日時。公開中の版（無ければ head_revision）の内容を受け付けた時刻。フォルダの移動・状態の変化・システムの取り込み直しでは変わらない
  - DocumentAccepted
    - `document_id` string, required
    - `revision_id` string, required
    - `created` boolean, required: 新しい文書（POST）か新しい版（PATCH・復元・POST の上書き）を作ったか。同じ内容なら false で、状態は 200
    - `replaced` boolean: POST だけ。同じフォルダに同じ名前の文書があり、新しく作らずにその文書を上書きしたら true（状態は 200、document_id はその文書）。外部 ID つきの作成は名前で照合しない

## Other responses

- `202`: 本文の変更を受け付けた
- `400`: 要求の形式か値が正しくない。errors（検証の誤り）に項目（path）ごとの理由が入る 返る code：`invalid_request`・`empty_manifest`・`duplicate_item`
- `401`: API キーが無いか無効（期限切れ・失効・削除の途中の組織を含み、理由は区別しない） 返る code：`unauthorized`
- `403`: この API キーの用途か、対象のソースでは実行できない 返る code：`forbidden`・`needs_draft`
- `404`: 指定した ID かパスが無い（ほかの組織の ID も同じ応答になる）。削除した文書は、削除の起票が保守の GC で消えるまで DELETE が already_deleted を返し、その後は 404 返る code：`not_found`
- `409`: 今の状態と合わない。code で見分ける 返る code：`source_locked`・`limit_exceeded`・`delete_limit_exceeded`・`folder_not_found`・`folder_limit_exceeded`・`upload_required`・`upload_not_expected`・`sync_in_progress`・`not_sync_source`・`same_name_exists`・`external_id_taken`・`draft_limit_exceeded`・`already_decided`・`not_retryable`
- `412`: If-Match（下書きの作成では base_revision_id）の版が、今の head の版と違う。存在しない文書へ If-Match を付けた upsert でも返る 返る code：`precondition_failed`
- `413`: 本文が上限を超えた（JSON 6 MiB・multipart 16 MiB・同期 8 MiB・同期のアップロード 15 MiB）。テキストの本文が 4 MiB を超えたときは、本文の上限の内側でも 422 invalid_request になる 返る code：`payload_too_large`
- `415`: Content-Type が、その操作の受け付ける形式でない。文書の作成に application/x-www-form-urlencoded で送ると、415 ではなく 400 になる 返る code：`unsupported_media_type`
- `422`: 内容を受け付けられない（編集の対象・分割の上限・形式・同期の項目・アップロードの中身など）。code で見分ける 返る code：`invalid_request`・`edit_no_match`・`edit_ambiguous`・`not_editable`・`too_many_parts`・`invalid_item`・`upload_mismatch`
- `428`: 本文を変える PATCH に If-Match が無い 返る code：`precondition_required`
- `429`: 上限を超えた。Retry-After の秒数だけ待ってからやり直す。上限は、API キーごとに 1 分 120 回（REST と MCP で共通）・同期の実行は鍵ごとに 1 分 10 回（dry_run は数えない）・組織の文書の書き込みは 1 分 300 版・API キーの検証の失敗は IP ごとに 1 分 20 回（超えると、正しい鍵でも 429 になる）。このほか、IP ごとの上限を超えると Cloudflare が 429（error code 1015）を返す。この応答は problem+json でないことがあり、Retry-After が無ければ少し待ってからやり直す 返る code：`rate_limited`
- `500`: サーバーで問題が起きた。少し待ってからやり直す 返る code：`internal_error`
- `503`: 保存かキューへの送信に失敗した（storage_failed）。Retry-After の秒数だけ待ってからやり直す。search_unavailable は今の操作からは返らない（変換の口と MCP のために目録に残している） 返る code：`storage_failed`・`search_unavailable`

---

[API](https://skmtc.dev/leafchat/apis/leafchat-api.md) · [All operations](https://skmtc.dev/leafchat/apis/leafchat-api/llms.txt) · [OpenAPI document](https://skmtc.dev/leafchat/apis/leafchat-api/revisions/2667db05c2ff?raw)
