To use Aidbox with C#, 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.

    Create a Client and an Access Policy in Aidbox.

    Navigate to REST Console in Aidbox UI and execute the following requests:

    POST /fhir/Client
    content-type: application/json
    accept: application/json
    
    {
      "secret": "secret",
      "grant_types": [
        "basic"
      ],
      "id": "basic",
      "resourceType": "Client"
    }
    
    POST /fhir/AccessPolicy
    content-type: application/json
    accept: application/json
    
    {
      "link": [
        {
          "reference": "Client/basic"
        }
      ],
      "engine": "allow",
      "id": "basic-policy",
      "resourceType": "AccessPolicy"
    }
    
  2. 2.

    Set up .NET project:

    dotnet new console -n MyApp
    cd MyApp
    dotnet add package System.Text.Json
    dotnet add package Microsoft.Extensions.DependencyInjection
    
  3. 3.

    Generate R4 SDK

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

    Here's how it should look like:

    ├── MyApp
    │   ├── aidbox
    │   ├── bin
    │   ├── MyApp.csproj
    │   ├── obj
    │   └── Program.cs
    ├── Program.cs
    └── docker-compose.yaml
    
  4. 4.

    Create a Patient using created Client:

    using Aidbox.Client;
    using Aidbox.FHIR.R4.Core;
    
    var auth = new Auth
    {
        Method = AuthMethods.BASIC,
        Credentials = new AuthCredentials
        {
            Username = "basic",
            Password = "secret"
        }
    };
    
    var client = new Client("http://localhost:8080/fhir", auth);
    
    
    var patient = new Patient
    {
        Identifier = [new Identifier { System = "http://org.io/id", Value = "0000-0000" }],
        Name = [new HumanName { Given = ["John"], Family = "Doe" }],
        Gender = "male",
        BirthDate = "1990-01-01",
    };
    
    var (result, error) = await client.Create(patient);
    if (result != null)
    {
        System.Console.WriteLine("Patient id: " + result.Id);
    }
    else
    {
        System.Console.WriteLine(error);
    }
    
  5. 5.

    Run project

    dotnet run
    

    The output:

    Patient id: 4c43be71-bebb-41ce-8913-e48d3fecbfa4
    

Next steps