Skip to content
Guides

FHIR Bulk NDJSON to HL7v2 over MLLP

Receive a FHIR Bulk Data file on a webhook, split it into resources and send each one as an HL7v2 message over MLLP.

On this page

This guide builds a workflow that accepts a FHIR Bulk Data file (NDJSON, one resource per line) on a webhook and sends each Patient, Encounter and Appointment to a receiving system as its own HL7v2 message. It uses the built-in FHIR Bulk webhook → HL7v2 over MLLP template: a fhir-bulk-split transform, a loop over the items, a fhir-to-hl7v2 transform and an MLLP send.

Here is the flow.

sequenceDiagram
    participant Sender as Bulk export (test)
    participant Engine as MedHook engine
    participant Rcv as Receiving system (MLLP)

    Sender->>Engine: POST /api/triggers/webhook/fhir-bulk (NDJSON, signed)
    Engine-->>Sender: 200 {status: accepted, executionIds}
    Engine->>Engine: Split the file into one item per resource
    loop Each resource
        Engine->>Rcv: HL7v2 message over MLLP
        Rcv-->>Engine: ACK (AA)
    end
FHIR resourceHL7v2 message
PatientADT^A08
EncounterADT^A01 while in progress, ADT^A03 when finished
AppointmentSIU^S12 when booked, S14 for a later version, S15 when cancelled

Other resource types in the file are counted, not sent.

Before you start

  • A running engine and a signed-in user. See the Quick start.
  • A receiving system that listens for HL7v2 over MLLP. Its host and port must be reachable from the engine.
  • A shared secret for the sender to sign requests with, for example whsec_test_example.

Steps

1. Create an MLLP Outbound profile

Open Adapters, choose New connection and pick MLLP. Fill in the following; Host and port takes mllp.example.test:2575. Settings that aren't in the short form are under More settings.

FieldValue
NameTest receiver
hostmllp.example.test
port2575
timeout30000, the connect and ACK timeout in ms, unless connectTimeoutMs or ackTimeoutMs is set

The profile can also send over TLS (tlsEnabled, with a pinned tlsFingerprint256 or a tlsCaPem, and a client certificate for mutual TLS), skip the ACK wait (expectAck: false), and override MSH-3 and MSH-4 (sendingApplication, sendingFacility). A send succeeds only on an AA whose MSA-2 matches the message's MSH-10. See Outbound sends.

Choose Test connection, then Save connection.

2. Start from the template

Open Workflows, choose Templates, choose FHIR Bulk webhook → HL7v2 over MLLP, then Use this template in its preview. The workflow starts Disabled.

A loop sends one message per resource, which a channel cannot express. So the builder opens this workflow read-only and says why under This workflow opens read-only. Switch to Graph to see the steps:

  1. Webhook (FHIR Bulk NDJSON or Bundle), the trigger.
  2. One item per resource, the fhir-bulk-split transform. It keeps Patient, Encounter and Appointment.
  3. Each resource, a loop over $.items with up to 1000 iterations. It records a failed resource and carries on with the rest. A loop's second connection, labelled done, runs once after the last item. Add a step there for a summary, such as a notification with the loop's counts.
  4. FHIR → HL7v2, the fhir-to-hl7v2 transform.
  5. Send over MLLP, an MLLP Outbound step that retries 3 times.

The Graph view of the FHIR Bulk workflow: the webhook trigger, One item per resource, the Each resource loop, FHIR → HL7v2 and Send over MLLP, one after another

3. Set the profile, secret and message header

Because the builder is read-only here, change the workflow with the API. Get a token as in Call the engine API. Then fetch the workflow, set the profile, the webhook secret and the HL7v2 header fields, and PUT it back:

bash
curl -s "$ENGINE_URL/api/workflows/$WF_ID" -H "Authorization: Bearer $TOKEN" \
| jq --arg profile "$MLLP_PROFILE_ID" --arg secret "$WEBHOOK_SECRET" '
    .workflow
    | (.nodes[] | select(.id == "send-mllp") | .data.adapterProfileId) = $profile
    | .trigger.secret = $secret
    | (.nodes[] | select(.id == "to-hl7v2") | .data.config) += {
        sendingFacility: "TESTFAC",
        receivingApplication: "TESTRCV",
        receivingFacility: "TESTDEST"
      }' \
| curl -s -X PUT "$ENGINE_URL/api/workflows/$WF_ID" \
    -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" -d @-

See Get workflow and Update workflow. The HMAC secret can only be set through the API, in this and every other webhook workflow. The builder shows a webhook trigger's Receives at URL, its Required headers (name: value, one per line) and its Mirth export path, but no secret field. The engine never returns the secret again. A later save that leaves trigger.secret out, or sends back the __set__ marker, keeps it.

To change which message a resource becomes, set messageTypes on the FHIR → HL7v2 step. Keys are "<type>", "<type>:<status>" or "<type>:<status>:updated". For example, {"Encounter:planned": "ADT^A05"}.

4. Test it

In the builder, choose Replace on the sample panel, paste this synthetic Bundle and choose Save sample. A JSON sample is accepted for a webhook trigger.

