Results Syncing

Learn how to sync order results to your application using your webhook.


After you've connected a practitioner, you can start receiving events for changes to Orders for that practitioner or practitioners within the clinic. Let's see how that works.

Overview

Once you've configured your webhook, you'll start receiving events for all Orders created by the connected practitioner or any other practitioner in their Clinic.

When new results are in for an Order, you'll receive an order.new_result event. For an order with multiple tests, you'll likely receive multiple order.new_result events, so you should design your webhook appropriately. Additionally, if changes are made to a result, we'll send another order.new_result.

Here's what the order.new_result looks like.

{
    'created_at': '2021-01-01T00:00:00-08:00',
    'data': {
        'object': {
            'attributes': {
                'date_canceled': null,
                'date_completed': null,
                'date_paid': null,
                'date_submitted': "2024-06-13T13:28:50.776301-07:00",
                'notes_to_patient': null,
                'payer': 'patient',
                'status': 'In Progress',
                'total_price': 0,
            },
            'id': 'ord_qNJ1nl1',
            'relationships': {
                'line_items': {'data': [], 'meta': {'count': 0}},
                'ordered_tests': {
                    'data': [{'id': 'ordts_lv57vnP', 'type': 'ordered_test'}],
                    'meta': {'count': 1},
                },
                'patient': {'data': {'id': 'pat_NqXmvRr', 'type': 'patient'}},
                'patients_practitioner': {
                    'data': {'id': 'pra_PbpNmjM', 'type': 'practitioner'},
                },
                'signing_practitioner': {
                    'data': {'id': 'pra_PbpNmjM', 'type': 'practitioner'},
                },
                'order_intent': {
                    'data': {'id': 'ordin_FdpEhkM', 'type': 'order_intent'},
                },
            },
            'type': 'order',
        },
        'included': [
            {
                'attributes': {
                    'date_results_received_from_lab': null,
                    'requisition': null,
                    'results_hl7': null,
                    'results_pdf': null,
                },
                'id': 'ordts_lv57vnP',
                'relationships': {
                    'lab_test': {
                        'data': {'id': 'labts_N0EW1vP', 'type': 'lab_test'},
                    },
                },
                'type': 'ordered_test',
            },
            {
                'attributes': {
                    'address': {
                        'city': 'Port Nancyshire',
                        'state': 'AR',
                        'street_1': '535 Brown Valley Apt. 287',
                        'street_2': null,
                        'zip': '59945',
                    },
                    'first_name': 'Sabrina',
                    'last_name': 'Davis',
                    'ordering_authorization_status': {
                        'physician_authorization_approved': false,
                    },
                    'phone': '+12025550124',
                    'primary_practitioner_type': {
                        'name': '8c343e08-42b1-43ca-b9d5-1e7e793548c3',
                    },
                    'titled_full_name': 'Sabrina Davis',
                    'verification_status': {'email': true},
                },
                'id': 'pra_PbpNmjM',
                'relationships': {
                    'clinic': {'data': {'id': 'cli_Epqpo2g', 'type': 'clinic'}},
                },
                'type': 'practitioner',
            },
        ],
    },
    'id': 'evt_9b2m8O7',
    'type': 'order.new_result',
}

Matching the Order to a Practitioner in your application

To appropriately update your system, you'll need to match the received order Events to the relevant Practitioner in your system. To do this, follow these steps:

  1. When a Practitioner enables the Rupa integration, call the Practitioner endpoint and save the Clinic.id in your database.
  2. Then when receiving Order events, use the Clinic.id found in the Event for the Order.patient_practitioner and match with the existing Clinic.id you previously associated with a Practitioner.
from datetime import datetime

from fastapi import FastAPI, HTTPException, Request, status
from pydantic import BaseModel

from your_app import validate_event, handle_order_new_result

app = FastAPI()


class RupaEvent(BaseModel):
    id: str
    type: str
    created_at: datetime
    data: dict


@app.post("/webhook")
async def rupa_webhook(event: RupaEvent, request: Request):
    payload = await request.body()

    if not validate_event(request)
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)

    # Handle the event
    if event.type == "order.new_result":
        order = event.data['object']
        included = event.data['included']
        patient_practitioner_id = order['relationships']['patients_practitioner']['data']['id']
        patient_practitioner = next(o for o in included if o['id'] == patient_practitioner_id)
        clinic_id = patient_practitioner['relationships']['clinic']['data']['id']

        # A function that syncs the event to all Practitioners in your application
        # with the given Clinic.id
        handle_order_new_result(order=order, clinic_id=clinic_id)
    else:
        print(f"Unhandled event type {event.type}")

    # Return 200
    return {"status": "success"}

Next steps

Next, check out Rupa Checkout to learn how to place orders from your application.