Data & APIs

How to Generate SOAP Envelopes, CSV Files and HL7 Messages in n8n

Jabane Mohamed Ayoub6 min read

How to Generate SOAP Envelopes, CSV Files and HL7 Messages in n8n title card on a pink-to-purple gradient, reading: SOAP, correctly quoted CSV and HL7
Table of contents
  1. The three formats fail in three different ways
  2. Build a SOAP request
  3. Build a correctly quoted CSV file
  4. Build an HL7 message
  5. Pitfalls that cost time
  6. Wrapping up
  7. FAQ
  8. Related resources

To generate a SOAP request, a CSV file or an HL7 message in n8n without a Code node, use the Message Template node: write the document as a template with {{ }} expressions, choose the output format, and let the node handle joining items and line endings. The part that needs care is escaping, because each format breaks differently when a value contains the wrong character.

This guide builds all three, and I ran each example through the real node to check the output shown. If you want the overview first, Message Template: Render JSON, XML, CSV and HL7 in n8n Without a Code Node explains the node, its two modes and its four outputs.

The three formats fail in three different ways

A SOAP body, a CSV row and an HL7 segment look like they are all “just text”. They are not interchangeable:

Format Breaks when a value contains What the node must do
SOAP / XML & or < Escape them as entities
CSV a comma, a quote or a line break Quote the field and double any inner quote
HL7 |, ^, &, ~ or \ Escape with HL7 sequences, and end segments with a carriage return

Message Template does not guess which one you are building. You escape inside the expression, and the node does the structural work: joining, header and footer, line endings, and parsing the result so mistakes show up early.

Build a SOAP request

Use one item per request. In Run Once for Each Item mode, set Output Format to XML and add the Add Formatted XML Field option with the name xml. The template is the envelope itself:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetCustomer><Id>{{ $json.id.replaceAll('&', '&amp;').replaceAll('<', '&lt;') }}</Id></GetCustomer>
  </soap:Body>
</soap:Envelope>

For an item with id set to A1, the node returns two things. The envelope as structured data you can drag fields from, with the namespace under $:

{
  "soap:Envelope": {
    "$": { "xmlns:soap": "http://schemas.xmlsoap.org/soap/envelope/" },
    "soap:Body": { "GetCustomer": { "Id": "A1" } }
  }
}

and the pretty-printed text in the xml field:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetCustomer>
      <Id>A1</Id>
    </GetCustomer>
  </soap:Body>
</soap:Envelope>

Connect an HTTP Request node, use the POST method, send a raw body with the content type text/xml; charset=utf-8, and set the body to {{ $json.xml }}. Add the SOAPAction header if the service’s WSDL asks for one. Because the node parses the XML before it passes it on, a missing closing tag fails here with a line and column, not as a vague fault from the remote service.

Build a correctly quoted CSV file

Use Combine All Items so that every item becomes one row of a single file:

Setting Value
Mode Combine All Items
Header id,name,total followed by a line break
Separator a line break
Output Format File
File Name orders.csv
MIME Type text/csv
Options → Line Endings Windows (CRLF)

The template renders one row:

{{ $json.id }},"{{ String($json.name).replaceAll('"', '""') }}",{{ $json.total }}

With two orders, one named Ann & Co and one named Bo "the" Builder, Ltd, the file contains this (shown with \r\n line endings made visible):

id,name,total\r\n
A1,"Ann & Co",10.5\r\n
B2,"Bo ""the"" Builder, Ltd",7

The name with both a quote and a comma stays in one cell, because the inner quotes are doubled. I would avoid the tempting shortcut of JSON.stringify($json.name) for CSV. It escapes an inner quote as \", so that same name comes out as "Bo \"the\" Builder, Ltd", which most CSV parsers read as a broken row.

Build an HL7 message

Use Run Once for Each Item with Text output, so the message lands in the message field. Set Options → Line Endings to Carriage Return Only (CR), because HL7 separates segments with a carriage return. Write one segment per line:

MSH|^~\&|APP|FAC|RECV|RFAC|{{ $now.toFormat('yyyyMMddHHmmss') }}||ADT^A08|{{ $json.id }}|P|2.4
PID|1||{{ $json.mrn }}||{{ $json.lastName }}^{{ $json.firstName }}

The node converts every line break in the output to a carriage return, so the two segments are separated by \r and the backslash in ^~\& stays untouched. Free-text values that can contain a separator need escaping before they go into a field. In HL7 the sequences are \F\ for a pipe, \S\ for a caret, \E\ for a backslash, \R\ for a tilde and \T\ for an ampersand. Escape the backslash first:

{{ String($json.lastName).replaceAll('\\', '\\E\\').replaceAll('|', '\\F\\').replaceAll('^', '\\S\\') }}

I tested that chain on Doe|Smith^Jr\X and got Doe\F\Smith\S\Jr\E\X. Extend it with the other two sequences if your data can contain them.

Message Template builds the message. How it reaches the receiving system depends on what that system accepts: a file drop, an HTTP endpoint or a gateway that speaks MLLP.

Pitfalls that cost time

  • Header, separator and footer come from the first item in combine mode. Set them once and do not expect per-item variation.
  • A header needs its own line break. In the CSV above, the header ends with a line break so the first row starts on a new line.
  • Check line endings against the receiver. CSV partners often want CRLF, HL7 wants CR, and most APIs do not care. Set Line Endings explicitly instead of relying on how your template was typed.
  • Turn on error continuation while testing. With Settings → On Error → Continue, a bad item comes out as { "error": "…" } and the other items still run.
  • XML values are strings. Convert numbers in a later step if a downstream node needs a number.

Wrapping up

Text-shaped formats are easier to get right when the template looks like the output. Put the document in the template, put the escaping in the expression, and let the node own the joining and the line endings. To reshape data into a target schema before you render it, How to Convert JSON to XML in n8n With Attributes, Repeated Elements and Lookups shows that side of the job.

FAQ

How do I send a SOAP request from n8n?

Build the envelope with a Message Template node set to XML output with a formatted XML field, then send that field as a raw text/xml body from an HTTP Request node using POST. Add the SOAPAction header your service requires.

How do I create a CSV file in n8n with quotes and commas handled correctly?

Use Combine All Items with a header, wrap each text value in double quotes, and double any quote inside the value. For example, "{{ String($json.name).replaceAll('"', '""') }}". Output the result as a file with the text/csv MIME type.

Why does my HL7 message need carriage returns?

HL7 v2 separates segments with a carriage return, not a line feed. Set Line Endings to Carriage Return Only (CR) in the Options of the Message Template node so any line break in your template becomes a CR.

Can I build all three from the same data?

Yes. The same input items can feed three Message Template nodes, one per format. Each template and each escaping rule is separate, which keeps a change to the CSV from touching the SOAP request.

Do I need a Code node for any of this?

Not for the structure. Small expressions inside {{ }} handle the escaping. A Code node is still the right place for complicated conditional logic, which can run before the template.