FHIR
Query HL7 FHIR R4 REST servers from SQL: patients, observations, generic search, reads by id, and server capabilities
- Connector software
- Free and open source
- Service credentials
- Not needed
- Query Farm hosting
- Available · $10/month organization minimum
Use with an agent
Install the Orchard skill in your project to help your agent find connectors, configure them, and verify SQL results.
npx skills add Query-farm/vgi-skills --skill orchard
Copy this request into your coding agent to get started.
Use https://query.farm/products/orchard/connectors/fhir/index.md to help me query FHIR with DuckDB or Haybarn 2.0.1 and VGI protocol 3.4. Ask only for missing details about the result I need or whether I want local execution, self-hosting or Query Farm hosting. Check the connector's actual hosting availability, credentials and input requirements. Read one format of each needed reference and the exact object declarations before writing SQL. Use a released connector, and distinguish a saved example result from anything you actually execute. If you cannot run the query, tell me what remains to verify.
Read body-weight observations from a public test server
Uses synthetic health records from a public FHIR test server.
Example output · captured
- Connector release
- v0.5.0
- Engine
- 2.0.1
- VGI protocol
- 3.4.0
SELECT id, value, unit, effective
FROM fhir.main.fhir_observations('https://hapi.fhir.org/baseR4', query := 'code=29463-7')
LIMIT 20;
| id | value | unit | effective |
|---|---|---|---|
| nhanes-112200-29463-7 | 120.1 | kg | 2026-01-15 09:00 |
| nhanes-120300-29463-7 | 100 | kg | 2026-01-15 09:00 |
| nhanes-118520-29463-7 | 69.9 | kg | 2026-01-15 09:00 |
| nhanes-112787-29463-7 | 97.4 | kg | 2026-01-15 09:00 |
| nhanes-122534-29463-7 | 83.2 | kg | 2026-01-15 09:00 |
Quick start
Choose where to run FHIR, then follow the steps below.
The connector runs alongside DuckDB or Haybarn. No Query Farm account is needed.
1. Install the vgi extension
Run this in your SQL client. On DuckDB, keep FROM query_farm on LOAD in later sessions.
SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;
INSTALL vgi FROM community;
LOAD vgi;
2. Attach the connector
Attach the release directly
Downloads the latest release for your platform and caches it locally.
ATTACH 'fhir' AS fhir (
TYPE vgi,
LOCATION 'github-auto://Query-farm/vgi-fhir'
);
Attach the signed image
Needs Docker, Podman or another OCI runtime. The extension runs the container and talks to it over stdio.
ATTACH 'fhir' AS fhir (
TYPE vgi,
LOCATION 'oci://ghcr.io/query-farm/vgi-fhir:0.5.0'
);
3. Run a query
SELECT id, value, unit, effective
FROM fhir.main.fhir_observations('https://hapi.fhir.org/baseR4', query := 'code=29463-7')
LIMIT 20;
Run the connector on a server you manage. You operate the service and choose who can connect.
1. Deploy the connector on your server
Follow the connector’s deployment instructions. Use its supported HTTP transport, or launch it remotely over SSH. The connection guide explains both options.
2. Install the vgi extension
Run this in your SQL client. On DuckDB, keep FROM query_farm on LOAD in later sessions.
SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;
INSTALL vgi FROM community;
LOAD vgi;
3. Attach the connector
For an HTTP deployment, replace the example URL with your endpoint. Add the authentication options your deployment requires. For SSH, use the SSH connection instructions.
ATTACH 'fhir' AS fhir
(TYPE vgi, LOCATION 'https://your-connector.example.com');
4. Run a query
SELECT id, value, unit, effective
FROM fhir.main.fhir_observations('https://hapi.fhir.org/baseR4', query := 'code=29463-7')
LIMIT 20;
Query Farm operates FHIR for your team. Hosting has a $10 monthly minimum per organization; usage counts toward it. From $2.97 per instance-month ($10 monthly minimum).
1. Create your hosted connector
Open the console, choose your organization and deployment size, and add any service credentials there.
2. Install the vgi extension
SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;
INSTALL vgi FROM community;
LOAD vgi;
3. Attach your deployment
Copy the ATTACH statement from your deployment in the console and run it in DuckDB or Haybarn. Use fhir as the catalog alias for the query below.
4. Run a query
SELECT id, value, unit, effective
FROM fhir.main.fhir_observations('https://hapi.fhir.org/baseR4', query := 'code=29463-7')
LIMIT 20;
Technical details
- Verified connector release
- v0.5.0
- Engine
- 2.0.1
- VGI protocol
- 3.4.0
- Verified on
- December 1, 2026
Pin a version for production
When running the connector yourself, keep a specific release until you’re ready to upgrade. Replace the ATTACH statement above with this one and follow the same extension and credential setup.
Attach the release directly
Uses the verified release each time you attach the connector.
ATTACH 'fhir' AS fhir (
TYPE vgi,
LOCATION 'github-auto://Query-farm/[email protected]'
);
Attach the signed image
Needs Docker, Podman or another OCI runtime. The extension runs the container and talks to it over stdio.
ATTACH 'fhir' AS fhir (
TYPE vgi,
LOCATION 'oci://ghcr.io/query-farm/vgi-fhir:0.5.0'
);
Overview
Query HL7 FHIR R4 REST servers and return resources as rows

