Schedule a demo
Segment

Message header

Must be the first segment in every HL7 v2 message. It defines the message's encoding characters, sending and receiving application and facility, message type and trigger event, message control identifier, processing identifier (production, test, or debug), and HL7 version. Every sending system produces an MSH and every receiving system parses it first to determine how to route and process the message.

28fields
7required
v2.9HL7 version
msh.py
from zato.hl7v2.v2_9 import MSH
from zato.hl7v2.v2_9 import HD, MSG
from zato.hl7v2.v2_9 import PT, VID

msh = MSH()
msh.field_separator = '|'
msh.encoding_characters = '^~\\&'
msh.sending_application = HD(
    namespace_id='WELL',
    universal_id='Wellness Hub',
)
msh.message_type = MSG(
    message_code='ADT',
    trigger_event='A04',
    message_structure='ADT_A01',
)
msh.processing_id = PT(
    processing_id='P',
    processing_mode='T',
)
msh.version_id = VID(version_id='2.9',)

Build MSH segments in Python

How to construct and work with real-world MSH segments.

1

ADT admission from HIS to Lab

An ADT^A01 admission message sent from the hospital information system to the laboratory system in production mode

from zato.hl7v2.v2_9 import MSH
from zato.hl7v2.v2_9 import HD, MSG, PT, VID

msh = MSH()
msh.field_separator = '|'
msh.encoding_characters = '^~\\&'
msh.sending_application = HD(
    namespace_id='HIS',
    universal_id='Hospital Info',
    universal_id_type='DNS'
)
msh.sending_facility = HD(
    namespace_id='MAIN',
    universal_id='Main Campus',
    universal_id_type='DNS'
)
msh.receiving_application = HD(
    namespace_id='LAB',
    universal_id='Lab System',
    universal_id_type='DNS'
)
msh.date_time_of_message = '20240601120000'
msh.message_type = MSG(
    message_code='ADT',
    trigger_event='A01',
    message_structure='ADT_A01'
)
msh.message_control_id = 'MSG-20240601-001'
msh.processing_id = PT(
    processing_id='P',
    processing_mode='T'
)
msh.version_id = VID(version_id='2.9')
2

ORM order with security and acknowledgment

An ORM^O01 order message with a security token and accept-level acknowledgment between an order entry and pharmacy system

from zato.hl7v2.v2_9 import MSH
from zato.hl7v2.v2_9 import HD, MSG, PT, VID

msh = MSH()
msh.field_separator = '|'
msh.encoding_characters = '^~\\&'
msh.sending_application = HD(
    namespace_id='CPOE',
    universal_id='Order Entry',
    universal_id_type='DNS'
)
msh.sending_facility = HD(
    namespace_id='EAST',
    universal_id='East Wing',
    universal_id_type='DNS'
)
msh.receiving_application = HD(
    namespace_id='PHARM',
    universal_id='Pharmacy',
    universal_id_type='DNS'
)
msh.receiving_facility = HD(
    namespace_id='EAST',
    universal_id='East Wing',
    universal_id_type='DNS'
)
msh.date_time_of_message = '20240715083000'
msh.security = 'AUTH-TKN-5521'
msh.message_type = MSG(
    message_code='ORM',
    trigger_event='O01',
    message_structure='ORM_O01'
)
msh.message_control_id = 'CTRL-20240715-100'
msh.processing_id = PT(processing_id='P')
msh.version_id = VID(version_id='2.9')
msh.accept_acknowledgment = 'AL'
3

MDM document notification with country and character set

An MDM^T02 document notification message that includes country code, character set, and principal language fields

from zato.hl7v2.v2_9 import MSH
from zato.hl7v2.v2_9 import CWE, HD, MSG, PT, VID

