On this page

Implementing FHIR Event Notifications

Configure a Subscription to notify a Medplum Bot or your own HTTPS endpoint when a matching FHIR resource changes. This example sends a notification when a Patient is created. The same event criteria work for either destination.

Use Running Server Side Code: Bots and Subscriptions to choose the destination and matching resource changes for each Subscription.

Select the resource change​

In a test project, create the following Subscription in the Medplum App or through the FHIR API. Replace <bot-id> with your deployed Bot's ID. The endpoint alternative is described below. Keep the Subscription off until the destination is ready.

{
  "resourceType": "Subscription",
  "status": "off",
  "reason": "Notify the registration integration when a Patient is created",
  "criteria": "Patient",
  "channel": {
    "type": "rest-hook",
    "endpoint": "Bot/<bot-id>",
    "payload": "application/fhir+json"
  },
  "extension": [
    {
      "url": "https://medplum.com/fhir/StructureDefinition/subscription-supported-interaction",
      "valueCode": "create"
    }
  ]
}
  • criteria: "Patient" selects Patient resources. Add supported FHIR search parameters to narrow the resource set.
  • channel.type: "rest-hook" is the notification channel used for both a Bot destination and an HTTPS destination. Bot/<bot-id> selects the Bot.
  • The create interaction extension excludes updates and deletions. Without interaction extensions, all three can trigger notifications. See Subscription interactions.

Deletion notifications have a different payload. An HTTPS endpoint receives an empty JSON object and the resource identifier in X-Medplum-Deleted-Resource; a Bot receives the deleted resource under event.input.deletedResource. See Subscriptions for delete events.

Scope the criteria so writes made by your handler do not trigger an unintended loop. Check meta.accounts if an expected notification never arrives: account scope can exclude a resource even when its criteria match. See Publish and Subscribe.

Filter for a status transition​

To react when a QuestionnaireResponse changes to completed, use a Bot or HTTPS endpoint whose handler accepts QuestionnaireResponse input. Replace the example's reason, criteria, and extension fields with:

{
  "reason": "Handle a QuestionnaireResponse when its status changes to completed",
  "criteria": "QuestionnaireResponse?status=completed",
  "extension": [
    {
      "url": "https://medplum.com/fhir/StructureDefinition/subscription-supported-interaction",
      "valueCode": "update"
    },
    {
      "url": "https://medplum.com/fhir/StructureDefinition/fhir-path-criteria-expression",
      "valueString": "%previous.status != %current.status"
    }
  ]
}

The search criteria select the completed state. The FHIRPath expression compares the previous and current status, excluding later edits that leave the status completed. The update interaction excludes creation and deletion.

To also handle a response created in the completed state, add a second interaction extension with valueCode: "create" and use %previous.id.exists() implies %previous.status != %current.status as the expression. Testing the previous resource's id accounts for the absence of a prior version on creation.

Use a Medplum Bot​

  1. Enable Bots for the project, create the Bot, and deploy its handler using Bots in Production.
  2. Configure its AccessPolicy for the reads and writes it needs, and use Bot secrets for external credentials. Bots normally run with their own permissions; use Run as User when the handler should use the triggering identity's permissions.
  3. Set the Subscription's channel.endpoint to Bot/<bot-id> and its status to active once the Bot is ready.

For the Patient creation example, the Bot receives the created Patient as event.input. Follow Bot Basics for the handler signature and automatic execution to try the sample handler.

Use your own endpoint​

For the Patient creation example, keep its criteria and create interaction extension, and replace its channel with:

{
  "type": "rest-hook",
  "endpoint": "https://your-service.example.com/medplum/patient-created",
  "payload": "application/fhir+json"
}

Medplum sends an HTTP POST containing the created Patient as FHIR JSON. Add the Subscription signature extension alongside the interaction extension and configure the same secret in your receiver. Verify the X-Signature header against the received request body and reject missing or invalid signatures before handling a notification. Enable the Subscription by setting status to active after deploying and verifying the receiver.

If the service needs to call the Medplum API, use a ClientApplication with client credentials and an AccessPolicy limited to the required resources and actions. Keep credentials on the server.

Failed HTTPS deliveries can be retried according to the Subscription retry policy. Configure the retry limit for your receiver and keep notification handling safe to repeat.

Return a successful response after handling the notification. If you use a queue, durably save the event or work reference before acknowledging it, then track worker failures in your service. A successful HTTP response tells Medplum that delivery succeeded; your service remains responsible for any queued action.

Handle repeated and delayed notifications​

HTTPS delivery retries and manual resends can repeat a notification. For Bot destinations, setup errors can retry, but failed handler logic does not automatically trigger a Subscription retry. Use monitoring and recovery to recover failed actions.

  1. Identify the intended action. Give it a stable identifier so a repeated notification or manual resend can reuse the result. For example, an integration that registers each Patient once can use that Patient's resource ID as part of the action's identity.
  2. Prevent duplicate effects. For generated FHIR resources, use identifier-based conditional creation. Protect external actions separately with the destination API's idempotency mechanism, and reconcile uncertain outcomes before retrying.
  3. Account for later changes. A resource may have changed again before the handler runs. Re-read current state when the action depends on current eligibility.

Subscriptions do not provide an ordered, permanent event history; an integration that requires every historical change needs its own retained records and recovery design.

Monitor failures and recover​

Subscription delivery records and Bot execution logs help diagnose failures. Confirm the intended action's completion using the resource or external system your handler updates.

Choose the recovery path for your destination:

  • Bot: Inspect execution failures with Monitoring and Recovering Bots. A failed Bot handler can be recorded in its AuditEvent without triggering a Subscription retry. Setup errors can still retry, so keep the handler safe to repeat. Recover failed actions from the source resource and evidence of what already succeeded.
  • HTTPS endpoint: Configure the HTTP delivery retry policy and monitor delivery failures. Retries are bounded and can be skipped when a newer resource version exists or the Subscription is inactive. Your service owns retries for actions it has already acknowledged.

After fixing a failure, an authorized administrator can use $resend to send selected Subscription notifications again. Inspect existing effects before replaying an action.

For the create-only Patient example, send POST /fhir/R4/Patient/<patient-id>/$resend with this body, replacing <subscription-id> with the Subscription's ID:

{
  "interaction": "create",
  "subscription": "Subscription/<subscription-id>"
}

Specify create because $resend defaults to update, which this Subscription excludes. The instance URL uses the current resource version. To replay a particular historical version, use the versioned $resend URL and review its delivery limitations. If notifications were missed, reconcile source resources against the destination's results.

Verify before enabling in production​

In a test project, verify:

  • A new Patient reaches the selected destination; updating that Patient does not trigger this create-only Subscription.
  • The handler has the required permissions, and the Subscription's account scope includes the intended resources.
  • A repeated notification preserves the intended result without duplicating the action.
  • A handler failure is visible, and your recovery procedure completes the missing action safely.

For the transition variant, also test an update into completed, a later edit while still completed, and creation in the completed state according to your chosen policy. For an HTTPS receiver, verify that missing or invalid signatures are rejected.

Follow unit tests with a test of the deployed Subscription and destination.

For other Bot triggers, see direct invocation, incoming webhooks, and scheduled Bots.