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

Steps

  1. 1.

    Generate SDK package

    npm install -g @fhirschema/codegen
    npx fscg generate -g typescript -p hl7.fhir.r4.core@4.0.1 -o aidbox
    
  2. 2.

    Create a new typescript project

    npm init -y
    npm i typescript
    npm i --save-dev @type-challenges/utils
    npm i --save-dev @types/node
    
  3. 3.

    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"]
    }
    
  4. 4.

    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"]
    }
    
  5. 5.

    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" }
      ]
    }
    
  6. 6.

    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)),
    );
    
  7. 7.

    Run the project

    npx tsx index.ts
    

Next steps