Skip to content

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
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;
Result · first 5 rows
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.

setup.sql
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;

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'
);

3. Run a query

query.sql
SELECT id, value, unit, effective
FROM fhir.main.fhir_observations('https://hapi.fhir.org/baseR4', query := 'code=29463-7')
LIMIT 20;
More example queries →
Technical details
Verified connector release
v0.5.0
Engine
2.0.1
VGI protocol
3.4.0
Verified on
December 1, 2026
Release notes on GitHub
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]'
);

Overview

Query HL7 FHIR R4 REST servers and return resources as rows

Catalog

Everything fhir exposes once attached — read straight from the worker's released build.

fhir 1 schema
main 1 view · 5 functions

FHIR R4 query functions: patients, observations, generic search, single-resource read, and server capabilities.

Schema examples (9)
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;
Views
fhir_servers 1 example5 cols

Curated directory of public HL7 FHIR R4 endpoints and whether they require authentication.

Columns
name VARCHAR
Human-friendly name of the FHIR R4 server.
base_url VARCHAR
Service base URL to pass as the first argument of every fhir_* table function.
fhir_version VARCHAR
FHIR specification version the endpoint serves (all R4, 4.0.1).
requires_auth BOOLEAN
Whether the endpoint requires an OAuth bearer token (all listed servers are open).
description VARCHAR
What the endpoint is and what data it holds.
Example

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;
Link
Table functions
fhir_capabilities (base_url VARCHAR, token := VARCHAR) 6 examples

List the resource types and REST interactions from a FHIR R4 server's CapabilityStatement (/metadata)

Positional arguments
base_url VARCHAR
FHIR R4 service base URL
Named arguments
token := VARCHAR
OAuth bearer token · default ""
Returns columns
resource_type VARCHAR
A FHIR resource type the server exposes (e.g. Patient).
interactions VARCHAR[]
REST interaction codes supported for that type (e.g. read, search-type, create).
Examples (6)

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');
Link
fhir_observations (base_url VARCHAR, query := VARCHAR, token := VARCHAR, count := BIGINT) 3 examples

List FHIR R4 Observations (valueQuantity flattened; non-numeric values NULL; raw = full JSON)

Positional arguments
base_url VARCHAR
FHIR R4 service base URL
Named arguments
query := VARCHAR
Raw FHIR search query string appended to the request, such as a status or LOINC code filter · default ""
token := VARCHAR
OAuth bearer token · default ""
count := BIGINT
Page size (_count search parameter) · default 50
Returns columns
id VARCHAR
Logical id of the Observation resource.
status VARCHAR
Observation status such as final, preliminary, or amended.
code VARCHAR
Primary observation code (e.g. the LOINC code from code.coding[0]).
code_display VARCHAR
Human-readable display text for the code.
value DOUBLE
Numeric valueQuantity.value; NULL for non-numeric observations.
unit VARCHAR
Unit of the value (valueQuantity.unit).
effective VARCHAR
Effective date/time of the observation.
subject VARCHAR
Reference to the subject (usually Patient/{id}).
raw VARCHAR
The full Observation resource as a JSON string.
Examples (3)

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;
Result · first 5 rows
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;
Link
fhir_patients (base_url VARCHAR, query := VARCHAR, token := VARCHAR, count := BIGINT) 2 examples

List FHIR R4 Patients (core fields flattened; raw = full JSON)

Positional arguments
base_url VARCHAR
FHIR R4 service base URL
Named arguments
query := VARCHAR
Raw search query string (e.g. family=smith) · default ""
token := VARCHAR
OAuth bearer token · default ""
count := BIGINT
Page size (_count search parameter) · default 50
Returns columns
id VARCHAR
Logical id of the Patient resource.
family VARCHAR
Family (last) name from the first name entry.
given VARCHAR
Given (first) name(s) from the first name entry.
gender VARCHAR
Administrative gender: male, female, other, or unknown.
birth_date VARCHAR
Date of birth (YYYY-MM-DD or a partial FHIR date).
active BOOLEAN
Whether the Patient record is in active use.
raw VARCHAR
The full Patient resource as a JSON string.
Examples (2)

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;
Link
fhir_read (base_url VARCHAR, resource_type VARCHAR, id VARCHAR, token := VARCHAR) 2 examples

Read a single FHIR R4 resource by id (one row; resource = full JSON)

Positional arguments
base_url VARCHAR
FHIR R4 service base URL
resource_type VARCHAR
FHIR resource type (e.g. Patient)
id VARCHAR
Logical id of the resource to read
Named arguments
token := VARCHAR
OAuth bearer token · default ""
Returns columns
resource VARCHAR
The requested resource as a JSON string.
Examples (2)

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;
Link

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.