## Hl7Message.getSegment() method [​](/content/docs/sdk/core.hl7message.getsegment#hl7messagegetsegment-method "Direct link to Hl7Message.getSegment(/index.html) method")

Returns an HL7 segment by index or by name.

When using a numeric index, the first segment (usually the MSH header segment) is at index 0.

When using a string index, this method returns the first segment with the specified name.

Segments are lazily parsed; the requested segment is parsed and cached on demand.

**Signature:**

```typescript
getSegment(index: number | string): Hl7Segment | undefined;
```

## Parameters [​](/content/docs/sdk/core.hl7message.getsegment#parameters "Direct link to Parameters"/index.html)

| Parameter | Type | Description |
| --- | --- | --- |
| index | number \| string | The HL7 segment index or name. |

**Returns:**

[Hl7Segment](/content/docs/sdk/core.hl7segment) \| undefined

The HL7 segment if found; otherwise, undefined.

- [Hl7Message.getSegment() method](/content/docs/sdk/core.hl7message.getsegment#hl7messagegetsegment-method/index.html)
- [Parameters](/content/docs/sdk/core.hl7message.getsegment#parameters/index.html)
