> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ferrum.thalamiq.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Transaction

> Execute multiple operations atomically

## Transaction

Execute multiple FHIR operations as a single atomic transaction. All operations succeed or all fail together.

## Endpoint

```
POST /fhir
```

## Request Body

The request body is a `Bundle` with `type` set to `transaction`:

```json theme={null}
{
  "resourceType": "Bundle",
  "type": "transaction",
  "entry": [
    {
      "request": {
        "method": "POST",
        "url": "Patient"
      },
      "resource": {
        "resourceType": "Patient",
        "name": [{"family": "Doe", "given": ["John"]}]
      }
    },
    {
      "request": {
        "method": "PUT",
        "url": "Patient/123"
      },
      "resource": {
        "resourceType": "Patient",
        "id": "123",
        "name": [{"family": "Doe", "given": ["Jane"]}]
      }
    },
    {
      "request": {
        "method": "DELETE",
        "url": "Observation/456"
      }
    },
    {
      "request": {
        "method": "GET",
        "url": "Patient/789"
      }
    }
  ]
}
```

## Request Methods

Each entry in the transaction can use different HTTP methods:

* `POST` - Create a new resource
* `PUT` - Update a resource (must include full resource)
* `PATCH` - Patch a resource
* `DELETE` - Delete a resource
* `GET` - Read a resource

## Request URL

The `url` in each request can be:

* **Absolute**: `Patient/123` - References a specific resource
* **Relative**: `Patient` - Creates a new resource of that type
* **Conditional**: `Patient?identifier=http://example.org/mrn|12345` - Conditional operation

## Response

### Success (200 OK)

Returns a `Bundle` with `type` set to `transaction-response`:

```json theme={null}
{
  "resourceType": "Bundle",
  "type": "transaction-response",
  "entry": [
    {
      "response": {
        "status": "201",
        "location": "Patient/abc123",
        "etag": "W/\"1\""
      },
      "resource": {
        "resourceType": "Patient",
        "id": "abc123",
        ...
      }
    },
    {
      "response": {
        "status": "200",
        "location": "Patient/123",
        "etag": "W/\"2\""
      },
      "resource": {
        "resourceType": "Patient",
        "id": "123",
        ...
      }
    },
    {
      "response": {
        "status": "204"
      }
    },
    {
      "response": {
        "status": "200",
        "location": "Patient/789",
        "etag": "W/\"1\""
      },
      "resource": {
        "resourceType": "Patient",
        "id": "789",
        ...
      }
    }
  ]
}
```

### Error (400 Bad Request)

If any operation fails, the entire transaction is rolled back:

```json theme={null}
{
  "resourceType": "OperationOutcome",
  "issue": [
    {
      "severity": "error",
      "code": "invalid",
      "details": {
        "text": "Transaction failed: Resource validation error in entry 2"
      }
    }
  ]
}
```

## Response Status Codes

Each entry's response includes a status code:

* `200 OK` - Successful read or update
* `201 Created` - Resource created
* `204 No Content` - Successful delete
* `400 Bad Request` - Invalid request (transaction fails)
* `404 Not Found` - Resource not found (transaction fails)
* `409 Conflict` - Version conflict (transaction fails)

## Conditional Operations

You can use conditional URLs in transactions:

```json theme={null}
{
  "request": {
    "method": "PUT",
    "url": "Patient?identifier=http://example.org/mrn|12345"
  },
  "resource": {
    "resourceType": "Patient",
    "identifier": [
      {
        "system": "http://example.org/mrn",
        "value": "12345"
      }
    ],
    "name": [{"family": "Doe"}]
  }
}
```

## Ordering

Operations in a transaction are processed in order. Later operations can reference resources created earlier:

```json theme={null}
{
  "entry": [
    {
      "request": {
        "method": "POST",
        "url": "Patient"
      },
      "resource": {
        "resourceType": "Patient",
        "id": "will-be-assigned"
      }
    },
    {
      "request": {
        "method": "POST",
        "url": "Observation"
      },
      "resource": {
        "resourceType": "Observation",
        "subject": {
          "reference": "Patient/will-be-assigned"
        }
      }
    }
  ]
}
```

The server resolves references to resources created in the same transaction.

## Notes

* All operations are atomic - either all succeed or all fail
* Operations are processed sequentially
* Resources created in the transaction can be referenced by later operations
* The transaction response includes the results of all operations
* Use transactions to ensure data consistency across multiple operations


## OpenAPI

````yaml POST /
openapi: 3.1.0
info:
  title: Ferrum API
  description: >-
    FHIR R4 RESTful API implementation. All endpoints are prefixed with `/fhir`
    and follow the FHIR specification.
  version: 1.0.0
  contact:
    name: Ferrum
    url: https://github.com/thalamiq/ferrum
  license:
    name: MIT
servers:
  - url: http://localhost:8080/fhir
    description: Local development server (default port 8080)
  - url: https://your-server.com/fhir
    description: Production server (replace with your actual server URL)
  - url: https://api.example.com/fhir
    description: Example production server
security:
  - bearerAuth: []
paths:
  /:
    post:
      tags:
        - Batch
        - Transaction
      summary: Batch or Transaction
      description: >-
        Execute a batch or transaction operation. The Bundle type determines the
        behavior: 'batch' for independent operations, 'transaction' for atomic
        operations.
      operationId: batchOrTransaction
      requestBody:
        required: true
        content:
          application/fhir+json:
            schema:
              $ref: '#/components/schemas/Bundle'
          application/fhir+xml:
            schema:
              $ref: '#/components/schemas/Bundle'
      responses:
        '200':
          description: Batch or transaction response
          content:
            application/fhir+json:
              schema:
                $ref: '#/components/schemas/Bundle'
            application/fhir+xml:
              schema:
                $ref: '#/components/schemas/Bundle'
        '400':
          $ref: '#/components/responses/BadRequest'
