Clara Form
FHIR QuestionnaireResponse example: export a medical form to FHIR R4
Build a FHIR R4 QuestionnaireResponse from a medical form in the browser: typed answers, one group per section, subject and encounter. Live example.
Loading the preview…
<script src="https://cdn.clara.care/form/latest.js"></script>
<main class="demo demo-split">
<clara-form lang="en" submit-label="Submit visit"></clara-form>
<section class="demo-out" aria-labelledby="fhir-h">
<div class="demo-actions">
<h2 id="fhir-h">FHIR R4 QuestionnaireResponse</h2>
<button type="button" id="export">Export now</button>
<button type="button" id="sample">Fill sample values</button>
</div>
<pre id="output" aria-live="polite">Fill the form, or load the sample values, and export.</pre>
</section>
</main>
// Template: sections become FHIR group items, fields become question items (linkId = field id).
const template = {
id: '3f2c6b1e-0d2a-4c9f-9a1e-000000000042',
name: 'Visit',
sections: [
{
key: 'visit',
label: 'Visit',
fields: [
{ id: 'reason', type: 'text', label: 'Reason for visit', required: true },
{ id: 'sex', type: 'select', label: 'Sex', options: ['Female', 'Male', 'Other'] },
],
},
{
key: 'vitals',
label: 'Vital signs',
fields: [
{ id: 'temperature', type: 'number', label: 'Temperature (°C)', min: 30, max: 45, step: 0.1 },
{ id: 'blood_pressure', type: 'text', label: 'Blood pressure', placeholder: '120/80', pattern: '\\d{2,3}/\\d{2,3}' },
{ id: 'smoker', type: 'checkbox', label: 'Smoker' },
],
},
],
};
const form = document.querySelector('clara-form');
form.template = template;
const output = document.querySelector('#output');
const show = (resource) => { output.textContent = JSON.stringify(resource, null, 2); };
// toFhir(): status, subject, encounter, questionnaire and codeSystem are yours to set.
const exportFhir = (status) => form.toFhir({
status,
subject: { reference: 'Patient/example-001' },
encounter: { reference: 'Encounter/example-visit-7' },
questionnaire: 'https://example.org/Questionnaire/visit',
codeSystem: 'https://example.org/CodeSystem/visit-options',
});
document.querySelector('#export').addEventListener('click', () => show(exportFhir('in-progress')));
// Synthetic values, only for the demonstration.
document.querySelector('#sample').addEventListener('click', () => {
form.setFieldValue('reason', 'Annual check-up');
form.setFieldValue('sex', 'Female');
form.setFieldValue('temperature', 36.8);
form.setFieldValue('blood_pressure', '118/76');
form.setFieldValue('smoker', false);
show(exportFhir('in-progress'));
});
// A valid submission exports with status "completed" (e.detail.fhir is the same resource).
form.addEventListener('clara-submit', (e) => show(e.detail.fhir));
Every value on this page is synthetic. Never paste real patient data into a public page.
How it works
Render the form from a template
The template's sections become group items and its fields become question items. The field id is the linkId; the label is the item text.
Fill the form
By hand, from your own code with setFieldValue(), or by voice with Clara Widget. Values are kept per field key and converted to the field type.
Export with toFhir()
Pass status, subject, encounter, questionnaire and codeSystem. Unanswered questions are omitted unless includeEmpty is true.
Send it to your FHIR endpoint
POST the resource as application/fhir+json, or let the form do it with submit-url and submit-format set to fhir.
QuestionnaireResponse is the FHIR resource that stores the answers to a questionnaire: who answered, about whom, when, and one item per question with a typed value. It is how a web form reaches a FHIR server, an EHR with a FHIR API or a SMART on FHIR app without a custom mapping for every field.
This example fills a short visit form and exports it with toFhir(). The resource below is produced in your browser by the real @clara.care/form package: valueString for text, valueDecimal for numbers, valueBoolean for checkboxes and valueCoding for selects, grouped by section with the field ids as linkIds.
How each field type maps to FHIR
text and textarea become valueString; number becomes valueDecimal (valueString if the text is not numeric); checkbox becomes valueBoolean; select becomes valueCoding with the option text as code and display, and your codeSystem as system; file becomes valueAttachment with the file name; the odontogram becomes valueString with the JSON list of actions. iframe and event fields have no value and do not appear.
The questionnaire reference defaults to urn:uuid:<template id>; pass your own canonical URL when the Questionnaire lives on your server. subject and encounter are plain references, for example Patient/123 and Encounter/456.
Without the form
toFhirQuestionnaireResponse(template, values, options) is also exported as a plain function. Use it in Node.js or in your own UI when you already have the values and only need the resource.
import { toFhirQuestionnaireResponse } from '@clara.care/form';
const qr = toFhirQuestionnaireResponse(template, values, {
status: 'completed', // default 'in-progress'
subject: { reference: 'Patient/123' },
encounter: { reference: 'Encounter/456' },
questionnaire: 'https://example.org/Questionnaire/visit',
codeSystem: 'https://example.org/CodeSystem/options',
});
await fetch('https://fhir.example.org/QuestionnaireResponse', {
method: 'POST',
headers: { 'Content-Type': 'application/fhir+json' },
body: JSON.stringify(qr),
});
Questions about this example
What is a FHIR QuestionnaireResponse?
It is the FHIR resource that records the answers to a Questionnaire: references to the subject and the encounter, a status, and a tree of items with typed answers. It lets any form reach a FHIR server without a custom data model.
Which FHIR version does the export follow?
FHIR R4. The resource uses resourceType QuestionnaireResponse, status, questionnaire, subject, encounter, authored and item, with answer values valueString, valueDecimal, valueBoolean, valueCoding and valueAttachment.
Do I need a Questionnaire resource on my server?
Not to export. The questionnaire field defaults to a urn:uuid built from the template id. If your server validates against a Questionnaire, publish one that mirrors the template and pass its canonical URL in the questionnaire option.
How do unanswered questions appear?
They are omitted by default, so the resource only contains what was filled in. With includeEmpty set to true every question appears, unanswered ones without an answer element and unchecked boxes as valueBoolean false.
Can the resource be posted automatically?
Yes. Set submit-url to your endpoint and submit-format to fhir: on a valid submission the form sends the QuestionnaireResponse as application/fhir+json and reports the result in clara-submitted or clara-submit-error.
Is this related to SMART on FHIR?
A SMART on FHIR app runs inside an EHR with a FHIR API. The form gives such an app a ready QuestionnaireResponse to write back, and Clara Widget can fill the app's fields by voice during the visit.
Planning an integration?
Tell us about your system and your forms. We run small pilots with technology partners.


