> ## Documentation Index
> Fetch the complete documentation index at: /developers/llms.txt
> Use this file to discover all available pages before exploring further.

# Create and edit documents

> Generate additional outputs from a session and preserve clinician edits with document versions.

Create additional outputs from a consultation, such as referrals and patient
instructions. Each document has its own content and version.

**Scopes:** `scribe:write` to create, edit, or delete; `scribe:generate` to generate
content; `scribe:read` to retrieve results.

## Create an empty document

`POST /sessions/{session_id}/documents`

```bash theme={null}
curl --request POST "$CORTEXA_API_BASE/sessions/$CORTEXA_SESSION_ID/documents" \
  --header "Authorization: Bearer $CORTEXA_JWT" \
  --header 'Content-Type: application/json' \
  --header 'Idempotency-Key: visit-001-letter-create' \
  --data "{\"title\":\"Referral letter\",\"template_id\":\"$CORTEXA_TEMPLATE_ID\"}"
```

Choose a template from [document templates](/developers/templates.md). `title` defaults to
`Document`; omitting `template_id` uses Auto SOAP. The `201` response contains the
document's `id`, `current_version: 1`, empty `rendered_text`, and empty `content`.

The new document is empty even if its status is `ready`. Generate content next.

## Generate or regenerate content

Save the returned ID as `CORTEXA_DOCUMENT_ID`, then request content:

```bash theme={null}
curl --no-buffer --request POST "$CORTEXA_API_BASE/sessions/$CORTEXA_SESSION_ID/documents/$CORTEXA_DOCUMENT_ID/content" \
  --header "Authorization: Bearer $CORTEXA_JWT" \
  --header 'Content-Type: application/json' \
  --header 'Idempotency-Key: visit-001-letter-generate-1' \
  --data '{"output_language_code":"en"}'
```

You can send `{}` to use the applicable language setting. The default response
is `200 text/event-stream`: append `output.delta` text for this document, then
replace it with the saved document from `output.ready`. See [Streaming](/developers/streaming.md)
for event handling and reconnecting without duplicate work.

For background generation, add `?stream=false`. The `202` JSON receipt contains
`job_id`, `status`, and `document_id`; [poll the job](/developers/jobs.md), then fetch the document.
Use a new key when the clinician intentionally requests fresh content after a
change to the session.

## Read a document or list the session's documents

```bash theme={null}
curl "$CORTEXA_API_BASE/sessions/$CORTEXA_SESSION_ID/documents/$CORTEXA_DOCUMENT_ID" \
  --header "Authorization: Bearer $CORTEXA_JWT"
```

| Response field | Description |
| - | - |
| `id`, `session_id` | Document and parent consultation |
| `status` | Current output state |
| `title` | Display title |
| `rendered_text` | Document text for clinician review and EHR write-back |
| `content` | Structured content object; the shape depends on the template |
| `current_version` | Revision to use as `base_version` when editing |
| `generation_job_id` | Associated generation job, when present |
| `failure_code` | Processing failure, when present |

`GET /sessions/{session_id}/documents?page=1&per_page=20` lists documents using
the standard paginated response.

## Save an edit

Read the document first, retain its `current_version`, and send that value as
`base_version`. The example assumes the version you just read was 1:

```bash theme={null}
curl --request PATCH "$CORTEXA_API_BASE/sessions/$CORTEXA_SESSION_ID/documents/$CORTEXA_DOCUMENT_ID" \
  --header "Authorization: Bearer $CORTEXA_JWT" \
  --header 'Content-Type: application/json' \
  --data '{"base_version":1,"title":"Reviewed referral","rendered_text":"Clinician-reviewed synthetic referral text."}'
```

`base_version` is required. `title`, `rendered_text`, and `content` are optional
editable fields. Retain the new `current_version` returned after saving.

A stale `base_version` returns `409`. Retrieve the latest document and resolve
the edits before saving again.

## Delete a document

`DELETE /sessions/{session_id}/documents/{document_id}` returns
`{"deleted": true}` and makes that document unavailable to the partner.
Deleting one document does not delete its session.

To save a reviewed note in your EHR, use your EHR API or an
[approved connector](/developers/connectors.md). Use [note sections](/developers/sectional-notes.md) to map
consult-note headings to EHR fields. PDF and DOCX downloads are not exposed by
these document endpoints.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.