Query live HL7 FHIR R4 healthcare APIs directly from SQL — turn any FHIR REST server into DuckDB tables and run analytics over patients, clinical observations, and server capabilities without writing a line of integration code.
The fhir catalog connects to any HL7 FHIR R4 endpoint over plain REST/JSON and streams the resources back as DuckDB rows over Apache Arrow. FHIR (Fast Healthcare Interoperability Resources) is the modern HL7 standard that powers EHR, EMR, and health-data interoperability across hospitals, payers, and digital-health platforms. This connector is for analysts, data engineers, and clinical-informatics teams who want to explore, validate, or extract FHIR data with familiar SQL instead of bespoke API clients — point a function at a service base URL (for example https://hapi.fhir.org/baseR4) and start querying.
Under the hood the worker speaks the FHIR R4 RESTful API using nothing but the Go standard library, so it stays light and dependency-free while remaining faithful to the published FHIR R4 specification (build source at HL7/fhir on GitHub). It follows FHIR search Bundle pagination automatically — every link[].relation == "next" page is fetched up to a safety cap — so a single query transparently spans many result pages. An optional OAuth bearer token argument unlocks protected endpoints, and non-2xx responses (including FHIR OperationOutcome bodies) surface as clean SQL errors rather than panics.
The connector turns a FHIR endpoint's core capabilities into SQL: pull clinical resources with their most-used fields flattened into typed columns, run a generic search across any resource type (Condition, Encounter, MedicationRequest, …) with an optional raw query string and _count page size, read an individual resource by its logical id, and discover which resource types and REST interactions a server supports from its CapabilityStatement. Flattened results always carry the untouched resource JSON alongside the typed columns, so nothing is lost — anything not surfaced as a column is still reachable with json_extract. Built on the vgi-go SDK.
Not sure which endpoint to point at? Browse the built-in fhir_servers directory view for a curated list of public FHIR R4 base URLs, then hand one to any fhir_* function. Worked, runnable statements ship as each function's examples.
Catalog
Everything fhir exposes once attached — read straight from the worker's
released build.
main 1 view · 5 functions
FHIR R4 query functions: patients, observations, generic search, single-resource read, and server capabilities.
Schema examples (9)
List the curated public FHIR R4 test servers and whether each needs authentication.
SELECT name, base_url, requires_auth
FROM fhir.main.fhir_servers
ORDER BY name;
List Patients with their core demographic fields flattened into columns.
SELECT id, family, given, gender, birth_date
FROM fhir.main.fhir_patients('https://r4.smarthealthit.org')
LIMIT 20;
List numeric Observations with their code, value, and unit.
SELECT code, code_display, value, unit
FROM fhir.main.fhir_observations('https://r4.smarthealthit.org')
WHERE value IS NOT NULL
LIMIT 20;
List the ids of Condition resources on a FHIR R4 server.
SELECT id
FROM fhir.main.fhir_search('https://r4.smarthealthit.org', 'Condition')
LIMIT 20;
5 more examples
Read a single Patient resource by logical id and return its full JSON.
SELECT resource
FROM fhir.main.fhir_read('https://r4.smarthealthit.org', 'Patient', '45a69bf0-7116-4f9f-a1d9-29700439b4a4');
Expand a server's supported REST interactions to one row per (resource type, interaction).
SELECT resource_type, UNNEST(interactions) AS interaction
FROM fhir.main.fhir_capabilities('https://r4.smarthealthit.org');
Track one patient's body-weight trend: join their Body Weight Observations (LOINC 29463-7) to fhir_patients on the subject reference to label each row with the patient's name, and compute the change in kg since the previous measurement.
SELECT p.given, p.family, o.effective[:10] AS measured_on,
round(o.value, 1) AS weight_kg,
round(o.value - lag(o.value) OVER (ORDER BY o.effective), 1) AS change_kg
FROM fhir.main.fhir_observations('https://r4.smarthealthit.org', query := 'patient=45a69bf0-7116-4f9f-a1d9-29700439b4a4&code=29463-7') AS o
JOIN fhir.main.fhir_patients('https://r4.smarthealthit.org', query := '_id=45a69bf0-7116-4f9f-a1d9-29700439b4a4') AS p ON o.subject = 'Patient/' || p.id
ORDER BY o.effective;
Build one patient's problem list: search their Condition resources with fhir_search, join to fhir_patients on the subject reference, and report each condition's clinical status, onset date, and the patient's age in whole years at onset.
SELECT p.given, p.family,
json_extract_string(c.resource, '$.code.text') AS condition_name,
json_extract_string(c.resource, '$.clinicalStatus.coding[0].code') AS clinical_status,
CAST(json_extract_string(c.resource, '$.onsetDateTime')[:10] AS DATE) AS onset_date,
date_sub('year', CAST(p.birth_date AS DATE), onset_date) AS age_at_onset
FROM fhir.main.fhir_search('https://r4.smarthealthit.org', 'Condition', query := 'patient=45a69bf0-7116-4f9f-a1d9-29700439b4a4') AS c
JOIN fhir.main.fhir_patients('https://r4.smarthealthit.org', query := '_id=45a69bf0-7116-4f9f-a1d9-29700439b4a4') AS p ON json_extract_string(c.resource, '$.subject.reference') = 'Patient/' || p.id
ORDER BY onset_date, condition_name;
Summarise one patient's latest vital signs: read the Patient by id with fhir_read for a display name, join it to their vital-signs Observations, and keep the most recent numeric value and unit per vital sign (components-only vitals such as Blood Pressure have a NULL value and drop out).
WITH pt AS (SELECT json_extract_string(resource, '$.id') AS patient_id, json_extract_string(resource, '$.name[0].given[0]') || ' ' || json_extract_string(resource, '$.name[0].family') AS patient_name FROM fhir.main.fhir_read('https://r4.smarthealthit.org', 'Patient', '45a69bf0-7116-4f9f-a1d9-29700439b4a4')) SELECT pt.patient_name, o.code_display, round(arg_max(o.value, o.effective), 1) AS latest_value, arg_max(o.unit, o.effective) AS unit, max(o.effective)[:10] AS latest_on
FROM fhir.main.fhir_observations('https://r4.smarthealthit.org', query := 'patient=45a69bf0-7116-4f9f-a1d9-29700439b4a4&category=vital-signs') AS o
JOIN pt ON o.subject = 'Patient/' || pt.patient_id
WHERE o.value IS NOT NULL
GROUP BY pt.patient_name, o.code_display
ORDER BY o.code_display;
fhir_servers
Curated directory of public HL7 FHIR R4 endpoints and whether they require authentication.
List the curated public FHIR R4 test servers and whether each needs authentication.
SELECT name, base_url, requires_auth
FROM fhir.main.fhir_servers
ORDER BY name;
fhir_capabilities (base_url VARCHAR, token := VARCHAR)
List the resource types and REST interactions from a FHIR R4 server's CapabilityStatement (/metadata)
List every resource type a FHIR server supports and the REST interactions available for each.
SELECT resource_type, interactions
FROM fhir.main.fhir_capabilities('https://r4.smarthealthit.org');
Expand the supported REST interactions to one row per (resource type, interaction).
SELECT resource_type, UNNEST(interactions) AS interaction
FROM fhir.main.fhir_capabilities('https://hapi.fhir.org/baseR4');
SELECT id, family, given, gender, birth_date
FROM fhir.main.fhir_patients('https://r4.smarthealthit.org')
LIMIT 20;
3 more examples
SELECT code, code_display, value, unit
FROM fhir.main.fhir_observations('https://r4.smarthealthit.org')
WHERE value IS NOT NULL
LIMIT 20;
SELECT id
FROM fhir.main.fhir_search('https://r4.smarthealthit.org', 'Condition')
LIMIT 20;
SELECT resource_type, UNNEST(interactions) AS interaction
FROM fhir.main.fhir_capabilities('https://r4.smarthealthit.org');
fhir_observations (base_url VARCHAR, query := VARCHAR, token := VARCHAR, count := BIGINT)
List FHIR R4 Observations (valueQuantity flattened; non-numeric values NULL; raw = full JSON)
List numeric Observations with their code, value, and unit.
SELECT code, code_display, value, unit
FROM fhir.main.fhir_observations('https://r4.smarthealthit.org')
WHERE value IS NOT NULL
LIMIT 20;
List body-weight Observations (LOINC 29463-7) on the public HAPI R4 server with their value, unit, and effective time.
SELECT id, value, unit, effective
FROM fhir.main.fhir_observations('https://hapi.fhir.org/baseR4', query := 'code=29463-7')
LIMIT 20;
| id | value | unit | effective |
|---|---|---|---|
| nhanes-112200-29463-7 | 120.1 | kg | 2026-01-15 09:00 |
| nhanes-120300-29463-7 | 100 | kg | 2026-01-15 09:00 |
| nhanes-118520-29463-7 | 69.9 | kg | 2026-01-15 09:00 |
| nhanes-112787-29463-7 | 97.4 | kg | 2026-01-15 09:00 |
| nhanes-122534-29463-7 | 83.2 | kg | 2026-01-15 09:00 |
Track one patient's body-weight trend: join their Body Weight Observations (LOINC 29463-7) to fhir_patients on the subject reference to label each row with the patient's name, and compute the change in kg since the previous measurement.
SELECT p.given, p.family, o.effective[:10] AS measured_on,
round(o.value, 1) AS weight_kg,
round(o.value - lag(o.value) OVER (ORDER BY o.effective), 1) AS change_kg
FROM fhir.main.fhir_observations('https://r4.smarthealthit.org', query := 'patient=45a69bf0-7116-4f9f-a1d9-29700439b4a4&code=29463-7') AS o
JOIN fhir.main.fhir_patients('https://r4.smarthealthit.org', query := '_id=45a69bf0-7116-4f9f-a1d9-29700439b4a4') AS p ON o.subject = 'Patient/' || p.id
ORDER BY o.effective;
fhir_patients (base_url VARCHAR, query := VARCHAR, token := VARCHAR, count := BIGINT)
List FHIR R4 Patients (core fields flattened; raw = full JSON)
List Patients with their core demographic fields flattened into columns.
SELECT id, family, given, gender, birth_date
FROM fhir.main.fhir_patients('https://r4.smarthealthit.org')
LIMIT 20;
List Patients with family name Smith on the public HAPI R4 server.
SELECT id, family, given, birth_date
FROM fhir.main.fhir_patients('https://hapi.fhir.org/baseR4', query := 'family=Smith')
LIMIT 20;
fhir_read (base_url VARCHAR, resource_type VARCHAR, id VARCHAR, token := VARCHAR)
Read a single FHIR R4 resource by id (one row; resource = full JSON)
Read one Patient by its logical id on the SMART Health IT R4 sandbox and return its full JSON.
SELECT resource
FROM fhir.main.fhir_read('https://r4.smarthealthit.org', 'Patient', '45a69bf0-7116-4f9f-a1d9-29700439b4a4');
Summarise one patient's latest vital signs: read the Patient by id with fhir_read for a display name, join it to their vital-signs Observations, and keep the most recent numeric value and unit per vital sign (components-only vitals such as Blood Pressure have a NULL value and drop out).
WITH pt AS (SELECT json_extract_string(resource, '$.id') AS patient_id, json_extract_string(resource, '$.name[0].given[0]') || ' ' || json_extract_string(resource, '$.name[0].family') AS patient_name FROM fhir.main.fhir_read('https://r4.smarthealthit.org', 'Patient', '45a69bf0-7116-4f9f-a1d9-29700439b4a4')) SELECT pt.patient_name, o.code_display, round(arg_max(o.value, o.effective), 1) AS latest_value, arg_max(o.unit, o.effective) AS unit, max(o.effective)[:10] AS latest_on
FROM fhir.main.fhir_observations('https://r4.smarthealthit.org', query := 'patient=45a69bf0-7116-4f9f-a1d9-29700439b4a4&category=vital-signs') AS o
JOIN pt ON o.subject = 'Patient/' || pt.patient_id
WHERE o.value IS NOT NULL
GROUP BY pt.patient_name, o.code_display
ORDER BY o.code_display;
fhir_search (base_url VARCHAR, resource_type VARCHAR, query := VARCHAR, token := VARCHAR, count := BIGINT)
Search a FHIR R4 resource type (one row per resource; resource = full JSON; follows Bundle next links)
List the ids of all Condition resources on a FHIR R4 server (paging through Bundle next links).
SELECT id
FROM fhir.main.fhir_search('https://r4.smarthealthit.org', 'Condition')
LIMIT 20;
Search Patients named 'smith' on the public HAPI R4 server, 100 per page, returning id plus the raw resource JSON.
SELECT id, resource
FROM fhir.main.fhir_search('https://hapi.fhir.org/baseR4', 'Patient', query := 'name=smith', count := 100);
Build one patient's problem list: search their Condition resources with fhir_search, join to fhir_patients on the subject reference, and report each condition's clinical status, onset date, and the patient's age in whole years at onset.
SELECT p.given, p.family,
json_extract_string(c.resource, '$.code.text') AS condition_name,
json_extract_string(c.resource, '$.clinicalStatus.coding[0].code') AS clinical_status,
CAST(json_extract_string(c.resource, '$.onsetDateTime')[:10] AS DATE) AS onset_date,
date_sub('year', CAST(p.birth_date AS DATE), onset_date) AS age_at_onset
FROM fhir.main.fhir_search('https://r4.smarthealthit.org', 'Condition', query := 'patient=45a69bf0-7116-4f9f-a1d9-29700439b4a4') AS c
JOIN fhir.main.fhir_patients('https://r4.smarthealthit.org', query := '_id=45a69bf0-7116-4f9f-a1d9-29700439b4a4') AS p ON json_extract_string(c.resource, '$.subject.reference') = 'Patient/' || p.id
ORDER BY onset_date, condition_name;
Try these queries
List the curated public FHIR R4 test servers and whether each needs authentication.
SELECT name, base_url, requires_auth
FROM fhir.main.fhir_servers
ORDER BY name;
Uses fhir_servers
List Patients with their core demographic fields flattened into columns.
SELECT id, family, given, gender, birth_date
FROM fhir.main.fhir_patients('https://r4.smarthealthit.org')
LIMIT 20;
Uses fhir_patients
List numeric Observations with their code, value, and unit.
SELECT code, code_display, value, unit
FROM fhir.main.fhir_observations('https://r4.smarthealthit.org')
WHERE value IS NOT NULL
LIMIT 20;
Uses fhir_observations
List the ids of Condition resources on a FHIR R4 server.
SELECT id
FROM fhir.main.fhir_search('https://r4.smarthealthit.org', 'Condition')
LIMIT 20;
Uses fhir_search
Read a single Patient resource by logical id and return its full JSON.
SELECT resource
FROM fhir.main.fhir_read('https://r4.smarthealthit.org', 'Patient', '45a69bf0-7116-4f9f-a1d9-29700439b4a4');
Uses fhir_read
Expand a server's supported REST interactions to one row per (resource type, interaction).
SELECT resource_type, UNNEST(interactions) AS interaction
FROM fhir.main.fhir_capabilities('https://r4.smarthealthit.org');
Uses fhir_capabilities
7 more examples in the Catalog, each listed under the table or function it uses.
FHIR is an independent open-source connector from Query.Farm. It is not affiliated with, endorsed by or sponsored by the services, projects or organisations it works with. Product names, logos and trademarks belong to their respective owners.
Related workers
HL7
Science & health
Split, validate, and pull fields out of HL7 v2.x clinical messages such as ADT and ORU feeds
Chem
Science & health
Compute molecular descriptors, fingerprints, similarity, substructure matches and InChI keys from SMILES strings
Periodic table
Science & health
The periodic table, isotopes and chemical formula molar masses as offline DuckDB SQL