Skip to main content

HL7

HL7 v2.x pipe-delimited message support is provided for healthcare systems integration through the ballerinax/health.hl7v2commons module (v2.0.1) and version-specific modules. These modules provide typed segment models, message parsing, and cross-version interoperability for all HL7 v2 versions from v2.3 through v2.8.

Module

ballerinax/health.hl7v2commons (cross-version types)

Version-specific modules: ballerinax/health.hl7v23, ballerinax/health.hl7v231, ballerinax/health.hl7v24, ballerinax/health.hl7v25, ballerinax/health.hl7v251, ballerinax/health.hl7v26, ballerinax/health.hl7v27, ballerinax/health.hl7v271, ballerinax/health.hl7v28

Usage

Parse an HL7 v2 message

import ballerinax/health.hl7v2;

string hl7Message = "MSH|^~\\&|SENDING|FACILITY|RECEIVING|FACILITY|20240115||ADT^A01|MSG001|P|2.5\rPID|||12345||Smith^Jane||19900515|F";

hl7v2:Message msg = check hl7v2:parse(hl7Message);

Work with typed segments

import ballerinax/health.hl7v25;

// Access typed segments for HL7 v2.5
hl7v25:MSH msh = {
msh1: "|",
msh2: "^~\\&",
msh3: {hd1: "SENDING_APP"},
msh4: {hd1: "SENDING_FACILITY"},
msh5: {hd1: "RECEIVING_APP"},
msh6: {hd1: "RECEIVING_FACILITY"},
msh9: {msg1: "ADT", msg2: "A01"},
msh10: "MSG001",
msh11: {pt1: "P"},
msh12: {vid1: "2.5"}
};

hl7v25:PID pid = {
pid3: [{cx1: "12345"}],
pid5: [{xpn1: {fn1: "Smith"}, xpn2: "Jane"}],
pid7: "19900515",
pid8: "F"
};

Cross-version interoperability

import ballerinax/health.hl7v2commons;

// Union types work across HL7 versions
// Msh covers MSH from v2.3 through v2.8
hl7v2commons:Msh mshSegment = ...;

// Pid covers PID from v2.3 through v2.8
hl7v2commons:Pid pidSegment = ...;

Functions

FunctionSignatureDescription
parseparse(string message) returns Message|hl7v2:HL7ErrorParse an HL7 v2 pipe-delimited message string into a typed Message.

Key segment types

The hl7v2commons module provides version-agnostic union types for the most common HL7 segments:

Union typeHL7 segmentDescription
MshMSHMessage header — sending/receiving app, message type, version.
PidPIDPatient identification — name, DOB, gender, identifiers.
Pv1PV1Patient visit — admission, attending physician, location.
Pv2PV2Patient visit additional info — expected length, accommodation.
EvnEVNEvent type — trigger event, date/time, operator.
Nk1NK1Next of kin — contact name, relationship, phone.
Al1AL1Allergy information — type, code, severity.
Dg1DG1Diagnosis — code, description, type.
ObxOBXObservation/result — test, value, units, status.
OrcORCCommon order — order control, placer/filler number.
ObrOBRObservation request — test ordered, specimens, results.
In1IN1Insurance — plan ID, company, group, policy.
Gt1GT1Guarantor — name, address, phone, employer.
MsaMSAMessage acknowledgment — ack code, message control ID.
MrgMRGMerge patient — prior patient identifiers.
NteNTENotes and comments — comment type and text.

Supported versions

ModuleHL7 versionLatest version
health.hl7v23v2.34.0.1
health.hl7v231v2.3.14.0.1
health.hl7v24v2.44.0.1
health.hl7v25v2.54.0.1
health.hl7v251v2.5.14.0.1
health.hl7v26v2.64.0.1
health.hl7v27v2.74.0.1
health.hl7v28v2.84.0.1