# @aehrc/sdc-populate

> Performs the $populate operation from the HL7 FHIR SDC (Structured Data Capture) specification: http://hl7.org/fhir/uv/sdc

Latest version **4.7.2** (published 2026-09-10) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @aehrc/sdc-populate
pnpm add @aehrc/sdc-populate
yarn add @aehrc/sdc-populate
bun add @aehrc/sdc-populate
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 4.7.2 |
| Published | 2026-09-10 |
| First published | 2023-09-01 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 5 |
| Unpacked size | 812.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 61 |
| Maintainers | clinnygee, janadh, maryammehdiza, johngrimes, leoniedickson |

## Links

- npm: https://www.npmjs.com/package/@aehrc/sdc-populate
- Repository: https://github.com/aehrc/smart-forms
- Homepage: https://github.com/aehrc/smart-forms#readme
- Issues: https://github.com/aehrc/smart-forms/issues
- npm.io page: https://npm.io/package/@aehrc/sdc-populate

## Dependencies (5)

- [dayjs](https://npm.io/package/dayjs.md) ^1.11.21
- [fhirpath](https://npm.io/package/fhirpath.md) ^4.10.1
- [js-base64](https://npm.io/package/js-base64.md) ^3.7.8
- [fhirclient](https://npm.io/package/fhirclient.md) ^2.6.3
- [fhir-sdc-helpers](https://npm.io/package/fhir-sdc-helpers.md) ^0.1.0

## Recent versions

- 4.7.2 (latest) — 2026-09-10
- 4.0.0-beta.2 (beta) — 2025-04-09
- 4.7.1 — 2026-07-03
- 4.7.0 — 2026-03-25
- 4.6.3 — 2026-03-11
- 4.6.2 — 2025-08-07
- 4.6.1 — 2025-08-07
- 4.6.0 — 2025-07-24
- 4.5.0 — 2025-07-14
- 4.4.0 — 2025-07-10
- 4.3.1 — 2025-06-18
- 4.3.0 — 2025-06-02
- 4.2.0 — 2025-06-02
- 4.1.0 — 2025-05-22
- 4.0.1 — 2025-04-10
- … 38 more at https://npm.io/package/@aehrc/sdc-populate/versions

## README

# SDC-Populate

A Typescript reference implementation of the [$populate](http://hl7.org/fhir/uv/sdc/OperationDefinition-Questionnaire-populate.html) operation from the [HL7 FHIR Structured Data Capture Specification](http://hl7.org/fhir/uv/sdc/ImplementationGuide/hl7.fhir.uv.sdc) designed for [Form Population](http://hl7.org/fhir/uv/sdc/populate.html).

Check out the [API Reference](https://smartforms.csiro.au/docs/api/sdc-populate) for technical specifications. 

## Usage
There are two ways to use this package:
1. Using it in a web app
2. Using it in a backend service e.g. ExpressJS

### Using it in a web app
It is recommended to use `populateQuestionnaire()`, which performs an in-app population. This means that the app is not sending data to an external server, but rather using the library to populate the questionnaire in the app itself.

```ts
const { populateSuccess, populateResult } = await populateQuestionnaire({
  questionnaire: yourQuestionnaireResource,
  fetchResourceCallback: yourFetchResourceCallbackFunction,
  fetchResourceRequestConfig: yourFetchResourceRequestConfig,
  patient: yourPatient,
});

// Pre-population is successful
if (populateSuccess && populateResult !== null) {
  const questionnaireResponse = populateResult.populatedResponse;
  
  // Do things with the pre-populated questionnaireResponse...
}
```

You will also need to define your own `fetchResourceCallback` function and `fetchResourceRequestConfig`. This function is responsible for fetching resources from your FHIR server. It should return a promise that resolves to the resource you want to fetch.

```ts
import { FetchResourceRequestConfig, FetchResourceCallback } from '@aehrc/sdc-populate';

const ABSOLUTE_URL_REGEX = /^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/;

export const yourFetchResourceRequestConfig: FetchResourceRequestConfig = {
  sourceServerUrl: 'https://proxy.smartforms.io/fhir',
  authToken: string | null
};

export const yourFetchResourceCallbackFunction: FetchResourceCallback = async (
  query: string,
  requestConfig: FetchResourceRequestConfig
) => {
  let { sourceServerUrl } = requestConfig;
  const { authToken } = requestConfig;

  const headers: Record<string, string> = {
    Accept: 'application/json;charset=utf-8'
  };

  if (authToken) {
    headers['Authorization'] = `Bearer ${authToken}`;
  }

  if (!sourceServerUrl.endsWith('/')) {
    sourceServerUrl += '/';
  }

  const requestUrl = ABSOLUTE_URL_REGEX.test(query) ? query : `${sourceServerUrl}${query}`;
  const response = await fetch(requestUrl, { headers });

  if (!response.ok) {
    throw new Error(`HTTP error when performing ${requestUrl}. Status: ${response.status}`);
  }

  return response.json();
};
```


Available parameters for `populateQuestionnaire()`:

| Parameter                      | Description                                                                                      |
|-------------------------------|--------------------------------------------------------------------------------------------------|
| `questionnaire`               | Questionnaire to populate                                                                        |
| `fetchResourceCallback`       | A callback function to fetch resources from your FHIR server                                     |
| `fetchResourceRequestConfig`  | Any request configuration to be passed to the fetchResourceCallback (e.g., headers, auth, etc.)  |
| `patient`                     | Patient resource as patient in context                                                           |
| `user`                        | Practitioner resource as user in context (optional)                                              |
| `encounter`                   | Encounter resource as encounter in context (optional)                                            |
| `fetchTerminologyCallback`    | A callback function to fetch terminology resources (optional)                                    |
| `fetchTerminologyRequestConfig` | Any request configuration to be passed to the fetchTerminologyCallback (e.g., headers, auth, etc.) (optional) |



### Using it in a backend service
Using this library in a backend service requires more pre-configuration. 
Due to how the [$populate](https://hl7.org/fhir/uv/sdc/OperationDefinition-Questionnaire-populate.html) works, you will need to provide a bunch of input parameters to the `populate()` function.

There is a sample implementation of how to use the `populate()` function in https://github.com/aehrc/smart-forms/blob/main/services/populate-express/src/index.ts.


### Local development notes
It's recommended to run this library within a web app or a service if you're doing local development. 
This library compiles to both CommonJS and ES Modules, so there is no problems in using it across web frameworks and Node-based backends.

To compile the code, use `npm run compile`.
To watch for changes, use `npm run watch`.

Note: Do not use `tsc` or `tsc -w` as it will only compile to ES Modules, which means it will not work with CommonJS-based implementations.

---
_Source: https://npm.io/package/@aehrc/sdc-populate · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
