How to build an HL7 message

Learn how the basic structure of an HL7 message works.

Building an HL7 message involves creating a specific format that includes a header and content segments. Segments are separated by a carriage return (), and each segment contains fields that are separated by a pipe character (|). Additionally, if fields contain subfields, they are separated by a caret (^).

Step-by-step guide to building the message

1

Creating the MSH segment (Message Header)

The first segment in any HL7 message is the MSH, which defines basic information about the message. The structure of the MSH generally includes the following fields:

  • MSH-1: Field Separator (usually “|”)

  • MSH-2: Encoding Characters (usually “^~&”)

  • MSH-3: Sending Application

  • MSH-4: Sending Facility

  • MSH-5: Receiving Application

  • MSH-6: Receiving Facility

  • MSH-7: Date/Time of Message

  • MSH-8: Security

  • MSH-9: Message Type (example: ADT^A01)

  • MSH-10: Message Control ID

  • MSH-11: Processing ID

  • MSH-12: Version ID

An example of how this can be formatted:

MSH|^~&|SendingApp|SendingFacility|ReceivingApp|ReceivingFacility|20241109120000||ADT^A01|123456|P|2.3|
2

Creating other segments

After the MSH, you can add other segments as needed. A common example is the PID (Patient Identification), which contains patient information.

Example of a PID segment:

PID|1||123456^^^Hospital^MR||Doe^John^^^Mr.|Smith|19800101|M|||123 Main St^^Anytown^NY^12345||(555)555-5555|||N|
3

Building the complete message

Now that we have the MSH and a PID segment, we can combine everything into a single HL7 message:

MSH|^~&|SendingApp|SendingFacility|ReceivingApp|ReceivingFacility|20241109120000||ADT^A01|123456|P|2.3\rPID|1||123456^^^Hospital^MR||Doe^John^^^Mr.|Smith|19800101|M|||123 Main St^^Anytown^NY^12345||(555)555-5555|||N|

Final considerations

When building HL7 messages, it’s important to ensure that all required fields are present and that they follow the expected format and order to avoid communication issues between the integrated healthcare systems.

Was this helpful?