json
{
  "resourceType": "Bundle",
  "type": "collection",
  "entry": [
    {
      "resource": {
        "resourceType": "Patient",
        "id": "p1",
        "identifier": [
          {
            "type": {
              "coding": [
                {
                  "code": "MR"
                }
              ]
            },
            "system": "https://ehr.example.test/mrn",
            "value": "MRN0001"
          }
        ],
        "name": [{ "family": "TESTPATIENT", "given": ["SYNTHETIC"] }],
        "gender": "female",
        "birthDate": "1980-01-01"
      }
    },
    {
      "resource": {
        "resourceType": "Encounter",
        "id": "e1",
        "status": "in-progress",
        "class": {
          "code": "IMP"
        },
        "subject": { "reference": "Patient/p1" },
        "period": { "start": "2026-01-01T12:00:00Z" }
      }
    },
    {
      "resource": {
        "resourceType": "Observation",
        "id": "o1",
        "status": "final",
        "code": { "text": "Example" }
      }
    }
  ]
}

Choose Run test. Nothing is sent. The run records:

  • the split step with count of 2 and ignored of { "Observation": 1 };
  • two passes through the loop, the first an ADT^A08 for the Patient and the second an ADT^A01 for the Encounter, which carries the Patient's name and MRN in PID because the Patient is in the same file;
  • two MLLP sends that report what they would send.

5. Deploy and send a file

To deploy, in Workflows, turn on the workflow's switch and choose Go to Deploy, then choose Deploy v1 to all messages on the Versions tab (see Save and deploy). Deploy needs a passing test newer than the last save, passing saved test cases (if the workflow has any) and an approval. Then send the file as NDJSON, one resource per line, signed with the secret:

bash
cat > bulk.ndjson <<'NDJSON'
{"resourceType":"Patient","id":"p1","identifier":[{"type":{"coding":[{"code":"MR"}]},"system":"https://ehr.example.test/mrn","value":"MRN0001"}],"name":[{"family":"TESTPATIENT","given":["SYNTHETIC"]}],"gender":"female","birthDate":"1980-01-01"}
{"resourceType":"Encounter","id":"e1","status":"in-progress","class":{"code":"IMP"},"subject":{"reference":"Patient/p1"},"period":{"start":"2026-01-01T12:00:00Z"}}
NDJSON

SIG=$(openssl dgst -sha256 -hmac "$WEBHOOK_SECRET" -hex < bulk.ndjson | sed 's/^.* //')

curl -s -X POST "$ENGINE_URL/api/triggers/webhook/fhir-bulk" \
  -H "Content-Type: application/fhir+ndjson" \
  -H "x-webhook-signature: $SIG" \
  --data-binary @bulk.ndjson

The signature is the hex HMAC-SHA256 of the raw body. The reply is:

json
{
  "received": true,
  "matched": true,
  "status": "accepted",
  "executionIds": ["..."]
}

The engine records the delivery, answers, and then runs the workflow. So poll Get execution with the id: it is pending, then running, and ends completed when every resource was delivered. The receiving system gets two messages, and each answers with its own ACK.

6. Choose what the sender gets back

The default answer above is the trigger's response.mode accepted. Two other modes exist. In the builder they are under Answer the sender with, and here you set them with the same jq and PUT as in step 3:

  • custom answers with your status (2xx, not 204 or 205) and a body of up to 8 KiB. The body can use ${executionId}, ${executionIds}, ${webhookId} and ${duplicate}, never message fields: .trigger.response = {mode: "custom", statusCode: 202, body: "{\"id\": \"${executionId}\"}"}.
  • destination waits for the run and answers with one step's output, chosen under Answer with the output of, waiting up to Wait up to (seconds, 1-60). Past the wait the sender gets 202 and the run goes on. A failed run answers 502.

A sender that resends with the same Idempotency-Key gets the first answer, and nothing runs twice. The same key with a different body is 422 IDEMPOTENCY_KEY_REUSED. In destination mode, make the load balancer's idle timeout longer than the wait. See Triggers: answering the sender.

🎉 A bulk file now turns into one HL7v2 message per resource. To send ordinary HL7v2 messages the other way, see HL7v2 over MLLP to FHIR R4.

Troubleshooting

  • The webhook answers 403 INVALID_SIGNATURE. The signature is missing or wrong. Sign the exact bytes you send, and use --data-binary so curl does not change them.
  • The webhook answers 401 SIGNATURE_REQUIRED. The trigger has no secret and the caller has no engine token. Set the secret as in step 3, or send Authorization: Bearer <token> from an API client for this workspace (see Call the engine API).
  • The webhook answers 409 NOT_RUN. A workflow matched but is not deployed or enabled. Deploy it.
  • The webhook answers 503. The engine could not record the delivery, for example during a shutdown or with a full backlog. Nothing ran. Resend after the Retry-After seconds.
  • The webhook answers 400 with a line number. That line is not a FHIR resource. Fix it or remove it. The engine rejects the whole file.
  • The webhook answers 413. The body is over WEBHOOK_MAX_BODY_BYTES (10 MiB), or an NDJSON file has more than 1000 resources (WEBHOOK_MAX_NDJSON_RESOURCES). Split the export into smaller files.
  • The execution fails but most messages arrived. A resource with no mapping, a non-AA ACK, a failed connection or a Patient it cannot identify is recorded in the loop step's failures with its index, step and error. The rest are still sent. The execution then fails so your failed-execution alert fires. It is not retried on its own, because the other messages were delivered. Fix the cause and send the failed resources again.
  • A resource is skipped silently. Only Patient, Encounter and Appointment are sent by default. Other types appear under ignored and count as not sent. A type you add to resourceTypes on One item per resource fails in the loop unless the converter has a mapping for it.

Reference