Blog

    FHIR Bundle Resource

    By GauravNovember 13, 202610 mins read
    On this page

    A client that creates a Patient, an Encounter, and a Condition in three calls can die between the second and the third. The chart then has a visit with no diagnosis, or a diagnosis with no visit. FHIR's answer is not a bigger resource. It is a Bundle whose type says whether the server should store the package, search inside it, or apply every entry as one commit.

    This guide is only about the FHIR R4 Bundle: the type codes, what the first entry must be, how a transaction POST differs from a batch, and the mistakes that make a server store the package instead of processing it.

    What is a FHIR Bundle resource?

    Posting transactions that either commit or roll back?

    Agnotic Technologies builds FHIR transaction and document exchange, including the Bundle rules servers actually enforce.

    A FHIR R4 Bundle is a container for a collection of resources. The type is required and names document, message, transaction, batch, searchset, history, collection, or a response type. A transaction is posted to the server base in one request, and every entry commits together or the server rejects the set.

    HL7 describes bundling as gathering resources into one instance with context. The uses it names are search results, history, messages, clinical documents, and a set of creates, updates, and deletes including an atomic transaction. A Bundle is a container for transfer and storage. It is not itself the clinical meaning. List, Group, and Composition carry meaning and then point at, or in the document case sit inside, the package.

    Contained resources are a different mechanism. They live inside one resource and cannot be addressed on their own. Entries in a Bundle can have their own identity and can be read later with the REST API. If you only need a practitioner nested on a single note and nobody will query that practitioner, contained may be enough. If the next call must GET the resource, it belongs as a Bundle entry or as its own POST.

    Which Bundle type should you send?

    Pick the type from the operation, not from habit. Search results are a searchset. A signed clinical document is a document. An atomic write is a transaction. Using collection for all of them hides the rules the server is required to check.

    Four Bundle patterns side by side: a searchset returned by a FHIR server, a document whose first entry is a Composition, a message whose first entry is a MessageHeader, and a transaction posted to the server base URL that commits every entry or none
    • searchset: Results of a search. total is allowed. Each entry has a resource, and search.mode may be match, include, or outcome.
    • history: Versions from a history interaction. total is allowed. Request and response elements are required on entries.
    • document: First entry is a Composition. The Bundle needs an identifier with system and value, and a timestamp. Every entry has a resource.
    • message: First entry is a MessageHeader. Later entries are the focus of that header.
    • transaction: One atomic commit. Each entry needs request.method and request.url. PUT and POST entries also need a resource.
    • batch: Same request shape as a transaction, but each entry succeeds or fails on its own.
    • transaction-response and batch-response: What the server returns, one entry per request entry, same order, with response.status.
    • collection: A package with no special first entry and no processing rules. Use it when you truly mean 'a bag of resources'.

    How do you POST a transaction so it commits?

    POST the Bundle to the server base URL, not to /Bundle. The body has type transaction. The server either accepts every action and returns 200 with a transaction-response, or rejects the set and returns an HTTP 400 or 500. A failed transaction comes back as a single OperationOutcome, not as a response Bundle.

    HL7 is explicit that POST /Bundle stores the Bundle as an ordinary resource. The entries are not applied. Teams discover this when a patient fails to appear and the only new row is a stored Bundle. Processing happens on POST to the base. Servers that do not implement transactions should return 400. Do not assume every FHIR server can take one.

    • type: transaction on the request, transaction-response on success.
    • entry.request.method: POST, PUT, DELETE, GET, PATCH, or the other allowed verbs. GET inside a transaction is legal and returns the read in the response entry.
    • entry.request.url: The relative URL the method would have used, such as Patient or Patient/123.
    • Resource body: Required for PUT and POST. Not required for DELETE.
    • Order of entries: Must not change the outcome. The server follows a fixed processing order, not your file order.
    • Identity: A resource may appear once. Overlapping identities across conditional creates and updates fail the transaction.
    • Empty bundle: Allowed. It is not an error to submit no entries.
    • References inside the bundle: A POST entry can use a temporary fullUrl, and the server rewrites references to the id it assigns. Canonical references are not rewritten.

    Conditional references exist so an interface can point at 'the Patient with this identifier' without a prior search outside the transaction. The server must find exactly one match. Zero or many matches fail the whole transaction. That rule is why a sloppy identifier system, shared by two patients, will roll back a large claim bundle.

    How is a batch different from a transaction?

    A batch is independent writes in one HTTP round trip. A transaction is one commit. If any transaction entry fails, none of the writes remain. If a batch entry fails, the others still stand, and the HTTP status is 200 as long as the batch itself was processed. You read each entry's response code to see what happened.

    • No cross-entry dependencies in a batch. A reference from one new resource to another new resource in the same batch is non-conformant.
    • Transaction references between entries are expected, including circular references, because the server assigns ids and rewrites links before finishing.
    • Batch response: look inside. A non-2xx on an entry is that entry's failure, not a failure of the HTTP call.
    • Transaction failure: no response Bundle. One OperationOutcome describes the rejection.
    • Use batch for a nightly load of unrelated documents. Use transaction when a note, its subject, and its provenance must appear together.
    • History bundles can be replayed as either, with care: a history list may contain the same resource more than once, which a transaction forbids.

    What must a document or message Bundle contain?

    A document Bundle starts with a Composition, carries an identifier that has both system and value, and has a timestamp. A message Bundle starts with a MessageHeader. In both cases every entry contains a resource. Order matters for that first entry and is otherwise defined by the type.

    The Composition is the attested statement. The Bundle is the bytes. Signing, when you need it, uses Bundle.signature. Do not invent a second wrapper resource to mean 'these entries are the document'. If you POST a document Bundle to the base URL, a server may accept types other than batch or transaction, but you should not expect it to file the Composition unless that behavior is in the CapabilityStatement. Storing via POST /Bundle keeps the document as a Bundle you can GET later. Search parameters composition and message let you query into that first entry.

    • Missing Composition in position one: the document invariant fails.
    • Identifier without a system: also fails for documents.
    • timestamp omitted: documents require it. The element is the date the bundle was assembled.
    • MessageHeader not first: the message invariant fails, even if a MessageHeader appears later.
    • fullUrl: Required in spirit for entries outside transaction and batch. Use a UUID URN when the resource has no persistent id yet. fullUrl must not be version-specific. Duplicate fullUrls are illegal unless the version ids differ, with a history exception.

    How do searchset and history Bundles behave?

    A searchset may contain zero entries. total is the match count, and it is legal only on searchset and history. entry.search is legal only on searchset. Paging uses Bundle.link with relations such as next and self. Clients that ignore link and re-query with a new page offset will skip or repeat rows when the underlying data changes.

    search.mode tells you why the entry is present. match is a hit. include is a resource pulled in because you asked for _include. outcome is an OperationOutcome mixed into the results. Treating every entry as a match will display included practitioners as if they were patients.

    History entries carry request and response because they record what happened. They are a poor template for a fresh transaction: the same resource can show up twice, and the original transaction boundaries are gone. If you replicate from history, decide explicitly whether you are applying a transaction or a batch, and collapse repeated ids first.

    How are references resolved inside a Bundle?

    Resolution uses ordinary FHIR identity rules. A reference matches an entry whose fullUrl is that URL, or whose resource id matches, depending on whether the reference is a local id or an absolute URL. UUID fullUrls are how a transaction names a resource that does not exist on the server yet.

    • For a transaction POST, the server ignores any id you claimed and assigns one, then rewrites matching links in the same Bundle.
    • Version-specific references stay version-specific after rewrite.
    • References to resources outside the Bundle are left alone.
    • Identifier-only references can be resolved by scanning identifiers in the Bundle when there is no URL.
    • Conditional references, written as a search URL, are resolved by the server during the transaction and fail the commit if the match is not unique.
    • Elements of type canonical are not rewritten. Do not use a temporary fullUrl there and expect the server to fix it.

    Which Bundle mistakes show up in testing?

    The expensive bugs are the ones that return a polite status while doing the wrong persistence.

    • Posted to /Bundle: The package is stored. No Patient is created. The test that only checks HTTP 201 passes.
    • type collection with request entries: request is forbidden except on batch, transaction, and history. Validators catch this. Lenient servers may ignore it.
    • total on a document: total is only for search or history. Remove it.
    • Batch with internal references: Non-conformant. The second entry's subject points at a fullUrl the server will not rewrite.
    • Two entries, one identity: The transaction fails. Split the updates or send the latest state once.
    • Assuming entry order: A delete of Patient/1 followed by a create that needs Patient/1 is not ordered by your file. Use the processing rules, or use conditional URLs.
    • Reading only the outer status on a batch: The call is 200 and three of fifty entries are 422.
    • fullUrl reused: Two entries share a fullUrl with the same version. The invariant fails and links become ambiguous.

    Add one test that posts a transaction whose second entry is invalid and then GETs the first resource. It must be absent. Add another that posts the same body to /Bundle and asserts that the resources were not created. Those two tests catch the failure mode this resource is famous for.

    How Agnotic can help

    Agnotic Technologies builds Bundle handling for products that must write several resources together: transaction endpoints, document packages with Composition, searchset paging, and the negative tests that prove a failed entry left nothing behind. Agnotic also sets the CapabilityStatement so partners know whether batch and transaction are actually implemented.

    If your facade sits on Epic, Oracle Health (Cerner), or a custom chart, we choose transaction only where the source can roll back, and batch where the source commits per row. Pretending an EHR supports atomic multi-resource writes is how bundles lie.

    Make multi-resource writes honest

    Walk through your Bundle types with an engineer, or read the FHIR hub.

    Related reading on Agnotic

    Messaging, implementation guides, and the broader FHIR map are the neighbors of this resource.

    Sources

    Frequently Asked Questions

    To the server base URL. POST /Bundle stores the Bundle and does not apply the entries. The success body is a Bundle of type transaction-response. A failure is a single OperationOutcome.

    Talk to an engineer about your project

    We implement FHIR R4 Bundles for transactions, documents, and search, including tests that prove a failed commit leaves no partial writes.