HL7v2 to FHIR, then to your own REST API
Convert HL7v2 messages from the MLLP listener to FHIR R4 with the built-in step, reshape the result into your API's JSON, and send it with an idempotent upsert through a Generic REST destination.
On this page
- Before you start
- Steps
- 1. Create the API's profile
- 2. Start from the template
- 3. Convert to FHIR
- 4. Reshape the Bundle into your API's JSON
- 5. Send it as an upsert
- 6. Test it
- 7. Choose when the sender gets its ACK
- 8. Save, deploy and send
- Receive HL7 over HTTP instead
- Retries and failures
- Troubleshooting
- Reference
Your destination is your own API, not a FHIR server. You still want FHIR's model in between: the built-in converter already reads PID, PV1, OBR and OBX, handles timezones and drops the SSN, so your code only picks the fields your API needs. This guide builds that channel: MLLP in, HL7 → FHIR, a JavaScript step that reshapes the Bundle into your API's JSON, and a Generic REST destination that upserts each patient by MRN.
To POST the Bundle to a FHIR server instead, see HL7v2 over MLLP to FHIR R4.
sequenceDiagram
participant Sender as Sending system (test)
participant Engine as MedHook engine
participant API as Your API
Sender->>Engine: ADT^A01 over MLLP
Engine-->>Sender: ACK (MSA|AA)
Engine->>Engine: HL7 → FHIR (Patient + Encounter)
Engine->>Engine: JavaScript: Bundle to your JSON
Engine->>API: PUT /patients/{mrn} (Idempotency-Key)
API-->>Engine: 200Before you start
- A running engine and a signed-in user. See the Quick start.
- Your API's base URL and auth, for example
https://api.example.test/v1with OAuth2 client credentials. This guide assumes it takesPUT /patients/{mrn}with this body, and creates the patient or replaces it:
{
"mrn": "TEST000123",
"familyName": "TESTPATIENT",
"givenName": "ALPHA",
"birthDate": "1980-01-01",
"sex": "female",
"phone": "555-0101",
"visit": {
"number": "V0001",
"class": "IMP",
"status": "in-progress",
"start": "2026-11-12T09:00:00-06:00"
}
}- The IANA timezone the sending system writes its times in, for example
America/Chicago. - The sending system can open a TCP connection to the engine's MLLP port, directly or through a site-to-site tunnel.
Steps
1. Create the API's profile
Open Adapters, choose New connection and pick Generic REST. Settings that aren't in the short form are under More settings.
| Field | Value |
|---|---|
| Name | Demo Health API |
| Base URL | https://api.example.test/v1 |
| Auth type | What your API takes: bearer, apikey, oauth2 (client credentials) or another of these |
With oauth2, the engine fetches the token, caches it until it expires, and fetches a new one once after a 401. See OAuth2.
New profiles sign each request by default, so your API can check that it came from your engine: the secret is shown once when you save. See Signed requests.
The engine refuses a base URL on a private address unless it is in SSRF_ALLOWLIST, or inside the remote subnets of a live tunnel in the same workspace. See SSRF protection.
2. Start from the template
Open Workflows, choose Templates and pick HL7v2 over MLLP → JSON webhook. Under Profiles it uses, choose Demo Health API, then Use this template. The builder opens with an MLLP listener source and one destination, POST JSON to webhook, whose output is Original message. See Start from the template.
Select the destination and set Destination name to Upsert patient.
3. Convert to FHIR
Under Output, choose + Add output step and set it to HL7 → FHIR. Enter the Sender timezone, for example America/Chicago.
A new HL7 → FHIR step fails a message that has a time with no UTC offset when no timezone is set, rather than guess. Set it before go-live. The step returns a FHIR R4 transaction Bundle: a Patient from PID, an Encounter from PV1, a ServiceRequest and Observations from OBR and OBX, and more. It drops the SSN by default. See HL7v2 to FHIR for every segment and option.
4. Reshape the Bundle into your API's JSON
Choose + Add output step again. It starts as JavaScript and runs on the Bundle, because output steps run in order, each on the one before. Replace its code with:
const resources = input.entry.map((e) => e.resource);
const patient = resources.find((r) => r.resourceType === "Patient");
const encounter = resources.find((r) => r.resourceType === "Encounter");
const mrn = patient?.identifier?.find(
(i) => i.type?.coding?.[0]?.code === "MR",
);
if (!mrn) throw new Error("PID-3 has no MR identifier");
const name = patient.name?.[0] ?? {};
const phone = patient.telecom?.find((t) => t.system === "phone");
return {
mrn: mrn.value,
familyName: name.family,
givenName: name.given?.[0],
birthDate: patient.birthDate,
sex: patient.gender,
phone: phone?.value ?? null,
visit: encounter
? {
number: encounter.identifier?.[0]?.value ?? null,
class: encounter.class?.code ?? null,
status: encounter.status,
start: encounter.period?.start ?? null,
}
: null,
};The converter types each Patient identifier: MR is the medical record number from PID-3. A message without one fails at this step and waits on Messages › Needs action, instead of reaching your API without a key.
For a flat field-to-field shape, a Data map → JSON step on the HL7 message is enough, and you can skip the FHIR step. Use FHIR in between when you want the converter's work: coded values, times with their offset, results linked to their order.
5. Send it as an upsert
Under Send, keep Adapter profile on Demo Health API · generic-rest and set:
| Field | Value |
|---|---|
| Action | put |
URL template (urlTemplate) | /patients/{{data.mrn}} |
| Body | empty: the step's input |
{{data.mrn}} is a field of the step's input, the JSON from step 4. The value is encoded as one path segment, so it can't change the path or the host. With no Body, the step sends its input as JSON. See URL templates.
The MRN is the upsert key. A resend, a retry or a replay of the same patient replaces the record your API already has, and never makes a second one. Each write also carries an Idempotency-Key header that stays the same across retries of the step for the same message, so an API that honors it can drop a repeat.
If your API has no PUT and answers a create of an existing record with 409, POST instead and set Answer statuses (answerStatuses) to 409. A listed status no longer fails the step: the output becomes { status, headers, body } for every response, so a condition (data.status === 409) or a later destination can send the update. A status you don't list still fails the step.
To branch on the status or read a Location header after a success, set Response to full. See The response.
6. Test it
Choose Load sample, then Paste a sample (or Replace), and paste this synthetic message:
MSH|^~\&|TESTSENDER|TESTFAC|MEDHOOK|DEMO|20261112090000||ADT^A01|MSG0001|P|2.5.1
EVN|A01|20261112090000
PID|1||TEST000123^^^TESTFAC&2.16.840.1.113883.19.5.99999&ISO^MR||TESTPATIENT^ALPHA||19800101|F|||1 Test St^^Testville^ST^00000||555-0101
PV1|1|I|WARD^101^1|||||||MED||||1|||||V0001^^^TESTFAC&2.16.840.1.113883.19.5.99999&ISO^VN|||||||||||||||||||||||||20261112090000Choose Save sample, then Run test. Nothing is delivered. The destination shows WOULD SEND and, under Would send, the JSON in Before you start: the visit time carries -06:00, the offset for the timezone you set.
7. Choose when the sender gets its ACK
Select the Source. ACK the sender decides what the ACK means:
| Option | The sender gets |
|---|---|
| accept (default) | AA once the engine has recorded the message. The conversion and the call to your API run after. A later failure waits on Needs action, not in the ACK. |
| destination | The outcome of your API call, waiting up to Wait for destinations (ms): AA when it succeeded, AR when your API refused the message for good, AE (resend) for any other failure or no answer in time. |
accept keeps the sender moving when your API is slow or down: messages queue in the engine and you replay any that fail. Choose destination when the sender must hold a message until your API has it. Each sender connection then waits on your API, one message at a time. See ACK from the destination.
Either way, a resend of a message the engine already has (same sending application, facility and MSH-10) is not run again. See Resends and MSH-10.
8. Save, deploy and send
Choose Save, then deploy from the Versions tab as in Save and deploy. Send the message from step 6 over MLLP as in Send a real message.
Open Messages and select the message. The Delivery tab shows the send as a timeline, such as Queued → Sent → 200, with the host and status. Operators and admins also see the request and response headers and bodies, with credentials replaced by [redacted]. See Messages.
🎉 HL7v2 from the listener now reaches your API in its own shape, one upsert per patient.
Receive HL7 over HTTP instead
A sender that posts HL7v2 over HTTPS can use a webhook trigger in place of the MLLP listener. Send the message as text/plain. A webhook run's input keeps the body in data, so put a JavaScript step first under Output that returns the message text:
return input.data;Then add HL7 → FHIR and the step from step 4 after it. HTTP has no HL7 ACK: the sender gets 200 with status: "accepted" once the message is recorded. To split HL7 batch files into one run per message, see HL7 batches.
Retries and failures
| Your API answers | What the step does |
|---|---|
| 2xx | Succeeds. |
| A status in Answer statuses | Succeeds, with the status in its output. |
| 4xx, other than 408, 425 and 429 | Fails, and is not retried: it would fail the same way again. Fix the cause and replay it from Needs action. |
| 5xx, 408, 425 or 429, or the connection is refused | Fails, and the step's On failure setting retries it. |
No answer within the profile's timeout | Fails as uncertain, and is not resent: your API may already have it. |
With On failure set to retry, a retry keeps the same Idempotency-Key. After the last retry the message waits on Messages › Needs action. See Executions and replay.
Troubleshooting
- The step fails with "sent with no UTC offset". Set Sender timezone on the HL7 → FHIR step, then replay the message.
- The JavaScript step fails with
PID-3 has no MR identifier. The message has no PID-3 repetition typedMR. Ask the sender which identifier is the MRN, and read that one instead. - The step fails before sending, naming
{{data.mrn}}. The step's input has nomrn. Check the JavaScript step's output in the test. - The test fails with a blocked-address error. The base URL is on a private address. Add it to
SSRF_ALLOWLIST, or reach it through a tunnel in the same workspace. - Your API gets the same patient twice. That is expected for two messages about one patient: the PUT replaces the record. Two records mean the API creates on PUT without matching the MRN.
- The sender gets
AEwhile your API is down. The trigger's ACK the sender is destination. Switch it to accept to let messages queue in the engine.