components:
  schemas:
    Bundle:
      type: object
      description: A container for a collection of resources
      required:
        - resourceType
        - type
      properties:
        resourceType:
          type: string
          enum:
            - Bundle
          description: Resource type
        type:
          type: string
          enum:
            - document
            - message
            - transaction
            - transaction-response
            - batch
            - batch-response
            - history
            - searchset
            - collection
          description: Indicates the purpose of this bundle
        total:
          type: integer
          description: If search, the total number of matches
        link:
          type: array
          items:
            $ref: '#/components/schemas/BundleLink'
          description: Links related to this Bundle
        entry:
          type: array
          items:
            $ref: '#/components/schemas/BundleEntry'
          description: Entry in the bundle
    BundleLink:
      type: object
      description: A link to another resource
      properties:
        relation:
          type: string
          description: >-
            See
            http://www.iana.org/assignments/link-relations/link-relations.xhtml#link-relations-1
        url:
          type: string
          format: uri
          description: Reference details for the link
    BundleEntry:
      type: object
      description: An entry in a bundle resource
      properties:
        fullUrl:
          type: string
          format: uri
          description: URI for resource
        resource:
          $ref: '#/components/schemas/Resource'
        request:
          $ref: '#/components/schemas/BundleEntryRequest'
        response:
          $ref: '#/components/schemas/BundleEntryResponse'
    OperationOutcome:
      type: object
      description: >-
        A collection of error, warning or information messages that result from
        a system action
      required:
        - resourceType
      properties:
        resourceType:
          type: string
          enum:
            - OperationOutcome
          description: Resource type
        issue:
          type: array
          items:
            $ref: '#/components/schemas/OperationOutcomeIssue'
          description: A list of issues associated with the operation
    Resource:
      type: object
      description: A FHIR resource. All resources have resourceType, id, and meta fields.
      required:
        - resourceType
      properties:
        resourceType:
          type: string
          description: The type of resource
        id:
          type: string
          description: Logical id of this artifact
        meta:
          $ref: '#/components/schemas/Meta'
      additionalProperties: true
    BundleEntryRequest:
      type: object
      description: Additional information about how this entry should be processed
      properties:
        method:
          type: string
          enum:
            - GET
            - POST
            - PUT
            - PATCH
            - DELETE
          description: HTTP verb for the operation
        url:
          type: string
          description: URL for HTTP equivalent of this entry
    BundleEntryResponse:
      type: object
      description: Indicates the results of processing the corresponding 'request' entry
      properties:
        status:
          type: string
          description: Status response code
        location:
          type: string
          description: The location (if the operation returns a location)
        etag:
          type: string
          description: The etag for the resource (if relevant)
        lastModified:
          type: string
          format: date-time
          description: Server's date time modified
        outcome:
          $ref: '#/components/schemas/OperationOutcome'
    OperationOutcomeIssue:
      type: object
      description: An issue associated with the operation
      required:
        - severity
        - code
      properties:
        severity:
          type: string
          enum:
            - fatal
            - error
            - warning
            - information
          description: Severity of the issue
        code:
          type: string
          enum:
            - invalid
            - structure
            - required
            - value
            - invariant
            - security
            - login
            - unknown
            - expired
            - forbidden
            - suppressed
            - processing
            - not-supported
            - duplicate
            - multiple-matches
            - not-found
            - deleted
            - too-long
            - code-invalid
            - extension
            - too-costly
            - business-rule
            - conflict
            - transient
            - lock-error
            - no-store
            - exception
            - timeout
            - incomplete
            - throttled
            - informational
          description: Error or warning code
        details:
          $ref: '#/components/schemas/CodeableConcept'
        diagnostics:
          type: string
          description: Additional diagnostic information about the issue
        location:
          type: array
          items:
            type: string
          description: XPath or JSONPath expression describing the location of the issue
    Meta:
      type: object
      description: Metadata about a resource
      properties:
        versionId:
          type: string
          description: Version specific identifier
        lastUpdated:
          type: string
          format: date-time
          description: When the resource version last changed
        profile:
          type: array
          items:
            type: string
            format: uri
          description: Profiles this resource claims to conform to
        tag:
          type: array
          items:
            $ref: '#/components/schemas/Coding'
          description: Tags applied to this resource
        security:
          type: array
          items:
            $ref: '#/components/schemas/Coding'
          description: Security labels applied to this resource
    CodeableConcept:
      type: object
      description: >-
        A concept that may be defined by a formal reference to a terminology or
        ontology
      properties:
        coding:
          type: array
          items:
            $ref: '#/components/schemas/Coding'
        text:
          type: string
          description: Plain text representation of the concept
    Coding:
      type: object
      description: A reference to a code defined by a terminology system
      properties:
        system:
          type: string
          format: uri
          description: Identity of the terminology system
        version:
          type: string
          description: Version of the system - if relevant
        code:
          type: string
          description: Symbol in syntax defined by the system
        display:
          type: string
          description: Representation defined by the system
  responses:
    BadRequest:
      description: Bad request - validation error or invalid input
      content:
        application/fhir+json:
          schema:
            $ref: '#/components/schemas/OperationOutcome'
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
      description: Bearer token authentication

````