Explore documentation

Bulk Export

Exporting your organization's data asynchronously with $export

Set up the SDK client as esus. Use the export ID as exportId when polling status or deleting the export.

Bulk Export prepares your organization’s data asynchronously. You get a manifest and can download one NDJSON file per resource type, with one FHIR R4 resource per line. The Esus CapabilityStatement does not advertise this operation.

Access: for users, the admin and data-export roles can use it. Only admin can delete an export. API keys need BulkExport.read to start, poll, and download (they are all GET requests) and BulkExport.delete to delete. SMART on FHIR tokens cannot call these routes.

How It Works

  1. Kick off with GET /fhir/$export. You receive 202 Accepted and a Content-Location header.
  2. Poll that location until it returns 200 OK with a manifest.
  3. Download each file with the same credential. The manifest URL streams the NDJSON.
  4. Delete the export when you are done. This also removes its files from storage.

Exports run in the background, one at a time across the platform, so a queued export can stay in-progress for a while before it starts. Each type is read in pages of 1,000 rows and streamed to storage, which bounds the worker’s memory use.

Kick Off an Export

REST
curl -i "https://api.esus.health/fhir/\$export" \
  -H "Authorization: Bearer TOKEN"
SDK
const result = await esus.fhir.operation("GET /fhir/$export", {});
const location = result.headers.get("content-location");

Response: 202 Accepted with an empty body:

Content-Location: /bulk-export/aa11bb22-cc33-dd44-ee55-ff6677889900

The Content-Location is a path relative to the API host. The Accept and Prefer: respond-async headers from the spec are not required.

Query parameters

ParameterDescription
_typeComma-separated resource types, e.g. Patient,Observation,Condition. If omitted, all supported types are exported.
_sinceISO 8601 instant. Only rows updated at or after it are exported. An invalid value returns 400.
REST
curl -i "https://api.esus.health/fhir/\$export?_type=Patient,Observation&_since=2024-01-01T00:00:00Z" \
  -H "Authorization: Bearer TOKEN"
SDK
const result = await esus.fhir.operation("GET /fhir/$export", {
  query: { _type: "Patient,Observation", _since: "2024-01-01T00:00:00Z" },
});
const location = result.headers.get("content-location");

You can request types from the Esus FHIR catalog. See GET /fhir/metadata or the SDK’s fhirResources array. An unknown type in _type returns 400.

Patient-level export

GET /fhir/Patient/$export accepts the same parameters, but it currently exports the same data as the system-level export: there is no patient-compartment filtering yet. Sending the patient parameter returns 422 (not-supported).

Poll the Status

REST
curl -i https://api.esus.health/bulk-export/aa11bb22-cc33-dd44-ee55-ff6677889900 \
  -H "Authorization: Bearer TOKEN"
SDK
const result = await esus.fhir.operation("GET /bulk-export/:id", {
  params: { id: exportId },
});
if (result.status === 200) console.log(result.body.output);

In progress: 202 Accepted, with an X-Progress header carrying the same text:

{ "status": "in-progress", "progress": "Export is being processed" }

Failed: 500

{
  "resourceType": "OperationOutcome",
  "issue": [{
    "severity": "error",
    "code": "exception",
    "diagnostics": "Export failed. Contact support if the issue persists."
  }]
}

Complete: 200 OK, with the manifest:

{
  "transactionTime": "2024-01-15T10:30:00.000Z",
  "request": "/$export",
  "requiresAccessToken": true,
  "output": [
    {
      "type": "Patient",
      "url": "/bulk-export/aa11bb22-cc33-dd44-ee55-ff6677889900/download/Patient",
      "count": 1240
    },
    {
      "type": "Observation",
      "url": "/bulk-export/aa11bb22-cc33-dd44-ee55-ff6677889900/download/Observation",
      "count": 5820
    }
  ],
  "error": []
}

Types with no matching rows are left out of output. An unknown or foreign export id returns 404.

Download the files

Each output[].url streams the file with Content-Type: application/fhir+ndjson. Send your Esus credential with that request.

REST
curl --fail-with-body \
  "https://api.esus.health/bulk-export/aa11bb22-cc33-dd44-ee55-ff6677889900/download/Patient" \
  -H "Authorization: Bearer TOKEN" \
  --output patients.ndjson
SDK
const { body: ndjson } = await esus.fhir.operation("GET /bulk-export/:id/download/:type", {
  params: { id: exportId, type: "Patient" },
});

Consume or cancel the stream returned by the SDK.

The limit is 4 downloads per minute per export file. If you exceed it, you receive 429. If the download limiter is unavailable, the API returns 503. A job that is still running, a type absent from the manifest, or a missing file returns 404.

File contents

Each line contains a serialized FHIR resource, ordered by id. Clinical fields use the same public shape as FHIR reads. Handle the files as sensitive clinical data. Partial failures appear in error and can be downloaded as NDJSON of OperationOutcome records.

Delete an Export

REST
curl -X DELETE https://api.esus.health/bulk-export/aa11bb22-cc33-dd44-ee55-ff6677889900 \
  -H "Authorization: Bearer TOKEN"
SDK
await esus.fhir.operation("DELETE /bulk-export/:id", {
  params: { id: exportId },
});

Returns 204 No Content. The NDJSON files are deleted from storage first. If that fails, the export is kept so you can retry the DELETE.

Processing NDJSON

Node.js

import { createReadStream } from 'fs'
import { createInterface } from 'readline'

const rl = createInterface({ input: createReadStream('patients.ndjson') })

rl.on('line', (line) => {
  const row = JSON.parse(line)
  console.log(row.resourceType, row.id, row.updated_at)
})

Python

import json

with open('patients.ndjson') as f:
    for line in f:
        row = json.loads(line)
        print(row['resourceType'], row['id'], row.get('updated_at'))

Use Cases

Use caseRequest
Full snapshot$export without filters
Incremental sync$export?_since=<last transactionTime>
Subset of types$export?_type=Patient,Observation,Condition