Encoding API
From a tag-keyed message to a framed wire string in dictionary order, plus the byte-accurate framing math — BodyLength and CheckSum over UTF-8 bytes. Generated from @boarteam/fix 0.5.2 as installed; the doc text is the library’s own. Guide: Encoding FIX messages.
encodefunctionsince 0.1.0
function encode(message: EncodeMessage, dictionaries: Dictionary | FixtDictionaries, options?: EncodeOptions): stringSource: packages/fix/src/codec/encode.ts:63
Encode a message into a complete, framed FIX string: fields are emitted in the order the dictionary prescribes for the message (expanding components and repeating groups), then BeginString/BodyLength are prefixed and CheckSum appended.
The framing math is byte-accurate (UTF-8), so non-ASCII values produce spec-correct BodyLength/CheckSum. The function is pure: it reads only the supplied data and the dictionary — no wall-clock, sequence numbers, or comp-ID lookups (supply those in EncodeMessage.fields). Presence/enum validity are not checked here; run validate for that. Throws only on programming errors, never on absent fields: when EncodeMessage.msgType is unknown to the dictionary, or when a numeric value cannot be rendered as a valid FIX value (non-finite, or one that String() would emit in exponent notation — pass such values as pre-formatted strings).
dict may also be a FIXT transport/application pair (FixtDictionaries): the message is then encoded over the pair's merged view — tag 8 carries the transport's FIXT.1.1, session messages take the transport's definitions, and application bodies are wrapped in the transport envelope. A supplied ApplVerID(1128) field (falling back to the pair's defaultApplVerID) routes a resolveApp hook.
| Parameter | Type | Description |
|---|---|---|
| message | EncodeMessage | The MsgType plus tag-keyed fields and groups to render. |
| dictionaries | Dictionary | FixtDictionaries | The dictionary — or FIXT transport/application pair — that prescribes field order. |
| options? | EncodeOptions | Output separator (EncodeOptions). |
Returns string — the framed message, terminated by a trailing separator.
EncodeMessageinterfacesince 0.1.0
interface EncodeMessageSource: packages/fix/src/codec/encode.ts:19
The message to encode. Framing tags (8/9/10) are computed and need not be supplied.
msgType: stringThe
MsgType(tag 35) value.fields?: Record<number, FieldValue>Top-level field values, keyed by tag.
SendingTime/MsgSeqNum/comp-IDs go here.groups?: Record<number, GroupEntry[]>Top-level repeating groups, keyed by counter tag.
EncodeOptionsinterfacesince 0.1.0
interface EncodeOptionsSource: packages/fix/src/codec/encode.ts:29
Options for encode.
soh?: stringField separator for the output. Defaults to
SOH.
GroupEntryinterfacesince 0.1.0
interface GroupEntrySource: packages/fix/src/codec/encode.ts:11
One entry of a repeating group: its own fields and any nested groups.
fields?: Record<number, FieldValue>Field values for this entry, keyed by tag.
groups?: Record<number, GroupEntry[]>Nested groups within this entry, keyed by counter tag.
FieldValuetypesince 0.1.0
type FieldValue = string | number | booleanSource: packages/fix/src/codec/encode.ts:8
A scalar field value the caller can supply. Booleans map to Y/N.
calculateChecksumfunctionsince 0.1.0
function calculateChecksum(message: string | Uint8Array): stringSource: packages/fix/src/codec/checksum.ts:20
FIX CheckSum (tag 10): the sum of all bytes of the message — from BeginString up to and including the SOH immediately preceding the checksum field — taken modulo 256 and rendered as a zero-padded three-digit string.
The sum is computed over UTF-8 bytes, not JavaScript string code units, so a message containing non-ASCII field values (for example an EncodedText payload) yields a spec-correct checksum. This is the correctness fix over the original implementation, which summed String.prototype.charCodeAt code units.
| Parameter | Type | Description |
|---|---|---|
| message | string | Uint8Array | The message bytes (or a string, encoded as UTF-8) to checksum. |
Returns string — The checksum as a three-character, zero-padded decimal string ("000"–"255").
bodyLengthfunctionsince 0.1.0
function bodyLength(body: string | Uint8Array): numberSource: packages/fix/src/codec/checksum.ts:40
FIX BodyLength (tag 9): the number of bytes in the message following the BodyLength field's terminating SOH, up to and including the SOH immediately preceding the CheckSum field.
Measured in UTF-8 bytes, not string length, for the same reason as calculateChecksum.
| Parameter | Type | Description |
|---|---|---|
| body | string | Uint8Array | The portion of the message the body length covers. |
Returns number — The body length in bytes.