SQL on FHIR
            
            2.1.0-pre - release
  
            
          
SQL on FHIR - Local Development build (v2.1.0-pre) built by the FHIR (HL7® FHIR® Standard) Build Tools. See the Directory of published versions
| Official URL: http://sql-on-fhir.org/OperationDefinition/$viewdefinition-export | Version: 2.1.0-pre | |||
| Active as of 2025-10-20 | Computable Name: ViewDefinitionExport | |||
Export a view definition. User can provide view definition references and/or resources as part of the input parameters.
Overview:
The $viewdefinition-export operation is an asynchronous operation that enables the bulk export of FHIR data that has been transformed using ViewDefinitions. Multiple ViewDefinitions can be exported in a single operation, allowing efficient batch processing of related views. Exported data can be written in various formats (CSV, NDJSON, Parquet) and delivered to file storage systems such as Amazon S3, Azure Blob Storage, or a local file system.
Use Cases:
FHIR Versions:
Operation may work in FHIR R4 compatibility mode or in R6 mode.
In R4 mode, operation can only be on system level ( {BaseURl}/$viewdefinition-export ),
for R6 mode operation can appear on type and instance level 
({BaseURl}/ViewDefinition/$viewdefinition-export and {BaseURl}/ViewDefinition/{id}/$viewdefinition-export).
Endpoints:
URL: [base]/$viewdefinition-export
URL: [base]/CanonicalResource/$viewdefinition-export
URL: [base]/CanonicalResource/[id]/$viewdefinition-export
| Use | Name | Scope | Cardinality | Type | Binding | Documentation | 
| IN | clientId | 0..1 | string | The client-supplied identifier for the export. Optional, but can be used for tracking if supported by the server. | ||
| IN | viewReference | type | 0..* | Reference | Reference to an existing view definition on the server. | |
| IN | viewResource | type | 0..* | Resource | An inline view definition resource to export. | |
| IN | view | type | 0..* | Convenience wrapper containing either a view reference, inline resource, or desired output format. | ||
| IN | view.reference | 0..1 | Reference | Reference to the view definition on the server. | ||
| IN | view.resource | 0..1 | Resource | The inline view definition resource. | ||
| IN | view.format | 0..1 | code | Output Format Codes (Extensible) | Optional explicit output format for this specific view. | |
| IN | _format | type | 1..1 | code | Output Format Codes (Extensible) | The requested bulk export format (for example csv, parquet, json). | 
| OUT | clientId | 0..1 | string | Echoed clientId when supplied and supported by the server. | ||
| OUT | exportId | 0..1 | string | Identifier assigned to the export request. | ||
| OUT | location | 1..1 | uri | Polling URL for the export status. | ||
| OUT | status | 1..1 | code | Export Status Codes (Required) | Status of the export (accepted, in-progress, completed, cancelled, failed). | |
| OUT | output | 0..* | Collection of exported artefacts. | |||
| OUT | output.name | 1..1 | string | Human-friendly name of the exported view definition. | ||
| OUT | output.location | 1..1 | uri | Location of the exported file. | ||
| OUT | output.format | 1..1 | code | Output Format Codes (Extensible) | Format of the exported file (for example csv, parquet, json). | 
This operation follows the FHIR Asynchronous Interaction Request Pattern:
Prefer: respond-async header and one or more view parametersNote: This operation uses Parameters resource format instead of Bundle format to:
The operation can export data from:
source parameterOptional filtering parameters:
patient - Export only resources for this patientgroup - Export only resources for this group_since - Export only resources updated since this timePrefer: respond-async (required) - Specifies that the response should be asynchronousAccept (recommended) - Specifies the format of the responseAccept (recommended) - Specifies the format of the response| Name | Type | Min | Max | Description | 
|---|---|---|---|---|
| view | complex | 1 | * | ViewDefinition(s) to export. Can be repeated to export multiple views in a single operation. See ViewDefinition Parameter | 
The view parameter is a complex type that can be repeated multiple times to export several ViewDefinitions in a single operation. Each view parameter has the following parts:
| Name | Type | Min | Max | Description | 
|---|---|---|---|---|
| view | complex | 1 | * | A ViewDefinition to export | 
| view.name | string | 0 | 1 | Name for the export output. If not provided, ViewDefinition name will be used | 
| view.viewReference | Reference | 0¹ | 1 | Reference to ViewDefinition on the server. Details | 
| view.viewResource | ViewDefinition | 0¹ | 1 | Inline ViewDefinition resource | 
¹ Either view.viewReference or view.viewResource is required
| Name | Type | Min | Max | Description | 
|---|---|---|---|---|
| clientTrackingId | string | 0 | 1 | Client-provided tracking ID for the export operation | 
| _format | code | 0 | 1 | Output format: csv,ndjson,parquet,json. Details | 
| Name | Type | Min | Max | Description | 
|---|---|---|---|---|
| patient | Reference | 0 | * | Filter by patient reference. Details | 
| group | Reference | 0 | * | Filter by group membership. Details | 
| _since | instant | 0 | 1 | Export only resources updated since this time. Details | 
| Name | Type | Min | Max | Description | 
|---|---|---|---|---|
| source | string | 0 | 1 | External data source (e.g., URI, bucket name). If absent, uses server data | 
If server does not support a parameter, request should be rejected with 400 Bad Request 
and OperationOutcome resource in the body with clarification that the parameter is not supported.
Server should document which parameters it supports in its CapabilityStatement.
The view.viewReference parameter MAY be specified using any of the following formats:
| A canonical URL (e.g. “http://specification.org/fhir/ViewDefinition/123 | 1.0.0”) | 
Servers MAY choose which reference formats they support. Servers SHALL document which reference formats they support in their CapabilityStatement.
For servers that want to support all types of references, it is recommended to follow the following algorithm:
It is RECOMMENDED to support ‘json’, ‘ndjson’ and ‘csv’ formats by default. Servers may support other formats, but they should be explicitly documented in the CapabilityStatement.
When provided, the server SHALL NOT return resources in the patient compartments belonging to patients outside of this list.
If a client requests patients who are not present on the server,
the server SHOULD return details via a FHIR OperationOutcome resource in an error response to the request.
When provided, the server SHALL NOT return resources that are not a member of the supplied Group.
If a client requests groups that are not present on the server,
the server SHOULD return details via a FHIR OperationOutcome resource in an error response to the request.
Resources will be included in the response if their state has changed after the supplied time 
(e.g., if Resource.meta.lastUpdated is later than the supplied _since time). 
In the case of a Group level export, the server MAY return additional resources modified prior to the supplied time 
if the resources belong to the patient compartment of a patient added to the Group after the supplied time (this behavior SHOULD be clearly documented by the server).
For Patient- and Group-level requests, the server MAY return resources that are referenced by the resources being returned 
regardless of when the referenced resources were last updated. 
For resources where the server does not maintain a last updated time, 
the server MAY include these resources in a response irrespective of the _since value supplied by a client.
| Name | Type | Min | Max | Description | 
|---|---|---|---|---|
| exportId | string | 1 | 1 | Server-generated export ID | 
| clientTrackingId | string | 0 | 1 | Client-provided tracking ID (echoed from input if provided) | 
| Name | Type | Min | Max | Description | 
|---|---|---|---|---|
| status | code | 1 | 1 | The status of the export: accepted,in-progress,completed,cancelled,failed | 
| location | uri | 1 | 1 | The URL to poll for the status of the export | 
| cancelUrl | uri | 0 | 1 | Dedicated URL to cancel the export (alternative to DELETE on location URL) | 
| Name | Type | Min | Max | Description | 
|---|---|---|---|---|
| _format | code | 0 | 1 | The format of the exported files (echoed from input if provided) | 
| exportStartTime | instant | 0 | 1 | When the export operation began | 
| exportEndTime | instant | 0 | 1 | When the export operation completed (only in completed status) | 
| exportDuration | integer | 0 | 1 | The actual duration of the export in seconds (only in completed status) | 
| estimatedTimeRemaining | integer | 0 | 1 | Estimated seconds until completion (only in in-progress status) | 
| Name | Type | Min | Max | Description | 
|---|---|---|---|---|
| output | complex | 0 | * | Output information for each exported view (only in completed status) | 
| output.name | string | 1 | 1 | The name of the exported view. Details | 
| output.location | uri | 1 | * | URL(s) to download the exported file(s). Details | 
The output.name parameter identifies each exported view in the results. The value is determined as follows:
view.name was provided in the input parameters, that value is usedview.name was not provided, the name is taken from the ViewDefinition resource’s name elementThis allows clients to correlate output files with their requested views and provides meaningful filenames for the exported data.
For large exports, servers MAY partition the output into multiple files. When partitioning occurs:
output.location parameter can repeat within a single output entryfilename.part1.parquet, filename.part2.parquet)Example of partitioned output:
{
  "name": "output",
  "part": [
    {
      "name": "name",
      "valueString": "patient_demographics"
    },
    {
      "name": "location",
      "valueUri": "https://example.com/export/123/patient_demographics.part1.parquet"
    },
    {
      "name": "location",
      "valueUri": "https://example.com/export/123/patient_demographics.part2.parquet"
    },
    {
      "name": "location",
      "valueUri": "https://example.com/export/123/patient_demographics.part3.parquet"
    }
  ]
}
Clients MUST download all parts to obtain the complete dataset for a view.
The $export operation uses standard HTTP status codes to indicate the outcome:
| Status Code | Description | When to Use | 
|---|---|---|
| 202 Accepted | Success | Export request accepted, poll for status | 
| 400 Bad Request | Client Error | Invalid parameters, unsupported parameters, missing required headers | 
| 404 Not Found | Not Found | ViewDefinition resource not found | 
| 422 Unprocessable Entity | Business Logic Error | Valid request but ViewDefinition is invalid or cannot be processed | 
| 500 Internal Server Error | Server Error | Unexpected server error | 
All error responses (4xx and 5xx) SHOULD include an OperationOutcome resource providing details about the error.
When the server does not support certain parameters, it returns 400 Bad Request:
HTTP/1.1 400 Bad Request
Content-Type: application/fhir+json
{
  "resourceType": "OperationOutcome",
  "issue": [
    {
      "severity": "error",
      "code": "not-supported",
      "diagnostics": "The server does not support the 'source' parameter"
    }
  ]
}
When a provided ViewDefinition is invalid:
HTTP/1.1 422 Unprocessable Entity
Content-Type: application/fhir+json
{
  "resourceType": "OperationOutcome",
  "issue": [
    {
      "severity": "error",
      "code": "invalid",
      "diagnostics": "The ViewDefinition 'patient_summary' is invalid: column 'age' contains invalid FHIRPath expression",
      "expression": ["parameter[0].part[1].resource.select[0].column[1].path"]
    }
  ]
}
When a referenced ViewDefinition does not exist:
HTTP/1.1 404 Not Found
Content-Type: application/fhir+json
{
  "resourceType": "OperationOutcome",
  "issue": [
    {
      "severity": "error",
      "code": "not-found",
      "diagnostics": "ViewDefinition with reference 'ViewDefinition/non-existent' not found"
    }
  ]
}
When filtering by patient or group that doesn’t exist:
HTTP/1.1 404 Not Found
Content-Type: application/fhir+json
{
  "resourceType": "OperationOutcome",
  "issue": [
    {
      "severity": "error",
      "code": "not-found",
      "diagnostics": "Patient with reference 'Patient/12345' not found"
    }
  ]
}
For group references:
HTTP/1.1 404 Not Found
Content-Type: application/fhir+json
{
  "resourceType": "OperationOutcome",
  "issue": [
    {
      "severity": "error",
      "code": "not-found",
      "diagnostics": "Group with reference 'Group/diabetes-cohort' not found"
    }
  ]
}
When processing multiple ViewDefinitions, servers SHOULD validate all of them before starting the export:
HTTP/1.1 400 Bad Request
Content-Type: application/fhir+json
{
  "resourceType": "OperationOutcome",
  "issue": [
    {
      "severity": "error",
      "code": "not-found",
      "diagnostics": "ViewDefinition 'patient-vitals' not found",
      "expression": ["parameter[1]"]
    },
    {
      "severity": "error",
      "code": "invalid",
      "diagnostics": "ViewDefinition 'lab-results' contains invalid resource type",
      "expression": ["parameter[2]"]
    }
  ]
}
POST ViewDefinition/$export with Prefer: respond-async header.202 Accepted status codeContent-Location header with the absolute URL for subsequent status requests (polling location)status parameter set to accepted and location parameter400 Bad Request and OperationOutcome resource in the body.202 Accepted with Parameters resource containing status parameter set to in-progressX-Progress header to indicate completion percentageRetry-After header to indicate when to retryoutput parameter200 OK status codestatus parameter set to completedoutput parameter containing the results of the export202 Accepted status code (during polling)status parameter set to failedCancellation (Optional): Servers MAY support export cancellation using one of two approaches:
Option 1: DELETE on location URL
DELETE request to the status polling URL (from location parameter)DELETE /fhir/export/{exportId}/statusOption 2: Separate cancel URL
cancelUrl parameter in responsesDELETE request to the provided cancel URLDELETE /fhir/export/{exportId}/cancelAfter successful cancellation:
404 Not Foundoutput.location parameters.This example demonstrates the full lifecycle of an export operation from initiation through completion.
Step 1: Kick-off Request
Client initiates export of two ViewDefinitions with patient filtering:
POST /ViewDefinition/$export HTTP/1.1
Host: example.com
Content-Type: application/fhir+json
Prefer: respond-async
Accept: application/fhir+json
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGc...
{
  "resourceType": "Parameters",
  "parameter": [
    {
      "name": "clientTrackingId",
      "valueString": "monthly-report-2024-01"
    },
    {
      "name": "view",
      "part": [
        {
          "name": "name",
          "valueString": "demographics_summary"
        },
        {
          "name": "viewReference",
          "valueReference": {
            "reference": "ViewDefinition/patient-demographics-v2"
          }
        }
      ]
    },
    {
      "name": "view",
      "part": [
        {
          "name": "viewResource",
          "resource": {
            "resourceType": "ViewDefinition",
            "name": "active_medications",
            "resource": "MedicationRequest",
            "select": [
              {
                "column": [
                  {
                    "path": "id",
                    "name": "medication_id"
                  },
                  {
                    "path": "medication.concept.coding[0].display",
                    "name": "medication_name"
                  },
                  {
                    "path": "authoredOn",
                    "name": "prescribed_date"
                  },
                  {
                    "path": "subject.reference",
                    "name": "patient_ref"
                  }
                ]
              }
            ],
            "where": [
              {
                "path": "status",
                "op": "=",
                "value": "active"
              }
            ]
          }
        }
      ]
    },
    {
      "name": "patient",
      "valueReference": {
        "reference": "Patient/cohort-123"
      }
    },
    {
      "name": "_since",
      "valueInstant": "2024-01-01T00:00:00Z"
    },
    {
      "name": "_format",
      "valueCode": "parquet"
    }
  ]
}
Step 2: Kick-off Response
Server accepts the request and provides polling location:
HTTP/1.1 202 Accepted
Content-Location: https://example.com/fhir/export/550e8400-e29b-41d4-a716-446655440000/status
Content-Type: application/fhir+json
{
  "resourceType": "Parameters",
  "parameter": [
    {
      "name": "exportId",
      "valueString": "550e8400-e29b-41d4-a716-446655440000"
    },
    {
      "name": "clientTrackingId",
      "valueString": "monthly-report-2024-01"
    },
    {
      "name": "status",
      "valueCode": "accepted"
    },
    {
      "name": "location",
      "valueUri": "https://example.com/fhir/export/550e8400-e29b-41d4-a716-446655440000/status"
    }
  ]
}
Step 3: First Status Poll (Starting)
Client polls immediately:
GET /fhir/export/550e8400-e29b-41d4-a716-446655440000/status HTTP/1.1
Host: example.com
Accept: application/fhir+json
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGc...
Response shows export is starting:
HTTP/1.1 202 Accepted
Content-Type: application/fhir+json
Retry-After: 5
X-Progress: 0%
{
  "resourceType": "Parameters",
  "parameter": [
    {
      "name": "exportId",
      "valueString": "550e8400-e29b-41d4-a716-446655440000"
    },
    {
      "name": "clientTrackingId",
      "valueString": "monthly-report-2024-01"
    },
    {
      "name": "status",
      "valueCode": "in-progress"
    },
    {
      "name": "location",
      "valueUri": "https://example.com/fhir/export/550e8400-e29b-41d4-a716-446655440000/status"
    },
    {
      "name": "cancelUrl",
      "valueUri": "https://example.com/fhir/export/550e8400-e29b-41d4-a716-446655440000"
    },
    {
      "name": "exportStartTime",
      "valueInstant": "2024-01-15T14:30:00Z"
    }
  ]
}
Step 4: Second Status Poll (In Progress)
After 5 seconds, client polls again:
GET /fhir/export/550e8400-e29b-41d4-a716-446655440000/status HTTP/1.1
Host: example.com
Accept: application/fhir+json
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGc...
Response shows progress:
HTTP/1.1 202 Accepted
Content-Type: application/fhir+json
Retry-After: 10
X-Progress: 65%
{
  "resourceType": "Parameters",
  "parameter": [
    {
      "name": "exportId",
      "valueString": "550e8400-e29b-41d4-a716-446655440000"
    },
    {
      "name": "clientTrackingId",
      "valueString": "monthly-report-2024-01"
    },
    {
      "name": "status",
      "valueCode": "in-progress"
    },
    {
      "name": "location",
      "valueUri": "https://example.com/fhir/export/550e8400-e29b-41d4-a716-446655440000/status"
    },
    {
      "name": "cancelUrl",
      "valueUri": "https://example.com/fhir/export/550e8400-e29b-41d4-a716-446655440000"
    },
    {
      "name": "exportStartTime",
      "valueInstant": "2024-01-15T14:30:00Z"
    },
    {
      "name": "estimatedTimeRemaining",
      "valueInteger": 25
    }
  ]
}
Step 5: Final Status Poll (Completed)
After another 10 seconds:
GET /fhir/export/550e8400-e29b-41d4-a716-446655440000/status HTTP/1.1
Host: example.com
Accept: application/fhir+json
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGc...
Response shows completion with download URLs:
HTTP/1.1 200 OK
Content-Type: application/fhir+json
Expires: Tue, 16 Jan 2024 14:30:42 GMT
{
  "resourceType": "Parameters",
  "parameter": [
    {
      "name": "exportId",
      "valueString": "550e8400-e29b-41d4-a716-446655440000"
    },
    {
      "name": "clientTrackingId",
      "valueString": "monthly-report-2024-01"
    },
    {
      "name": "status",
      "valueCode": "completed"
    },
    {
      "name": "_format",
      "valueCode": "parquet"
    },
    {
      "name": "exportStartTime",
      "valueInstant": "2024-01-15T14:30:00Z"
    },
    {
      "name": "exportEndTime",
      "valueInstant": "2024-01-15T14:30:42Z"
    },
    {
      "name": "exportDuration",
      "valueInteger": 42
    },
    {
      "name": "output",
      "part": [
        {
          "name": "name",
          "valueString": "demographics_summary"
        },
        {
          "name": "location",
          "valueUri": "https://example.com/fhir/export/550e8400-e29b-41d4-a716-446655440000/demographics_summary.parquet"
        }
      ]
    },
    {
      "name": "output",
      "part": [
        {
          "name": "name",
          "valueString": "active_medications"
        },
        {
          "name": "location",
          "valueUri": "https://example.com/fhir/export/550e8400-e29b-41d4-a716-446655440000/active_medications.part1.parquet"
        },
        {
          "name": "location",
          "valueUri": "https://example.com/fhir/export/550e8400-e29b-41d4-a716-446655440000/active_medications.part2.parquet"
        }
      ]
    }
  ]
}
Step 6: Download Files
Client downloads each file:
GET /fhir/export/550e8400-e29b-41d4-a716-446655440000/demographics_summary.parquet HTTP/1.1
Host: example.com
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGc...
HTTP/1.1 200 OK
Content-Type: application/octet-stream
Content-Disposition: attachment; filename="demographics_summary.parquet"
Content-Length: 1048576
[Binary parquet file content]