fhirlab
The glue that holds FHIR together· 12 min read· hands-on

Search — the REST you already know

`?subject=Patient/123&date=gt2024-01-01` and the whole language behind it.

The REST you already know, with a tiny grammar on top

If you've ever written ?page=2&sort=-created_at against any web API, you already know 80% of FHIR search. It is plain HTTP GET on a resource-type URL, with query parameters that filter, sort, and shape the response. What makes FHIR search worth a whole lesson is the grammar on top — a handful of parameter types, modifiers, and prefixes that turn "give me some patients" into "give me every body-temperature over 38°C recorded in the last week." Once you own it, you can answer any question about the data in the server.

Search by id vs search by params

There are two different-looking operations that both feel like "fetching data", and it's worth separating them in your head.

  • GET /Patient/123 is a read. You know the id. The server returns exactly one resource, or a 404. The response body is the Patient.
  • GET /Patient?name=Curie is a search. You describe what you want. The server returns a Bundle — an envelope of zero, one, or many matches, each wrapped in an entry, along with paging links and a total count.

The Bundle exists because a search can match many resources, and HTTP needs a single response body. Even when the search matches nothing, you still get a Bundle — just an empty one. Expect to see Bundles for the rest of this lesson.

1 · Basic string search

Simplest possible search: find patients whose name contains "Curie". The name parameter is of type string, which by default matches at the start of each word, case- and accent-insensitive. We also ask for only five results with _count=5.

GET /Patient?name=Curie&_count=5
Results are across the entire public HAPI sandbox — you'll see patients created by everyone on the internet learning FHIR. That's expected.
GET
https://hapi.fhir.org/baseR4

Switch to the JSONPath tab and try $.total to see how many patients matched in total (not just the five returned), then $.entry[*].resource.name[0].family to pull family names across the page.

Parameter types matter

Every FHIR search parameter has a type, and the type decides what syntax is legal for its value. The main ones:

  • string — plain text like name=Curie. Default is start-of-word match; add :exact or :contains to change that.
  • token — a coded value, written as system|code. Example: gender=male or code=http://loinc.org|8310-5. No partial matching — a token is either a full match or not.
  • date — an ISO date or dateTime, optionally prefixed with eq, ne, gt, lt, ge, le, sa (starts after), eb (ends before), or ap (approximately).
  • number — same prefixes as date, for numeric fields.
  • reference — a pointer at another resource: subject=Patient/123.
  • quantity — a number with units: value-quantity=gt38|http://unitsofmeasure.org|Cel.
  • uri, composite — URLs and paired values; you'll meet these in later lessons.

The spec is the source of truth here — see hl7.org/fhir/R4/search.html for the exhaustive list, including modifiers like :missing, :not, :above, and :below.

2 · Search by token (code)

Clinical data lives inside CodeableConcepts, so most interesting searches are token searches. The form is parameter=system|code. Below we ask for any Observation whose code is LOINC 8310-5 — body temperature.

GET /Observation?code=http://loinc.org|8310-5
The pipe (|) separates the code system URL from the code itself. You can also pass just a code (code=8310-5) or just a system (code=http://loinc.org|) — each means something subtly different.
GET
https://hapi.fhir.org/baseR4

In the response, the matched Observation.code.coding entries will all share that system and code. This is how an app on the other side of the world asks "every body-temperature reading on this server" without having to know the local schema.

3 · Date range

Dates use prefixes. Repeating the same parameter name with two different prefixes composes them with AND — so the pair below means date ≥ 2020-01-01 AND date ≤ 2024-12-31.

GET /Observation?date=ge2020-01-01&date=le2024-12-31
Prefixes: ge (≥), le (≤), gt (>), lt (<), eq (default, =), ne (≠), sa (starts after), eb (ends before), ap (approximately).
GET
https://hapi.fhir.org/baseR4

Prefixes are two-letter and must be stuck directly to the value with no space or equals sign in between — date=ge2020-01-01, not date=ge=2020-01-01. The default when no prefix is given is eq.

Controlling the result shape

Four parameters shape the response rather than the filter. They all start with an underscore so they can't collide with resource fields.

  • _sort — order the results. Prefix a field with - for descending. _sort=-_lastUpdated gives newest first.
  • _count — page size. Servers cap it (HAPI around 1000), but you can ask for fewer to keep the response snappy.
  • _summary — trim the payload. Legal values: true (summary fields only), text (just narrative + id), data (everything except narrative), count (just the total — no entries at all), and false.
  • _elements — hand-pick fields: _elements=id,name,birthDate.

4 · _summary=count — quick totals

Sometimes all you want is "how many?" A full search with a huge _count is wasteful; _summary=count tells the server to run the query and return only the Bundle's total, with no entry array at all.

GET /Patient?_summary=count
How many Patient resources live on the public HAPI sandbox right now? One query, one number.
GET
https://hapi.fhir.org/baseR4

Combine it with a filter — /Observation?code=http://loinc.org|8310-5&_summary=count — and you've got a zero-cost counter for any slice of the data.

5 · Find all your own resources (recap)

Back to a familiar search from the first lesson, now with new vocabulary. We filter by _tag (a token), sort by _lastUpdated descending so the newest resource appears first, and cap the page at ten.

GET /Patient?_tag=…|{YOUR_UID}&_sort=-_lastUpdated&_count=10
Every search parameter here is one you've just met: _tag is a token, _sort takes a field with an optional minus prefix, _count caps the page.
GET
https://hapi.fhir.org/baseR4
URL still contains a placeholder ({UID}). Waiting for your anonymous ID…

A few other common built-in parameters you'll see often: _id (search by id, useful when combined with other params), _lastUpdated (date, so prefixes apply), _profile (which profiles a resource claims to conform to), and _tag (what we just used).

_include and _revinclude

Two parameters deserve their own moment later: _include pulls in resources that this resource references (e.g. a search for Observation with _include=Observation:subject also returns the referenced Patient), and _revinclude pulls in resources that reference this one back. They turn one round-trip into a full graph. We'll cover both in detail in the References lesson.

What just happened

  • You learned the single biggest grammar in FHIR: parameter type decides what syntax is legal for its value — string, token, date, number, reference, quantity.
  • You ran token searches (system|code), date range searches (ge/le), and result-shaping searches (_sort, _count, _summary=count).
  • You now have enough to answer most real questions about a FHIR server's data — and to read almost any FHIR URL you encounter in the wild.

From here on, exploration is yours. The Console gives you the same request bar without a lesson wrapped around it — point it at any resource type, compose your own query, and read the Bundles that come back. That's where FHIR stops being a tutorial and starts being a tool.