# How to build an HL7 message

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**

{% stepper %}
{% step %}

### **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:

{% code overflow="wrap" %}

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

{% endcode %}
{% endstep %}

{% step %}

### **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:

{% code overflow="wrap" %}

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

{% endcode %}
{% endstep %}

{% step %}

### **Building the complete message**

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

{% code overflow="wrap" %}

```
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|
```

{% endcode %}
{% endstep %}
{% endstepper %}

## **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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.digibee.com/documentation/connectors-and-triggers/connectors/industry-solutions/hl7/build-an-hl7-message.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
