Asynchronous Processing Overview

Asynchronous processing allows you to upload lab report documents and receive immediate responses while the AI analysis happens in the background. This approach provides better user experience for interactive applications and enables you to handle multiple requests efficiently.

How Asynchronous Processing Works

  1. Upload Document: send a POST request with wait_on_process: false (default)
  2. Immediate Response: receive a response with status: "processing", and a record_id
  3. Background Processing: AI models analyze the document (typically 10–60 seconds)
  4. Get Results: receive webhook notification with analysis report when finished

Getting Results

Webhooks

Configure a webhook URL to receive real-time notifications when analysis completes: Request:
{
  "mime_type": "application/pdf",
  "body": "base64-encoded-document-data",
  "filename": "lab_report_2024_01_15.pdf",
  "wait_on_process": false
}
Immediate Response:
{
  "lab_report": {
    "record_id": "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
    "status": "processing",
    "uploaded_at": "2024-01-15T10:30:04.521Z"
  }
}
Webhook Notification (when analysis completes):
{
  "application_user_id": "end_user_id1",
      "record_id": "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
      "status": "completed",
      "collection_date": "2024-01-15",
      "result_date": "2024-01-16",
      "sections": [
        {
          "loinc_code": "58410-2",
          "loinc_common_name": "CBC panel - Blood by Automated count",
          "original_section_name": "HEMATOLOGY",
          "results": [
            {
              "loinc_code": "6690-2",
              "loinc_common_name": "Leukocytes [#/volume] in Blood",
              "original_test_name": "White Blood Count",
              "original_unit_name": "K/uL",
              "standard_unit_name": "K/uL",
              "value": 7.2,
              "within_normal_range": true
            }
          ]
        }
      ],
      "uploaded_at": "2024-01-15T10:30:04.521Z",
      "modified_at": "2024-01-15T10:30:12.132Z"
}

Alternative: Manual Checking

If webhooks are not available in your environment, you can check the status using the GET /lab_reports/{lab_report_id} endpoint:
GET /lab_reports/6ba7b810-9dad-11d1-80b4-00c04fd430c8

Webhook Configuration

Setup

Configure your webhook URL through the admin console. Your endpoint must:
  • Respond with HTTP 200 to acknowledge receipt
  • Handle POST requests with JSON payloads
  • Verify HMAC signatures for security
If requests fail, the system retries up to 5 times with exponential backoff.

Security

All webhook requests include HMAC SHA256 signature verification:
  • Header: x-body-signature contains the hex-encoded HMAC signature
  • Key: uses your application’s webhook signature key (configured in admin console)
  • Algorithm: HMAC-SHA256(webhook_signature_key, request_body)
See Webhook Signature for more details.

Status Handling

Your webhook handler should handle different status values:
  • completed: Analysis successful - lab report data available
  • failed: Analysis failed - check parsing_error field for details
When status is failed, check the parsing_error field for specific details about what prevented successful analysis.

Implementation Examples

package main

import (
	"crypto/hmac"
	"crypto/sha256"
	"encoding/hex"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"time"
)

const webhookSignatureKey = "YOUR_WEBHOOK_SECRET_FROM_ADMIN_CONSOLE"

// LabReport represents the webhook payload structure
type LabReport struct {
	ApplicationUserID string    `json:"application_user_id"`
	RecordID          string    `json:"record_id"`
	Status            string    `json:"status"`
	ParsingError      string    `json:"parsing_error,omitempty"`
	CollectionDate    string    `json:"collection_date,omitempty"`
	UploadedAt        time.Time `json:"uploaded_at"`
	ModifiedAt        time.Time `json:"modified_at"`
	// Add other fields as needed
}

func main() {
	http.HandleFunc("/lab-reports-webhook", func(w http.ResponseWriter, r *http.Request) {
		// Verify HMAC signature
		signature := r.Header.Get("x-body-signature")
		if signature == "" {
			http.Error(w, "Missing signature", http.StatusBadRequest)
			return
		}

		// Read request body
		body, err := io.ReadAll(r.Body)
		if err != nil {
			http.Error(w, "Failed to read body", http.StatusInternalServerError)
			return
		}

		// Calculate and verify HMAC
		hm := hmac.New(sha256.New, []byte(webhookSignatureKey))
		hm.Write(body)
		expectedSignature := hex.EncodeToString(hm.Sum(nil))
		
		if signature != expectedSignature {
			http.Error(w, "Invalid signature", http.StatusUnauthorized)
			return
		}

		// Parse lab report
		var record LabReport
		if err := json.Unmarshal(body, &record); err != nil {
			http.Error(w, "Invalid JSON", http.StatusBadRequest)
			return
		}

		// Process the lab report
		fmt.Printf("Received lab report analysis: %s for user %s\n", record.Status, record.ApplicationUserID)
		if record.Status == "completed" {
			fmt.Printf("Lab report completed for record %s\n", record.RecordID)
			// Update your application with the results
		} else if record.Status == "failed" {
			fmt.Printf("Analysis failed: %s\n", record.ParsingError)
			// Handle failure case
		}

		// Respond with success
		w.WriteHeader(http.StatusOK)
		w.Write([]byte("OK"))
	})

	fmt.Println("Starting lab reports webhook server on port 8000")
	http.ListenAndServe(":8000", nil)
}
For complete API specifications and additional configuration options, see the Implementation Guide.