Medplum (medplum_elixir v0.2.0)

Copy Markdown View Source

Concise client for Medplum's FHIR API.

Build a Medplum.Client, then pass it to the resource helpers in this module for authenticated FHIR requests. Public operations return {:ok, map()} for decoded response bodies or {:error, %Medplum.Error{}} for transport, auth, or API failures.

Clients reuse cached OAuth tokens by default, accept shared Req options, and expose both resource helpers and lower-level request functions for unsupported endpoints.

Example

client =
  Medplum.new(
    base_url: "https://api.medplum.com",
    client_id: System.fetch_env!("MEDPLUM_CLIENT_ID"),
    client_secret: System.fetch_env!("MEDPLUM_CLIENT_SECRET")
  )

{:ok, patient} = Medplum.read(client, "Patient", "123")

{:ok, patients} = Medplum.search(client, "Patient", %{"family" => "Smith"})

Summary

Functions

Executes a FHIR batch bundle against the base endpoint.

Creates a FHIR resource and fills in "resourceType" when it is omitted.

Uploads raw bytes to the Binary endpoint.

Deletes a FHIR resource by resource type and id.

Downloads raw bytes from a Binary resource.

Sends a GraphQL request to the FHIR GraphQL endpoint.

Builds a reusable Medplum client from base URL and client credentials.

Builds a reusable Medplum client and raises on invalid configuration.

Invokes a FHIR operation on the system, type, or instance level.

Applies a JSON Patch document to a resource by type and id.

Polls an async status URL until a final response is available.

Fetches one FHIR resource by resource type and id.

Sends an authenticated request to any FHIR-relative path or absolute Medplum URL.

Searches a FHIR resource type with query parameters encoded from the given map.

Streams Bundle entries across paginated search results.

Executes a FHIR transaction bundle against the base endpoint.

Replaces a FHIR resource by type and id, forcing the outgoing body to match both.

Performs an official Medplum conditional upsert using a query embedded in attrs.

Types

async_result()

@type async_result() :: %{
  required(String.t()) => term(),
  optional(String.t()) => term()
}

binary_result()

@type binary_result() :: %{
  body: binary(),
  content_type: String.t() | nil,
  headers: [{String.t(), String.t()}]
}

client()

@type client() :: Medplum.Client.t()

result()

@type result() :: {:ok, map()} | {:error, Medplum.Error.t()}

Functions

batch(client, entries_or_bundle)

@spec batch(Medplum.Client.t(), [map()] | map()) :: result() | {:ok, async_result()}

Executes a FHIR batch bundle against the base endpoint.

create(client, resource_type, attrs)

@spec create(Medplum.Client.t(), String.t(), map()) :: result()

Creates a FHIR resource and fills in "resourceType" when it is omitted.

create_binary(client, data, opts)

@spec create_binary(Medplum.Client.t(), binary(), keyword()) :: result()

Uploads raw bytes to the Binary endpoint.

delete(client, resource_type, id)

@spec delete(Medplum.Client.t(), String.t(), String.t()) :: result()

Deletes a FHIR resource by resource type and id.

get_binary(client, id_or_url)

@spec get_binary(Medplum.Client.t(), String.t()) ::
  {:ok, binary_result()} | {:error, Medplum.Error.t()}

Downloads raw bytes from a Binary resource.

graphql(client, query, opts \\ [])

@spec graphql(Medplum.Client.t(), String.t(), keyword()) :: result()

Sends a GraphQL request to the FHIR GraphQL endpoint.

new(opts)

@spec new(keyword() | map()) :: Medplum.Client.t()

Builds a reusable Medplum client from base URL and client credentials.

new!(opts)

@spec new!(keyword() | map()) :: Medplum.Client.t()

Builds a reusable Medplum client and raises on invalid configuration.

operation(client, target, operation_name, params \\ %{}, opts \\ [])

@spec operation(
  Medplum.Client.t(),
  :system | String.t() | {String.t(), String.t()},
  String.t(),
  map(),
  keyword()
) :: result() | {:ok, async_result()}

Invokes a FHIR operation on the system, type, or instance level.

patch(client, resource_type, id, operations)

@spec patch(Medplum.Client.t(), String.t(), String.t(), [map()]) :: result()

Applies a JSON Patch document to a resource by type and id.

poll_async(client, status_url_or_path, opts \\ [])

@spec poll_async(Medplum.Client.t(), String.t(), keyword()) ::
  result() | {:ok, async_result()}

Polls an async status URL until a final response is available.

read(client, resource_type, id)

@spec read(Medplum.Client.t(), String.t(), String.t()) :: result()

Fetches one FHIR resource by resource type and id.

request(client, method, path, opts \\ [])

@spec request(Medplum.Client.t(), atom(), String.t(), keyword()) :: result()

Sends an authenticated request to any FHIR-relative path or absolute Medplum URL.

search(client, resource_type, params \\ %{})

@spec search(Medplum.Client.t(), String.t(), map()) :: result()

Searches a FHIR resource type with query parameters encoded from the given map.

stream_search(client, resource_type, params \\ %{})

@spec stream_search(Medplum.Client.t(), String.t(), map()) :: Enumerable.t()

Streams Bundle entries across paginated search results.

The stream yields each Bundle entry map and raises Medplum.Error if a later page fails.

transaction(client, entries_or_bundle)

@spec transaction(Medplum.Client.t(), [map()] | map()) ::
  result() | {:ok, async_result()}

Executes a FHIR transaction bundle against the base endpoint.

update(client, resource_type, id, attrs)

@spec update(Medplum.Client.t(), String.t(), String.t(), map()) :: result()

Replaces a FHIR resource by type and id, forcing the outgoing body to match both.

upsert(client, resource_type, attrs)

@spec upsert(Medplum.Client.t(), String.t(), map()) :: result()

Performs an official Medplum conditional upsert using a query embedded in attrs.