Custom FHIR Operations | Medplum

Custom FHIR Operations is a powerful feature that allows you to extend the Medplum FHIR server with your own custom operations using the Medplum Bots framework. This feature enables you to create sophisticated business logic that integrates seamlessly with the FHIR API while leveraging the full power of JavaScript execution.

What are Custom FHIR Operations?

Custom FHIR Operations combine two key Medplum features:

  1. FHIR Operations - Standard FHIR operations (like $validate, $expand, etc.) that can be invoked via HTTP requests
  2. Medplum Bots - JavaScript functions that can be executed server-side with access to the Medplum SDK

This integration allows you to:

How It Works

Custom operations are implemented through three main components:

  1. Bot - Contains the JavaScript code that implements your custom logic
  2. OperationDefinition - Defines the operation interface and links to your Bot
  3. Extension - Links the OperationDefinition to the Bot implementation

When a client calls your custom operation (e.g., POST /fhir/R4/Patient/$my-custom-operation), Medplum:

  1. Looks up the corresponding OperationDefinition
  2. Finds the linked Bot via the extension
  3. Executes the Bot with the operation input
  4. Returns the Bot's output as the operation result

How to Use Custom FHIR Operations

Step 1: Create a Bot

First, create a Bot that contains your custom logic:

exports.handler = async function (medplum, event) {

const patient = event.input;

// Your custom logic here

patient.identifier = patient.identifier || [];

patient.identifier.push({

system: 'https://example.com/patient-id',

value: '12345',

});

return patient;
};

See Bot Basics for more details on creating Bots.

Important Requirements:

Step 2: Deploy the Bot

Deploy your Bot code using the $deploy operation:

curl -X POST "https://api.medplum.com/fhir/R4/Bot/{bot-id}/$deploy" \
  -H "Content-Type: application/fhir+json" \
  -H "Authorization: Bearer {access-token}" \
  -d '{

"code": "exports.handler = async function (medplum, event) { ... }"

}'

Step 3: Create an OperationDefinition

Create an OperationDefinition that describes your operation and links to your Bot:

{
  "resourceType": "OperationDefinition",
  "extension": [
    {
      "url": "https://medplum.com/fhir/StructureDefinition/operationDefinition-implementation",
      "valueReference": {
        "reference": "Bot/{bot-id}"
      }
    }
  ],
  "name": "my-custom-operation",
  "status": "active",
  "kind": "operation",
  "code": "my-custom-operation",
  "system": true,
  "type": false,
  "instance": false,
  "parameter": [
    {
      "use": "in",
      "name": "input",
      "type": "Patient",
      "min": 1,
      "max": "1"
    },
    {
      "use": "out",
      "name": "return",
      "type": "Patient",
      "min": 1,
      "max": "1"
    }
  ]
}

Key Requirements:

Step 4: Invoke Your Custom Operation

Once deployed, you can invoke your custom operation like any standard FHIR operation:

curl -X POST "https://api.medplum.com/fhir/R4/Patient/$my-custom-operation" \
  -H "Content-Type: application/fhir+json" \
  -H "Authorization: Bearer {access-token}" \
  -d '{
    "resourceType": "Patient",
    "name": [
      {
        "family": "Smith",
        "given": ["John"]
      }
    ]
  }'

Operation Types

Custom operations support all standard FHIR operation types:

Configure the operation type using the system, type, and instance properties in your OperationDefinition.

Input and Output Handling

Input Processing

Output Processing

The Bot's return value is automatically formatted according to the OperationDefinition's output parameters. You can return:

Security and Permissions

Custom operations inherit the security model of both Bots and FHIR operations:

Error Handling

If your Bot encounters an error:

Example Use Cases