Use Aidbox with TypeScript
To use Aidbox with Typescript, we recommend to use our SDK generation library. It will generate static FHIR Client and the set of types for your FHIR package. You can read about the idea of it here.
Prerequisites
- Run Aidbox locally by following the instructions in the Run Aidbox locally guide
Steps
-
Generate SDK package
npm install -g @fhirschema/codegen npx fscg generate -g typescript -p hl7.fhir.r4.core@4.0.1 -o aidbox -
Create a new typescript project
npm init -y npm i typescript npm i --save-dev @type-challenges/utils npm i --save-dev @types/node -
Create a new file
tsconfig.jsonand add the following code:{ "compilerOptions": { "target": "ES2020", "module": "NodeNext", "moduleResolution": "NodeNext", "esModuleInterop": true, "strict": true, "skipLibCheck": true, "noEmit": true }, "include": ["./aidbox/**/*.ts"] } -
Navigate to Aidbox REST console and create a client by executing the following request:
POST /fhir/Client Content-Type: application/json { "resourceType": "Client", "id": "my-client", "secret": "my-secret", "grant_types": ["basic"] } -
Create an access policy by executing the following request in the Aidbox REST console:
POST /fhir/AccessPolicy Content-Type: application/json { "resourceType": "AccessPolicy", "engine": "allow", "link": [ { "id": "my-client", "resourceType": "Client" } ] } -
Create a new file
index.tsand add the following code:import { Client } from "../aidbox"; import type { Patient } from "../aidbox/types/hl7-fhir-r4-core"; async function main() { const client = new Client("http://localhost:8080", { auth: { method: "basic", credentials: { username: "my-client", password: "my-secret" } } }); const patient: Patient = { identifier: [{ system: "http://org.io/id", value: "0000-0000" }], name: [{ given: ["John"], family: "Doe" }], gender: "male", birthDate: "1990-01-01", }; const response = await client.resource.create("Patient", patient); console.log(JSON.stringify(response, null, 2)); } main().catch((error) => console.log(error instanceof Error ? error.message : String(error)), ); -
Run the project
npx tsx index.ts
Next steps
- Learn more about Aidbox SDKs generation
- Learn more about Aidbox Access Control