FHIRKit Client
fhir-kit-client is a robust Node.js FHIR client library designed to facilitate interaction with FHIR servers. Currently at version 1.9.2, it maintains an active release cycle, frequently updating its 1.x.x branch to incorporate new features and fixes. This library supports a wide range of FHIR versions, including R4, STU3, and DSTU2, ensuring broad compatibility with various healthcare systems. It offers comprehensive support for all FHIR REST actions, operations, and advanced features like pagination, batch/transaction processing, and handling of absolute, in-bundle, and contained references. A key differentiator is its emphasis on modern JavaScript practices, utilizing a contemporary async/await structure and ES6 Classes, while also providing full TypeScript support. It includes features like metadata caching, SMART security integration, and capability-checking based on server statements. The library is built with minimal dependencies, aiming for efficiency and ease of integration into existing projects.
Common errors
-
TypeError: Client is not a constructor
cause Attempting to import `Client` as a named export (`import { Client } from 'fhir-kit-client'`) when it is a default export, or trying to access it as a property of the `require`d module.fixFor ESM, use `import Client from 'fhir-kit-client';`. For CommonJS, use `const Client = require('fhir-kit-client');`. -
Error: Resource not found for path: Patient/invalidId
cause The requested FHIR resource (e.g., Patient with a specific ID) does not exist on the server, or the ID is malformed.fixVerify the `resourceType` and `id` provided in your `read` or `request` call against the actual resources available on your target FHIR server. -
Error: Not Found - The request could not be mapped to an existing operation
cause The FHIR server's base URL is incorrect, or the specific resource type or operation you are attempting to access is not supported or does not exist at the provided endpoint on the server.fixDouble-check the `baseUrl` provided to the `Client` constructor. Consult the server's capability statement (available via `/metadata` endpoint) to confirm supported resource types and operations.
Warnings
- breaking The library explicitly requires Node.js version 16.0.0 or higher. Running in older Node.js environments may lead to unexpected errors or unsupported syntax.
- gotcha While fhir-kit-client supports multiple FHIR versions (R4, STU3, DSTU2), the TypeScript types from `@types/fhir` are version-specific (e.g., `fhir4.Patient`). Ensure that the types you import and use in your application match the FHIR version of the server you are connecting to, to avoid type mismatches and runtime errors.
- gotcha Setting up SMART security can be complex, involving OAuth 2.0 flows. Incorrect configuration of client credentials, redirect URIs, scopes, or token exchange can lead to authentication failures.
Install
-
npm install fhir-kit-client -
yarn add fhir-kit-client -
pnpm add fhir-kit-client
Imports
- Client
import { Client } from 'fhir-kit-client'import Client from 'fhir-kit-client'
- Client
const { Client } = require('fhir-kit-client')const Client = require('fhir-kit-client') - fhir4.Resource
import type { Resource, Patient } from 'fhir-kit-client'import type { Resource, Patient } from '@types/fhir'
Quickstart
const Client = require('fhir-kit-client');
const fhirClient = new Client({
baseUrl: 'https://sb-fhir-stu3.smarthealthit.org/smartstu3/open'
});
async function asyncExamples() {
// Get SMART URLs for OAuth
let response = await fhirClient.smartAuthMetadata();
console.log('SMART Auth Metadata:', response);
// Read a patient
response = await fhirClient.read({
resourceType: 'Patient',
id: '2e27c71e-30c8-4ceb-8c1c-5641e066c0a4'
});
console.log('Read Patient:', response);
// Search for patients with pagination
response = await fhirClient.search({ resourceType: 'Patient', searchParams: { _count: '1', gender: 'female' } });
console.log('Search Results (Page 1):', response);
if (response.link && response.link.find(l => l.relation === 'next')) {
const nextPageResponse = await fhirClient.nextPage(response);
console.log('Search Results (Next Page):', nextPageResponse);
}
}
asyncExamples().catch(console.error);