Right now, somewhere in a hospital, a lab system is sending a patient's potassium result to the electronic health record (EHR). It isn't sending JSON, and it isn't calling a REST API. It's sending a few lines of plain text full of pipes and carets, in a format from the 1980s.

That format is HL7 version 2, and it still moves most hospital data. If you build healthcare software, you will meet it sooner or later.
The format looks cryptic. It isn't. Once you can read one HL7 message example, you can read most of them.

This guide follows one made-up patient through three messages: an admission, a lab order and a lab result. Every line gets a plain-English translation.

Why a 1980s format still runs hospitals

HL7 v2 dates back to 1987. It was built to move data between systems inside one hospital: registration, lab, pharmacy, radiology and billing.

Old does not mean retired. According to the HL7 Version 2 product brief, 95% of U.S. healthcare organizations used V2 in 2024, and more than 35 countries used it too.

It has lasted for two practical reasons:

  • It works. Hospitals run decades of interfaces on it, and nobody wants to rewrite a lab feed that works.
  • It's backward compatible. New versions add fields at the end and never renumber old ones. A parser written for version 2.3 can still read most of a 2.5.1 message.

How to read the HL7 message structure

Every HL7 v2 message has the same four layers:

  • Message: one event, such as "patient admitted."
  • Segment: one line that starts with a three-letter code, such as MSH or PID. A carriage return ends each segment.
  • Field: a value between two pipes (|). Position identifies each field, so PID-5 means the fifth field of the PID segment.
  • Component: one part of a field, separated by a caret (^). In DOE^JANE^A, the components are family name, given name and middle initial.

A tilde (~) separates repeated values, such as two phone numbers. A backslash (__) escapes special characters.

Every message opens with an MSH (message header) segment, and every MSH starts with MSH|^~&. Those characters declare the delimiters for that message. A good parser reads them from each message instead of assuming them.

One rule catches almost every beginner: empty fields still count. || means "this field exists and is empty." Skip one, and every field after it shifts by one position.

Meet Jane: one patient, three messages

Jane Doe arrives at General Hospital feeling weak. She's admitted, her doctor orders an electrolytes panel, and the lab sends results the same morning.

Each step produces one HL7 message, and all data here is synthetic.

The examples show one segment per line. To find a field, count the pipes: the value after the fifth pipe in a PID line is PID-5. MSH is the exception: its first pipe is MSH-1.

Message 1: The admission (HL7 ADT message)

When Jane is admitted, the registration system sends an ADT (admit, discharge, transfer) message. The trigger event, the real-world event behind the message, is A01: "admit a patient."

MSH|^~\&|ADT_APP|GENHOSP|LAB_APP|GENLAB|20261201083000||ADT^A01^ADT_A01|MSG00001|P|2.5.1 EVN|A01|20261201083000 PID|1||100234^^^GENHOSP^MR||DOE^JANE^A||19850412|F|||1 EXAMPLE RD^^ANYTOWN^IL^60000 PV1|1|I|4WEST^412^B^GENHOSP||||1234^SMITH^ALEX^^^DR|||MED 

Here's what each line says:
MSH: The registration app sends an admission (ADT^A01) to the lab on December 1 at 8:30 a.m., as message MSG00001.
EVN: The admission happened at 8:30 a.m.
PID: Jane A. Doe, female, born April 12, 1985, has medical record (MR) number 100234 at General Hospital.
PV1: She's an inpatient (I) in 4 West, room 412, bed B, under Dr. Alex Smith on the medicine service.

PHI in messages: The PID segment carries protected health information in plain text. A real HL7 feed needs encryption in transit, access controls and logs that never print full messages. More on that in Your First HIPAA App: 8 Things Developers Learn the Hard Way.

Message 2: The lab order (ORM)

Jane's doctor orders an electrolytes panel in the EHR. The EHR then sends an ORM (order) message to the lab. Newer HL7 versions use OML for lab orders, but many feeds still send ORM.

MSH|^~\&|EHR_APP|GENHOSP|LAB_APP|GENLAB|20261201091500||ORM^O01^ORM_O01|MSG00002|P|2.5.1 PID|1||100234^^^GENHOSP^MR||DOE^JANE^A||19850412|F PV1|1|I|4WEST^412^B^GENHOSP ORC|NW|ORD5001 OBR|1|ORD5001||ELEC^Electrolytes panel^L 

Here's what each line says:
MSH: The EHR sends a general order (ORM^O01) to the lab at 9:15 a.m., as message MSG00002.
PID: Same patient and record number, because the lab matches on the number, not the name.
PV1: Same location: 4 West, room 412, bed B.
ORC: NW means this is a new order, numbered ORD5001.
OBR The test is an electrolytes panel, sent with the hospital's local code ELEC (L means local).

