Real-time progress with SSE
Receive the progress of file translation jobs (/jobs) in real time through SSE (Server-Sent Events). When SSE is not available, the connection returns 404; poll the job status instead.
Connection flow
Section titled “Connection flow”The browser’s EventSource cannot send custom headers, so credentials take two steps:
- Your server exchanges the API key for a ticket (valid for 30 seconds, usable once)
- Connect to SSE with the ticket (this can happen in the browser; the ticket is not a long-lived credential)
POST /api/v1/sse/ticket/{job_id} → {"ticket": "..."}GET /api/v1/sse/jobs/{job_id}?ticket=...JavaScript example
Section titled “JavaScript example”Keep the API key on the server: the server gets the ticket and hands it to the browser.
// Server (Node.js 18+): exchange the key for a ticketasync function createSseTicket(jobId) { const response = await fetch( `https://omnidoc.top/api/v1/sse/ticket/${jobId}`, { method: 'POST', headers: { 'X-API-Key': process.env.OMNIDOC_API_KEY } } ); if (!response.ok) throw new Error(`ticket failed: ${response.status}`); return (await response.json()).ticket;}// Browser: connect to SSE with the ticket from your serverconst source = new EventSource( `https://omnidoc.top/api/v1/sse/jobs/${jobId}?ticket=${encodeURIComponent(ticket)}`);
// The events are named, so use addEventListener (onmessage does not receive them)source.addEventListener('job_update', (event) => { const data = JSON.parse(event.data); console.log(`status: ${data.status}, stage: ${data.stage}, progress: ${data.stage_progress}%`);});
source.addEventListener('job_done', (event) => { const data = JSON.parse(event.data); console.log(`finished: ${data.status}`); source.close();});Event types
Section titled “Event types”| Event | Description |
|---|---|
job_update | Status or stage changed (a snapshot of the current state is sent right after connecting) |
job_done | The job ended (completed or failed); you can close the connection |
timeout | The stream reached its maximum duration; reconnect |
error | Server-side problem; get a new ticket and reconnect later |
Event data
Section titled “Event data”The server’s stage_message is in Chinese:
event: job_updatedata: {"status":"processing","stage":"translating","stage_progress":45,"stage_message":"正在翻译..."}
event: job_donedata: {"status":"completed","stage":"completed","stage_progress":100,"stage_message":""}When the job completes, get the result list with GET /api/v1/artifacts?job_id=....
Python example
Section titled “Python example”pip install requests sseclient-pyimport json
import requestsimport sseclient # sseclient-py
BASE = "https://omnidoc.top/api/v1"HEADERS = {"X-API-Key": "sk_your_key"}job_id = "q3f8s6c2a9m1k4tx"
ticket_response = requests.post( f"{BASE}/sse/ticket/{job_id}", headers=HEADERS, timeout=20)ticket_response.raise_for_status()ticket = ticket_response.json()["ticket"]
# Read timeout longer than the 15-second heartbeat; no API key while connectedresponse = requests.get( f"{BASE}/sse/jobs/{job_id}", params={"ticket": ticket}, stream=True, timeout=(10, 60),)response.raise_for_status()client = sseclient.SSEClient(response)
for event in client.events(): data = json.loads(event.data) print(f"[{event.event}] {data.get('stage')} {data.get('stage_progress', 0)}%") if event.event in {"job_done", "timeout", "error"}: break # timeout / error: get a new ticket and reconnect, or poll instead- A ticket works once; get a new one before reconnecting
- Each account can keep at most 6 SSE connections open at a time and exchange at most 30 tickets a minute; beyond that the response is
429, so wait forRetry-Afteror poll instead - When there are no job events for a long time, the server sends a
timeoutevent; get a new ticket and reconnect - While the connection is idle the server sends heartbeat comments; clients should ignore comment lines
- If the connection drops, polling
GET /jobs/{id}works as a fallback