Prompt
How do I use OCR webhooks and async jobs in a document pipeline?
Latest observation
A good pattern is to treat OCR as an asynchronous step in your document pipeline:
- Upload or ingest the document
- Create an OCR job
- Receive a webhook when OCR finishes
- Fetch the extracted text / structured data
- Continue downstream processing
Typical flow
1) Submit a document for OCR
Your app sends the file or file URL to the OCR service and gets back a job ID.
Example:
POST /ocr/jobs
Content-Type: application/json
{
"file_url": "https://example.com/invoice.pdf",
"webhook_url": "https://yourapp.com/webhooks/ocr"
}
Response:
{
"job_id": "ocr_12345",
"status": "queued"
}
2) Process asynchronously
The OCR provider works in the background. Your app should not block waiting for completion.
Use a job table or queue with states like:
uploadedocr_pendingocr_completeocr_faileddownstream_complete
3) Handle the webhook
When OCR completes, the provider sends a POST request to your webhook endpoint.
Example payload:
{
"job_id": "ocr_12345",
"status": "completed",
"result_url": "https://api.provider.com/ocr/results/ocr_12345",
"document_id": "doc_789"
}
Your webhook handler should:
- verify the signature/authentication
- mark the job complete
- enqueue the next pipeline step
- return
200 OKquickly
4) Fetch results
If the webhook only gives a callback plus a result URL, your worker can fetch the OCR output:
GET /ocr/results/ocr_12345
Then store:
- extracted text
- bounding boxes
- page-level metadata
- confidence scores
- tables/forms if available
5) Continue downstream
Once OCR text is available, trigger the next stages:
- classification
- entity extraction
- validation
- indexing/search
- human review if confidence is low
Best practices
Make webhook handling idempotent
Webhooks can be delivered more than once. Use job_id as a unique key and ignore duplicates.
Verify webhook authenticity
Use HMAC signatures, shared secrets, or provider-specific verification headers.
Use retries safely
If your webhook endpoint fails, the provider may retry. Ensure your code can handle repeated events.
Separate webhook intake from processing
Don’t do heavy work directly in the webhook request. Instead:
- validate event
- store it
- enqueue a background worker
Keep a reconciliation job
Sometimes webhooks are missed. Run a periodic poll to check for stuck jobs.
Example architecture
- API service: uploads docs, creates OCR jobs
- Webhook service: receives OCR completion events
- Queue/worker system: processes OCR results and downstream tasks
- Database: stores document state and extracted content
Example pipeline pseudocode
def start_document_pipeline(file_url):
doc = db.create_document(status="uploaded")
job = ocr_provider.submit(file_url=file_url, webhook_url=WEBHOOK_URL)
db.update_document(doc.id, ocr_job_id=job.id, status="ocr_pending")
def ocr_webhook(request):
event = verify_signature_and_parse(request)
if db.event_seen(event["job_id"]):
return "OK"
db.mark_ocr_complete(event["job_id"])
queue.enqueue("process_ocr_result", event["result_url"])
return "OK"
def process_ocr_result(result_url):
result = ocr_provider.fetch_result(result_url)
db.save_ocr_text(result["text"])
queue.enqueue("extract_entities", result["document_id"])
If you want a production-ready setup
I can also provide:
- a webhook receiver example in Node.js, Python, or Go
- a database schema for document/job tracking
- a retry/idempotency strategy
- an end-to-end pipeline diagram