ORD5001 is the placer order number, the ID the ordering system gives the order. The lab sends it back with the result, so the result lands on the right order.

Message 3: The result (HL7 ORU message)

The lab runs the test and replies with an ORU (observation result) message. The trigger event is R01.

MSH|^~\&|LAB_APP|GENLAB|EHR_APP|GENHOSP|20261201114500||ORU^R01^ORU_R01|MSG00003|P|2.5.1 PID|1||100234^^^GENHOSP^MR||DOE^JANE^A||19850412|F OBR|1|ORD5001|LAB7781|ELEC^Electrolytes panel^L|||20261201093000 OBX|1|NM|2951-2^Sodium [Moles/volume] in Serum or Plasma^LN||139|mmol/L|136-145|N|||F OBX|2|NM|2823-3^Potassium [Moles/volume] in Serum or Plasma^LN||5.8|mmol/L|3.5-5.1|H|||F 

Here's what each line says:
MSH: The direction flips: the lab sends a result (ORU^R01) to the EHR at 11:45 a.m.
PID: Still Jane, record 100234.
OBR: These results answer order ORD5001, from a specimen collected at 9:30 a.m.
OBX 1: Sodium is 139 mmol/L, inside the 136 to 145 range, so the flag is normal (N).
OBX 2: Potassium is 5.8 mmol/L, above the 3.5 to 5.1 range, so the flag is high (H).

In both OBX lines, NM means the value is a number, and F means the result is final. 2951-2 and 2823-3 are LOINC codes, the standard codes for lab tests, and LN tells the receiver which code system they come from.

Each OBX segment holds one result, so a panel of 10 tests means 10 OBX segments under one OBR.

The EHR then confirms receipt with a short ACK (acknowledgment) message. Its MSA segment returns AA (application accept) and echoes MSG00003, so the lab knows exactly which message arrived.

5 fields that break integrations

The usual suspects

  1. MSH-9 (message type). ADT^A01 means "admit." ADT^A08 means "update patient information." Treat an A08 like an A01, and every address change becomes a duplicate admission.
  2. MSH-10 (control ID). It must be unique for every message. If it repeats, the sender can't tell which message an ACK confirms, and a resent message may be processed twice.
  3. PID-3 (patient identifier list). It can repeat with ~ and hold a medical record number, an insurance ID and more. Check the assigning authority (the organization that issued the ID) and the ID type, not just the first value.
  4. OBX-3 (observation identifier). One sender uses LOINC, another uses local codes, and a third sends both. Your mapping table lives or dies here.
  5. Timestamps (MSH-7, OBR-7). Many feeds leave out the time zone, so 20261201083000 means "8:30 a.m. somewhere." Agree on the time zone in writing.

Where FHIR fits

If HL7 v2 feels like reading a fax, FHIR feels like a modern web API. It uses REST endpoints, JSON and resources such as Patient, Encounter and Observation. In FHIR, Jane's potassium result would be an Observation resource you could fetch with a GET request.

So why learn v2 at all? Because most hospitals run both. New apps and patient-facing APIs use FHIR, but the plumbing between registration, lab and pharmacy is still mostly v2. Many integration projects receive v2 messages and translate them into FHIR resources for newer apps.

For a side-by-side comparison of the two standards, see this guide to HL7 vs FHIR.

When integrations get serious

Reading three messages is easy. Running a production feed is not.

A real feed handles thousands of messages a day, survives network drops, retries safely and keeps an audit trail.

The weak spot is often a system you don't control. The CorLab engineering team saw this on eRețeta, Moldova's national e-prescribing platform, built with the National Medical Insurance Company (CNAM). The platform connects doctors' medical records systems, MSign (the government's e-signature service) and a central pharmacy interface. The night before launch, MSign froze. The platform's own code was ready, yet one shared service outside it stopped prescribing everywhere.

eRețeta isn't an HL7 v2 feed, but the lesson carries over: every integration point needs a plan for the moment the other side goes quiet.

That's why most teams don't parse raw pipes in application code. They place an integration engine or a dedicated service between the hospital and the app to validate, map and log each message. Solid API integration services agree on the data contract before writing code and keep an audit log for every call.

3 tips for your first HL7 feed

1. Get the partner's interface spec before you write a parser. HL7 v2 is flexible, and every hospital bends it a little. The spec shows which fields the partner actually fills, which codes it uses and whether it adds custom "Z" segments.
2. Build a library of synthetic test messages. Include admissions, updates, orders and results for fake patients, plus edge cases: empty fields, repeated IDs, abnormal flags and missing time zones. Run every parser change against that library.
3. Log message IDs, not message bodies. When a message fails, you need MSH-10, the message type and the error. You don't need Jane's name and birth date in your logs.

Once you can read Jane's three messages, you can follow the outline of almost any HL7 v2 feed. The details change from hospital to hospital. The pipes and carets don't.