Pull from an API to HL7v2 over MLLP
Poll a REST API for new events, turn each one into an HL7v2 message with a JavaScript step and send it to an EHR over MLLP.
On this page
This guide builds a workflow for a source that cannot push to the engine, such as a patient messaging or scheduling system that only offers a REST API. A polling trigger reads the API every few seconds. Each new event runs once: a JavaScript step turns it into an HL7v2 ADT^A08 (patient update), and an MLLP send delivers it to the EHR and waits for its ACK.
Here is the flow.
sequenceDiagram
participant Engine as MedHook engine
participant API as Events API (test)
participant EHR as EHR (MLLP)
loop Every interval
Engine->>API: GET /events?updatedSince={cursor}
API-->>Engine: { data: [events] }
end
Engine->>Engine: One execution per new event
Engine->>Engine: JavaScript: event to ADT^A08
Engine->>EHR: HL7v2 message over MLLP
EHR-->>Engine: ACK (AA)Before you start
- A running engine and a signed-in user. See the Quick start.
- A REST API that returns a list of events as JSON. Each event needs an id and a value that grows with each change, such as an
updatedAttimestamp. This guide useshttps://api.example.test, which answersGET /eventswith:
{
"data": [
{
"id": "evt-1001",
"type": "contact.updated",
"updatedAt": "2026-10-19T14:02:11Z",
"patient": {
"mrn": "TEST000123",
"family": "TESTPATIENT",
"given": "ALPHA",
"birthDate": "1980-01-01",
"sex": "F",
"phone": "555-0101"
}
}
]
}- An EHR, or a test receiver, that listens for HL7v2 over MLLP and answers with an ACK. Its host and port must be reachable from the engine.
Steps
1. Create the two adapter profiles
Open Adapters and choose New connection twice. Settings that aren't in the short form are under More settings.
| Profile | Type | Settings |
|---|---|---|
Events API | Generic REST | Base URL https://api.example.test, and the Auth type the API needs |
Demo Health EHR | MLLP | host ehr.example.test, port 2575, timeout 10000 (connect and ACK wait) |
The engine refuses a Generic REST base URL on a private address unless the operator allows it. See Adapters. For the MLLP settings, including TLS, see Outbound sends.
2. Create the workflow
Open Workflows and choose New workflow. On Start, pick Poll and push and Blank workflow, then Continue. Source asks Which API do you pull from?: you can paste the events API's description to list its endpoints, or skip it, then Continue. Leave Sample empty and choose Continue. On Draft, enter the name Contact updates → EHR. Leave This workflow processes PHI on for real patient data. The workflow then runs only when your license allows PHI. Choose Open in builder. The draft starts with a Schedule trigger: you change it to polling in the next step.
3. Set up the polling trigger
On the Source step, set Trigger type to polling and fill in:
| Field | Value |
|---|---|
| Interval (seconds) | 60, or as low as 10 while you test |
| Adapter profile | Events API · generic-rest |
| Action | read |
| Params | {"endpoint": "/events", "query": {"updatedSince": "{{cursor}}"}} |
| Items path | data, where the list is in the API's answer |
| Cursor field | updatedAt |
| Id field | id (the default) |
| First poll | now to skip what is already there, or beginning to send it |
{{cursor}} becomes the newest updatedAt the workflow has already run, so each read asks only for what changed. On the first poll there is no cursor yet, so updatedSince is left out and the API returns everything.
4. Build the HL7v2 message
Choose + Add destination and set Destination name to Demo Health EHR. Under Output, choose + Add output step. It starts as JavaScript. Replace its code with:
const e = input.item;
const p = e.patient;
const ts = e.updatedAt.replace(/[-:TZ]/g, "");
return [
`MSH|^~\\&|MEDHOOK|DEMO|EHR|DEMOHEALTH|${ts}||ADT^A08^ADT_A01|${e.id}|P|2.5.1`,
`EVN|A08|${ts}`,
`PID|1||${p.mrn}^^^DEMO^MR||${p.family}^${p.given}||${p.birthDate.replace(/-/g, "")}|${p.sex}|||||${p.phone}`,
].join("\r");A polling run gives its steps { triggerType: "polling", triggerId, item, polledAt }, so the event is input.item. The step returns the message as text, with segments separated by \r. The event id becomes MSH-10, the message control ID. The EHR echoes it in MSA-2 of its ACK, so each ACK matches one event.
Under Send, set Adapter profile to Demo Health EHR · mllp and Action to send. Then choose Save.
5. Run a test
Choose Load sample, then Paste a sample. Paste one event as the API returns it, for example the evt-1001 object above, and choose Save sample. Then choose Run test. It runs the sample as a poll would, input.item included, and delivers nothing. The destination shows WOULD SEND and, under Would send, the HL7v2 message:
MSH|^~\&|MEDHOOK|DEMO|EHR|DEMOHEALTH|20261019140211||ADT^A08^ADT_A01|evt-1001|P|2.5.1
EVN|A08|20261019140211
PID|1||TEST000123^^^DEMO^MR||TESTPATIENT^ALPHA||19800101|F|||||555-01016. 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. Deploy needs a passing test newer than the last save, passing saved test cases (if the workflow has any) and an approval. An admin can deploy without them with Override and deploy, confirming Deploy anyway, and the audit log records it. See Save and deploy.
The first poll runs within a few seconds of the deploy, then once per interval.
7. Watch the events arrive
Open Messages. Each event is one row with the workflow name, the source polling and its outcome. Select a row:
- Raw shows the event as the API returned it.
- Trace lists each step. The Send line ends with the ACK the EHR answered, for example
Send · 13 ms · ACK MSA|AA|evt-1001.
🎉 New events in the API now reach the EHR as HL7v2, one message each. To send the same event to a second system, add another destination, as in Build a channel with several destinations.
Troubleshooting
- Nothing runs after the deploy. With First poll set to now, the first poll only records what is there, and only later changes run. Add or change an event in the API. First poll applies only when the cursor starts, so set it before the first deploy. Check Items path too: a path that finds no list reads no events.
- Every poll runs the same events again. The API ignored
updatedSince, and there is no cursor field. Set Cursor field, or make the API filter by the cursor. - Run test works but live runs fail with
Cannot read properties of undefined. The code reads the event's fields frominputinstead ofinput.item. - The send fails with
AEorAR. The EHR refused the message.AEis retried by the step's On failure setting;ARis not. Read the error in Messages, fix the message, and replay it. - An event failed and the next poll did not pick it up. The cursor moves past an event once its execution has started, so a failed send is not read again. Find it in Messages, fix the cause, and replay it. See Executions and replay.