Use Aidbox with TypeScript
Follow these steps to create a TypeScript project with Aidbox
Prerequisites
- Run Aidbox locally by following the instructions in the Run Aidbox locally guide
Steps
- 1.Generate SDK package
npm install -g @fhirschema/codegen
npx fscg generate -g typescript -p hl7.fhir.r4.core@4.0.1 -o aidbox
- 1.Create a new typescript project
npm init -y
npm i typescript
npm i --save-dev @type-challenges/utils
npm i --save-dev @types/node
- 1.Create a new file
tsconfig.json
and add the following code:
{
"compilerOptions": {
"target": "ES2020",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"strict": true,
"skipLibCheck": true,
"noEmit": true
},
"include": ["./aidbox/**/*.ts"]
}
- 1.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"]
}
- 1.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" }
]
}
- 1.Create a new file
index.ts
and 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)),
);
- 1.Run the project
npx tsx index.ts
Next Steps
- Learn more about Aidbox SDKs generation
- Learn more about Aidbox Access Control