Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

FHIR Client Build Status

Ruby FHIR client.


  • FHIR R4, STU3 and DSTU2
  • JSON and XML
  • All CRUD, including version read and history
  • Transactions and Batches
  • Search
  • Operations (e.g. $everything, $validate)
  • Support for OAuth2

Getting Started

$ bundle install
$ bundle exec rake fhir:console

Creating a Client

client =

This client supports two modes of operation: basic and advanced. The basic mode is useful for simple operations because it promotes an ActiveRecord-like style of interaction. The advanced mode is less developer-friendly, but is currently necessary if you would like to use the entire range of operations exposed by FHIR.

Basic Usage

Associate the client with the model:

FHIR::Model.client = client

The FHIR models can now be used to directly interact with a FHIR server.

# read an existing patient with an ID of 'example'
patient ='example')

# update a patient
patient.gender = 'female'
patient.update # saves the patient

# create a patient
patient = FHIR::Patient.create(name: {given: 'John', family: 'Doe'})

#create a patient with specific headers
patient = {given: 'John', family: 'Doe'}).create({Prefer: "return=representation"})

# search patients
results = 'John', family: 'Doe')
results.count # results in an enumeration

# delete the recently created patient

Advanced Usage

Changing FHIR Versions

The client defaults to R4 but can be switched to DSTU2 or STU3. It can also attempt to autodetect the FHIR version based on the metadata endpoint.

# autodetect the FHIR version
client =
version = client.detect_version
if version == :stu3
  puts 'FHIR Client using STU3'
elsif version == :dstu2
  puts 'FHIR Client using DSTU2'
elsif version == :r4
  puts 'FHIR Client using R4'

# tell the client to use R4
# now use the client with the DSTU2 models
patient ='example')
patient =, 'example').resource

# tell the client to use STU3 (default)
# now use the client normally
patient ='example')
patient =, 'example').resource

# tell the client to use DSTU2
# now use the client with the DSTU2 models
patient ='example')
patient =, 'example').resource

Changing FHIR Formats

The client defaults to json representation of resources but can be switched to xml representations.

client =

# Tell the client to use xml

# Tell the client to use json


You can specify additional properties for the client:

client.additional_headers = {Prefer: 'return=representation'}
client.proxy = ''

CRUD Examples

# read an existing patient with id "example"
patient =, "example").resource

# read specifying Formats
patient =, "example", FHIR::Formats::FeedFormat::FEED_JSON).resource

# create a patient
patient =
patient_id = client.create(patient).id

# update the patient
patient.gender = 'female'
client.update(patient, patient_id)

# conditional update
p_identifier =
p_identifier.use = 'official'
p_identifier.system = ''
p_identifier.value = '123'
patient.identifier = [ p_identifier ]
searchParams = { :identifier => '|123' }
client.conditional_update(patient, patient_id, searchParams)

# conditional create
ifNoneExist = { :identifier => '|123' }
client.conditional_create(patient, ifNoneExist)

# destroy the patient
client.destroy(FHIR::Patient, patient_id)


# via GET
reply =, search: {parameters: {name: 'P'}})

# via POST
reply =, search: {body: {name: 'P'}})

bundle = reply.resource
patient = bundle&.entry&.first&.resource

Fetching a Bundle

reply = client.read_feed(FHIR::Patient) # fetch Bundle of Patients
bundle = reply.resource
bundle.entry.each do |entry|
  patient = entry.resource
puts reply.code # HTTP 200 (or whatever was returned)
puts reply.body # Raw XML or JSON


# Create a patient
@patient = = 'temporary_id'

# Create an observation
@observation =
@observation.status = 'final'
@coding =
@coding.system = ''
@observation.code =
@observation.code.coding = [ @coding ]
@quantity =
@quantity.value = 170
@quantity.unit = 'cm'
@quantity.system = ''
@observation.valueQuantity = @quantity
@reference =
@reference.reference = "Patient/#{}"
@observation.subject = @reference

# Submit them both as a transaction
reply = @client.end_transaction

OAuth2 Support

client =
client_id = 'example'
client_secret = 'secret'
options = client.get_oauth2_metadata_from_conformance
if options.empty?
  puts 'This server does not support the expected OAuth2 extensions.'
  client.set_oauth2_auth(client_id, client_secret, options[:authorize_url] ,options[:token_url], options[:site])
  reply = client.read_feed(FHIR::Patient)
  puts reply.body


Copyright 2014-2022 The MITRE Corporation

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.