SFTP file to FHIR
Pick up patient CSV files from an SFTP folder and create a FHIR Patient for each row, once per MRN.
On this page
This guide builds a workflow that picks up patient CSV files from an SFTP folder and creates a FHIR R4 Patient on a FHIR server for each row. It uses the built-in SFTP File Import to FHIR template. The template's trigger is an SFTP pickup: it checks the folder every 5 minutes, runs once per CSV row, and moves each file to a processed folder once its rows are recorded, so a file is never read twice.
Here is the flow.
sequenceDiagram
participant SFTP as SFTP server
participant Engine as MedHook engine
participant FHIR as FHIR server (R4)
Engine->>SFTP: List /incoming/patients (*.csv)
SFTP-->>Engine: patients.csv
Engine->>Engine: One run per row: row → Patient
Engine->>FHIR: POST transaction (Patient, ifNoneExist on the MRN)
FHIR-->>Engine: 201 Created (or 200: already there)
Engine->>SFTP: Move patients.csv to processed/Before you start
- A running engine and a signed-in user. See the Quick start.
- An SFTP server the engine can reach, with a test account and a folder to read.
- A FHIR R4 server the engine can reach. See step 1 of HL7v2 over MLLP to FHIR R4 for its profile.
The file is a CSV with a header row. This synthetic example is used throughout:
mrn,first_name,last_name,dob,gender
MRN0001,SYNTHETIC,TESTPATIENT,1980-01-01,female
MRN0002,SYNTHETIC,TESTPATIENT-TWO,1975-06-30,maleSteps
1. Create the SFTP profile
Open Adapters, choose New connection and pick SFTP. Fill in the following. Settings that aren't in the short form are under More settings.
| Field | Value |
|---|---|
| Name | Test SFTP |
host | sftp.example.test |
port | 22 |
username | testuser |
authType | password or privatekey |
password | the password, if authType is password |
hostKeyFingerprints | the server's key, as SHA256:... (one per line, see below) |
For privatekey, set privateKey and, if the key has one, passphrase. Secret fields are write-only: the engine encrypts them and never returns them.
The engine checks the server's host key on every connection. Get the fingerprint from a machine you trust and paste it into hostKeyFingerprints:
ssh-keyscan sftp.example.test | ssh-keygen -lf -You can paste known_hosts lines into knownHosts instead. Choose Test connection, then Save connection. A failed test shows the fingerprint the server presented, so you can compare it with the one you expect.
Uploads write a hidden .part file and rename it onto the final name (uploadMode: temp-rename, the default), so a reader never sees a half-written file. createDirs creates a missing folder before an upload, and operationTimeout (default 5 minutes) bounds each upload, download, list or delete.
Create the FHIR REST profile the same way if you have not already. Name it Test FHIR server.
2. Start from the template
Open Workflows, choose Templates, choose SFTP File Import to FHIR. Under Connections it uses, choose Test SFTP for SFTP pickup (/incoming/patients) and Test FHIR server for Destination FHIR server, then choose Use this template. The Steps list shows:
- Source, an SFTP pickup of
/incoming/patients/*.csv, one run per row. - Destination FHIR server, with a JavaScript output that turns the row into a Patient.
3. Check the pickup and the row script
Select Source. The pickup reads /incoming/patients every 5 minutes, takes files named *.csv, and runs once per row. After a file's rows are recorded it moves to /incoming/patients/processed, or to /incoming/patients/error when it can't be read as CSV. Change the folders to yours. See Triggers: SFTP pickup for file age, size limits and the CSV settings.
Select Destination FHIR server. Its output reads the row (input.row) and builds a transaction Bundle with one Patient:
| Column | Patient field |
|---|---|
mrn | identifier[0] (with MRN_SYSTEM) |
first_name | name[0].given[0] |
last_name | name[0].family |
dob | birthDate |
gender | gender (F and female both work) |
Set MRN_SYSTEM at the top of the script to the identifier system of your MRNs. The Patient is a conditional create (ifNoneExist on that identifier), so a row sent again creates no second Patient. A row with no mrn fails its run, which then waits on the run list.
4. Test it
Choose Run test. The saved sample is one row, so the test shows the Bundle the destination would send. It does not read the folder: a test run calls no adapters.
Deploy the workflow (see Save and deploy). Then put the file in the folder:
printf 'put patients.csv /incoming/patients/patients.csv\n' | sftp -b - testuser@sftp.example.testWithin 5 minutes there are two runs, one per row, and the file is in processed/. Check the FHIR server:
curl -s "https://fhir.example.test/R4/Patient?identifier=MRN0001" \
-H "Authorization: Bearer $FHIR_TOKEN"The result is a searchset Bundle with one Patient named TESTPATIENT, born 1980-01-01. Put the same file again and the runs change nothing.
🎉 Each CSV that lands in the folder now becomes Patients on the FHIR server. For a file in a different format, see Concepts: transforms for the CDA and X12 parsers. For a folder on FTP, SMB, WebDAV, S3 or a mounted volume, pick the file trigger instead. See Triggers: file pickup.
Troubleshooting
- The pickup fails with an authentication error. Check
username,authTypeand the secret on the profile, then choose Test connection. Secrets cannot be read back, so choose Replace on the field and enter it again. - The pickup fails with
SFTP host key not trusted: server presented SHA256:.... The profile has no pinned key, or the server's key changed. Check the presented fingerprint againstssh-keyscanfrom a trusted machine, then add it tohostKeyFingerprints. - Nothing runs. The folder or the file name pattern is wrong, or the file is still being written. List the folder with
sftpto check. A file that isn't CSV moves toerror/. - The Patient is created with empty fields. The header row does not match the script. It reads
mrn,first_name,last_name,dobandgender. Rename the columns or edit the script. - The step shows
CHOOSE PROFILEorNO PROFILE, or Save is blocked. Choose an SFTP profile on the pickup and a FHIR REST profile on the destination. - The FHIR server answers
4xx. It rejected the Patient. The error is in the execution's last step. Fix the mapped fields, for example an invalidgendervalue, and run again.