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.
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',)How to construct and work with real-world MSH segments.
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')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'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'
)Step-by-step guides for working with HL7 v2 in Zato.
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.
Complete list of fields in the MSH segment, HL7 v2.9.
| # | Python name | Datatype | Usage | Repeatable | Table |
|---|---|---|---|---|---|
| 1 | field_separator | ST | Required | No | - |
| 2 | encoding_characters | ST | Required | No | - |
| 3 | sending_application | HD | Optional | No | HL70361 |
| 4 | sending_facility | HD | Optional | No | HL70362 |
| 5 | receiving_application | HD | Optional | No | HL70361 |
| 6 | receiving_facility | HD | Optional | Yes | HL70362 |
| 7 | date_time_of_message | DTM | Required | No | - |
| 8 | security | ST | Optional | No | - |
| 9 | message_type | MSG | Required | No | - |
| 10 | message_control_id | ST | Required | No | - |
| 11 | processing_id | PT | Required | No | - |
| 12 | version_id | VID | Required | No | - |
| 13 | sequence_number | NM | Optional | No | - |
| 14 | continuation_pointer | ST | Optional | No | - |
| 15 | accept_acknowledgment | ID | Optional | No | HL70155 |
| 16 | application_acknowledgment_type | ID | Optional | No | HL70155 |
| 17 | country_code | ID | Optional | No | HL70399 |
| 18 | character_set | ID | Optional | Yes | HL70211 |
| 19 | principal_language_of_message | CWE | Optional | No | HL70609 |
| 20 | alternate_character_set_handling_scheme | ID | Optional | No | HL70356 |
| 21 | message_profile_identifier | EI | Optional | Yes | - |
| 22 | sending_responsible_organization | XON | Optional | No | - |
| 23 | receiving_responsible_organization | XON | Optional | No | - |
| 24 | sending_network_address | HD | Optional | No | - |
| 25 | receiving_network_address | HD | Optional | No | - |
| 26 | security_classification_tag | CWE | Optional | No | HL70952 |
| 27 | security_handling_instructions | CWE | Optional | Yes | HL70953 |
| 28 | special_access_restriction_instructions | ST | Optional | Yes | - |
Get started with Zato and connect your systems in minutes.