Learn

Explore development tutorials, how-tos and case studies

Playing with FHIR – TUT003 : Creating a PATIENT Resource (via code)

This tutorial will show how the Patient resource created in https://developer.nhs.uk/learn/playing-with-fhir-tut002-creating-and-editing-a-patient/ can be created using the .NET FHIR API available from GitHub

There are a number of FHIR Client APIs available, in later tutorials we’ll be using the HAPI API library to show some JAVA examples.

So to get started we need to define the FHIR endpoint we are going to be working with.

var client = new FhirClient("http://fhir2.healthintersections.com.au/open/");

Next we will define a patient resorce, set the patient identifier and make the patient resource active

            Patient myPat = new Patient();
            myPat.Identifier.Add(new Identifier("http://www.nhsdev.net/psn", "4568532"));
            myPat.Active = true;

Next we will set the patient’s name

            HumanName patName = new HumanName();
            patName.FamilyElement.Add(new FhirString("Vanilla"));
            patName.GivenElement.Add(new FhirString("Vincent"));
            patName.Use = HumanName.NameUse.Official;
            myPat.Name.Add(patName);

Now the date of birth and gender

            myPat.Gender = AdministrativeGender.Male;
            myPat.BirthDate = "1989-01-21";

and finally a reference the patients managing organisation (in our case their GP)

            myPat.ManagingOrganization = new ResourceReference() 
                  { Reference = "Organization/NHSDEVORG" };

Now we have created the patient resource (as per TUT002) we need to create the patient on the FHIR server. This happens in the following line of code.

            client.Create<Patient>(myPat);

That’s it. Clearly you’d need to be a bit more defensive in a real system but it shows the outline code required. The response of the “create” action is easily obtained.

            
            var httpResponse = client.LastResult.Status;
            var resourceId = client.LastBodyAsResource.Id;

So to recap the full code is as follows

            var client = new FhirClient("http://fhir2.healthintersections.com.au/open/");

            Patient myPat = new Patient();
            myPat.Identifier.Add(new Identifier("http://www.nhsdev.net/psn", "4568532"));
            myPat.Active = true;

            HumanName patName = new HumanName();
            patName.FamilyElement.Add(new FhirString("Vanilla"));
            patName.GivenElement.Add(new FhirString("Vincent"));
            patName.Use = HumanName.NameUse.Official;
            myPat.Name.Add(patName);

            myPat.Gender = AdministrativeGender.Male;
            myPat.BirthDate = "1989-01-21";

            myPat.ManagingOrganization = new ResourceReference() { Reference = "Organization/NHSDEVORG" };

            client.Create<Patient>(myPat);

            var httpResponse = client.LastResult.Status;
            var resourceId = client.LastBodyAsResource.Id;

That’s it..

If you have any questions or feedback on this tutorial then please feedback so we can improve the future tutorials.