1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390---
title: API Reference
date: 2026-03-23
author: Equalify Tech Team
description: Complete reference for the Equalify Reflow REST API โ document submission, job tracking, SSE streaming, and review workflows.
---
# API Reference
The Equalify Reflow API accepts PDF documents, processes them through the conversion pipeline, and returns accessible markdown with extracted images. All endpoints are prefixed with `/api/v1/`.
Interactive documentation is available at `/docs` on any running instance (requires HTTP Basic auth in production).
## Authentication
All API requests require an API key passed in the `X-API-Key` header:
```bash
curl -H "X-API-Key: YOUR_API_KEY" https://your-instance/api/v1/documents/submit
```
For browser-based SSE connections (where custom headers aren't possible), use a **stream token** instead โ see [Streaming Events](#streaming-events).
## Document Processing
### Submit a Document
```
POST /api/v1/documents/submit
```
Upload a PDF for processing. The document is scanned for PII, then queued for the conversion pipeline.
**Request** (multipart/form-data):
| Field | Type | Default | Description |
|-------|------|---------|-------------|
| `file` | file | required | PDF file to process |
| `skip_pii_scan` | boolean | `false` | Bypass PII scanning (requires `skip_reason`) |
| `skip_reason` | string | โ | Justification for skipping PII scan (recorded in audit trail) |
| `review_mode` | string | `"auto"` | `"auto"` completes immediately; `"human"` holds for ledger review |
| `generate_debug_bundle` | boolean | `false` | Save all agent prompts and responses for debugging |
**Response** (`201 Created`):
```json
{
"job_id": "abc123-def456",
"status": "pii_scanning",
"message": "Document submitted successfully",
"estimated_completion_minutes": 5
}
```
**Example โ submit with curl:**
```bash
curl -X POST https://your-instance/api/v1/documents/submit \
-H "X-API-Key: YOUR_API_KEY" \
-F "file=@syllabus.pdf" \
-F "review_mode=human"
```
**Example โ submit from WordPress (PHP):**
```php
$response = wp_remote_post($api_url . '/api/v1/documents/submit', [
'headers' => ['X-API-Key' => $api_key],
'body' => ['file' => new CURLFile($pdf_path, 'application/pdf')],
]);
$job_id = json_decode(wp_remote_retrieve_body($response))->job_id;
```
### Get Job Status
```
GET /api/v1/documents/{job_id}
```
Returns the current state of a processing job. The response shape changes based on the job's status.
**Possible statuses:**
| Status | Description |
|--------|-------------|
| `pii_scanning` | Document is being scanned for PII |
| `awaiting_approval` | PII detected โ waiting for human review |
| `processing` | Document is being converted |
| `completed` | Processing finished successfully |
| `failed` | Processing encountered an error |
| `denied` | PII review was denied |
**Completed response example:**
```json
{
"job_id": "abc123-def456",
"status": "completed",
"review_mode": "auto",
"markdown_url": "https://s3.../result.md",
"figures": [
{
"figure_id": "figure_1_1",
"url": "https://s3.../figure_1_1.png",
"page": 1,
"alt_text": "",
"caption": "Figure 1: Fall 2025 Enrollment"
}
],
"llm_cost": {
"input_tokens": 45000,
"output_tokens": 12000,
"total_tokens": 57000,
"estimated_cost_dollars": 0.34
},
"warnings": []
}
```
The `markdown_url` and figure `url` fields are pre-signed S3 URLs valid for a limited time. Download them promptly after job completion.
**Example โ poll until complete (JavaScript):**
```javascript
async function waitForCompletion(jobId, apiKey) {
while (true) {
const res = await fetch(`/api/v1/documents/${jobId}`, {
headers: { 'X-API-Key': apiKey }
});
const data = await res.json();
if (data.status === 'completed') return data;
if (data.status === 'failed') throw new Error(data.error);
await new Promise(r => setTimeout(r, 5000)); // poll every 5s
}
}
```
### Get Change Ledger
```
GET /api/v1/documents/{job_id}/ledger
```
Returns the complete change ledger โ every edit the pipeline made, grouped by page. Only available after processing completes.
**Response example:**
```json
{
"job_id": "abc123-def456",
"document_title": "BIOS 343 Syllabus",
"total_pages": 13,
"pages_with_changes": 11,
"total_edits": 68,
"pages": [
{
"page": 1,
"edit_count": 8,
"entries": [
{
"entry_id": "e1",
"action": "modify",
"target": "heading",
"before": "## BIOS 343",
"after": "# BIOS 343: Animal Physiology",
"reasoning": "Promoted to H1 โ this is the document title based on font size and position"
}
]
}
],
"final_markdown_url": "https://s3.../result.md"
}
```
## Streaming Events
For real-time progress tracking, connect to the SSE (Server-Sent Events) stream. This is how both the built-in viewer and the WordPress plugin display live progress.
### Get a Stream Token
```
POST /api/v1/documents/{job_id}/stream/token
```
Browser `EventSource` cannot send custom headers. This endpoint generates a single-use, short-lived token that authenticates the SSE connection via query parameter.
**Response:**
```json
{
"token": "st_abc123...",
"expires_in_seconds": 300,
"stream_url": "/api/v1/documents/abc123-def456/stream?token=st_abc123..."
}
```
### Connect to the Stream
```
GET /api/v1/documents/{job_id}/stream?token={stream_token}
```
Opens an SSE connection. The server sends events as the pipeline progresses.
**Event types:**
| Event | Data | Description |
|-------|------|-------------|
| `pipeline:phase` | `{display_name, step_number, total_steps}` | A pipeline stage started |
| `processing:complete` | `{}` | Processing finished successfully |
| `processing:error` | `{error}` | Processing failed |
| `done` | `{}` | Stream is closing |
**Example โ connect from JavaScript:**
```javascript
// 1. Get stream token (requires API key)
const tokenRes = await fetch(`/api/v1/documents/${jobId}/stream/token`, {
method: 'POST',
headers: { 'X-API-Key': apiKey }
});
const { token, stream_url } = await tokenRes.json();
// 2. Connect to SSE stream (token in URL, no headers needed)
const source = new EventSource(stream_url);
source.addEventListener('pipeline:phase', (e) => {
const { display_name, step_number, total_steps } = JSON.parse(e.data);
console.log(`Step ${step_number}/${total_steps}: ${display_name}`);
});
source.addEventListener('processing:complete', () => {
source.close();
// Fetch final results via GET /documents/{job_id}
});
source.addEventListener('processing:error', (e) => {
const { error } = JSON.parse(e.data);
console.error('Processing failed:', error);
source.close();
});
```
**Example โ connect from WordPress (PHP + JavaScript):**
```javascript
// In admin-media.js โ after submitting the PDF:
// Get token through WordPress REST proxy (avoids exposing API key to browser)
const tokenRes = await fetch(`/wp-json/equalify-reflow/v1/stream-token/${attachmentId}`, {
headers: { 'X-WP-Nonce': wpNonce }
});
const { token, stream_url, job_id } = await tokenRes.json();
// Connect using the client-facing API URL
const source = new EventSource(`${clientApiUrl}${stream_url}`);
source.addEventListener('pipeline:phase', (e) => {
const data = JSON.parse(e.data);
updateProgressBar(data.step_number, data.total_steps, data.display_name);
});
source.addEventListener('processing:complete', () => {
source.close();
completeProcessing(attachmentId); // triggers result download
});
```
The WordPress plugin proxies the stream token through its own REST endpoint so the browser never sees the API key. The SSE connection itself uses the single-use token.
## PII Approval
When PII is detected, the job enters `awaiting_approval` status. The status response includes an `approval_token` and `approval_url`.
### Get Review Details
```
GET /api/v1/approval/{token}/review
```
Returns job details and PII findings for the review interface.
**Response:**
```json
{
"job_id": "abc123",
"status": "awaiting_approval",
"pii_findings": [
{"entity_type": "EMAIL_ADDRESS", "text": "john@example.com", "score": 0.95},
{"entity_type": "PHONE_NUMBER", "text": "312-555-0100", "score": 0.88}
],
"created_at": "2026-03-23T10:00:00Z",
"expires_at": "2026-03-23T11:00:00Z"
}
```
### Submit Approval Decision
```
POST /api/v1/approval/{token}/decision
```
**Request:**
```json
{
"decision": "approved",
"justification": "Course material, no student PII โ instructor contact info is public",
"reviewed_by": "admin@uic.edu"
}
```
If approved, the document is queued for processing. If denied, the job moves to `denied` status.
## Pipeline Viewer
These endpoints power the built-in pipeline viewer at `/viewer` โ a visualizer for the Equalify Reflow AI pipeline. The viewer displays real-time progress, versioned markdown diffs, and the change ledger as a document moves through each stage. It is not intended for bulk document processing.
### Process with Streaming
```
POST /api/v1/pipeline/process/stream
```
Upload a PDF and receive an SSE stream of the full pipeline execution with versioned markdown snapshots at each stage. Unlike the document submission flow, this is synchronous โ you upload and stream in one connection.
**Request** (multipart/form-data):
| Field | Type | Default | Description |
|-------|------|---------|-------------|
| `file` | file | required | PDF to process |
| `images_scale` | float | `2.0` | Page image DPI scale (1.0โ3.0) |
| `do_table_structure` | boolean | `true` | Enable table structure extraction |
| `ocr_languages` | string | โ | OCR language codes (e.g., `"en,es"`) |
**SSE event types:**
| Event | Description |
|-------|-------------|
| `session` | Session ID for reconnection |
| `init` | Docling extraction complete โ metadata + initial markdown |
| `page_image` | Individual page image (base64 PNG) |
| `figure_image` | Individual extracted figure (base64 PNG) |
| `processing` | A pipeline step is starting |
| `step` | A pipeline step completed โ includes version diff |
| `error` | A step failed (non-fatal, pipeline continues) |
| `done` | Processing complete |
### Reconnect to Session
```
GET /api/v1/pipeline/sessions/{session_id}/stream?last_event_id={id}
```
If disconnected, reconnect to a running session and replay all events after the given ID. The pipeline continues running whether or not a client is connected.
## Health Checks
```
GET /health # Full health check (Redis, S3, queues)
GET /health/ready # Readiness probe for orchestration
```
## Rate Limiting
The API enforces per-IP rate limits on document submission. Default limits can be configured via environment variables. When rate-limited, the API returns `429 Too Many Requests` with a `Retry-After` header.
## Error Responses
All errors follow a consistent format:
```json
{
"detail": "Job not found"
}
```
| Status | Meaning |
|--------|---------|
| `400` | Bad request (invalid parameters, job not ready) |
| `401` | Missing or invalid API key |
| `404` | Job or resource not found |
| `413` | File too large (max 50 MB) |
| `422` | Validation error (details in response body) |
| `429` | Rate limited |
| `500` | Internal server error |