msh = MSH()
msh.field_separator = '|'
msh.encoding_characters = '^~\\&'
msh.sending_application = HD(
    namespace_id='DOCMGR',
    universal_id='Document Manager',
    universal_id_type='DNS'
)
msh.sending_facility = HD(
    namespace_id='CENTRAL',
    universal_id='Central Office',
    universal_id_type='DNS'
)
msh.receiving_application = HD(
    namespace_id='EHR',
    universal_id='Health Records',
    universal_id_type='DNS'
)
msh.receiving_facility = HD(
    namespace_id='CENTRAL',
    universal_id='Central Office',
    universal_id_type='DNS'
)
msh.date_time_of_message = '20240820140000'
msh.message_type = MSG(
    message_code='MDM',
    trigger_event='T02',
    message_structure='MDM_T02'
)
msh.message_control_id = 'DOC-20240820-042'
msh.processing_id = PT(processing_id='P')
msh.version_id = VID(version_id='2.9')
msh.country_code = 'USA'
msh.character_set = 'UNICODE UTF-8'
msh.principal_language_of_message = CWE(
    identifier='en',
    text='English',
    name_of_coding_system='ISO639-2'
)

Learn by building

Step-by-step guides for working with HL7 v2 in Zato.

Frequently asked questions

MSH establishes the delimiter characters, the sender and receiver context, the message type and control identifiers, and the HL7 version. Parsers read MSH before interpreting following segments.

Use the MSG composite with message_code, trigger_event, and message_structure on separate lines:

from zato.hl7v2.v2_9 import MSH
from zato.hl7v2.v2_9 import MSG

msh = MSH()
msh.message_type = MSG(
    message_code='ADT',
    trigger_event='A04',
    message_structure='ADT_A01',
)

HD carries namespace_id, universal_id, and universal_id_type as separate keyword arguments:

from zato.hl7v2.v2_9 import MSH
from zato.hl7v2.v2_9 import HD

msh = MSH()
msh.sending_application = HD(
    namespace_id='WELLAPP',
    universal_id='Wellness Hub',
    universal_id_type='DNS',
)

processing_id is a PT value (production vs training and processing mode). version_id is a VID that names the HL7 version such as 2.9. Both are required on MSH in typical implementations.

Use accept_acknowledgment and application_acknowledgment_type when partners negotiate AL/NE or similar acknowledgment rules for wellness feeds, screening results, or scheduling traffic.

Zato connects to any system that speaks HL7v2 over MLLP or FHIR over REST. This includes Epic, Cerner, Meditech, Allscripts, and other EHR platforms.

Yes, Zato has built-in MLLP support for sending and receiving HL7v2 messages. MLLP channels handle the framing protocol automatically.

Zato supports HL7 v2.9, which is backward compatible with earlier versions including v2.3, v2.5, and v2.7. Standard segments and datatypes are available as typed Python classes.

Yes. Zato handles both HL7v2 and FHIR natively, so you can parse v2 messages and build FHIR resources with IG-specific extensions in the same service. Typed Python classes are available for both protocols, including extensions from US Core, UK Core, Da Vinci, and other Implementation Guides.

MSH field reference

Complete list of fields in the MSH segment, HL7 v2.9.

#Python nameDatatypeUsageRepeatableTable
1field_separatorSTRequiredNo-
2encoding_charactersSTRequiredNo-
3sending_applicationHDOptionalNoHL70361
4sending_facilityHDOptionalNoHL70362
5receiving_applicationHDOptionalNoHL70361
6receiving_facilityHDOptionalYesHL70362
7date_time_of_messageDTMRequiredNo-
8securitySTOptionalNo-
9message_typeMSGRequiredNo-
10message_control_idSTRequiredNo-
11processing_idPTRequiredNo-
12version_idVIDRequiredNo-
13sequence_numberNMOptionalNo-
14continuation_pointerSTOptionalNo-
15accept_acknowledgmentIDOptionalNoHL70155
16application_acknowledgment_typeIDOptionalNoHL70155
17country_codeIDOptionalNoHL70399
18character_setIDOptionalYesHL70211
19principal_language_of_messageCWEOptionalNoHL70609
20alternate_character_set_handling_schemeIDOptionalNoHL70356
21message_profile_identifierEIOptionalYes-
22sending_responsible_organizationXONOptionalNo-
23receiving_responsible_organizationXONOptionalNo-
24sending_network_addressHDOptionalNo-
25receiving_network_addressHDOptionalNo-
26security_classification_tagCWEOptionalNoHL70952
27security_handling_instructionsCWEOptionalYesHL70953
28special_access_restriction_instructionsSTOptionalYes-

Ready to build integrations?

Get started with Zato and connect your systems in minutes.

Open source In Python