Explore documentation

Make server requests with the SDK

Install the SDK, verify a connection, and make a typed FHIR request from your server.

Install the SDK

Use Node.js 22 or later and ES modules. Install the SDK in your server project.

npm install @esushealth/sdk

Use a test organization, synthetic records, and an API key with Patient.read. Before serving requests from your application, enforce your own user’s permissions. The SDK uses the permissions of the credential you give it.

Make your first request

Set ESUS_ORGANIZATION_ID to the organization UUID and ESUS_API_KEY to the complete Client ID:Secret in your server environment. Set ESUS_APP_ID if the key is scoped to an app. These variable names are for this example. The SDK takes their values as constructor options.

For the SDK, save the example as first-request.mjs. It verifies the organization, requests up to five patients, and logs the organization UUID and result count without printing patient records.

REST
headers=(
  -H "X-API-Key: $ESUS_API_KEY"
  -H "X-Esus-Expected-Organization: $ESUS_ORGANIZATION_ID"
)
if [[ -n ${ESUS_APP_ID:-} ]]; then
  headers+=(-H "X-App-Id: $ESUS_APP_ID")
fi

curl --fail-with-body --silent --show-error "${headers[@]}" \
  'https://api.esus.health/v1/integration/connection?resources=Patient' &&
curl --fail-with-body --silent --show-error "${headers[@]}" \
  'https://api.esus.health/fhir/Patient?_count=5'
SDK
import { createServiceClient } from "@esushealth/sdk/server";

const organizationId = process.env.ESUS_ORGANIZATION_ID;
const apiKey = process.env.ESUS_API_KEY;
if (!organizationId || !apiKey) {
  throw new Error("Set ESUS_ORGANIZATION_ID and ESUS_API_KEY.");
}

const esus = createServiceClient({
  url: "https://api.esus.health",
  organizationId,
  apiKey,
  appId: process.env.ESUS_APP_ID,
});

const proof = await esus.validateConnection({ resources: ["Patient"] });
const { resources } = await esus.fhir.search("Patient", { _count: 5 });
console.log({ organizationId: proof.organizationId, returned: resources.length });

Run node first-request.mjs with those variables set. If you stored them in an untracked local env file, run node --env-file=.env.local first-request.mjs.

REST verifies the connection first and returns a search Bundle. The SDK example extracts the organization UUID and result count without printing records.

Read and update FHIR resources

Use fhir.search(resource, filters) to get resources and, when supplied by the API, total. The exported fhirResources array lists the supported types. The SDK types the filters and result for the chosen resource.

For a single patient, fhir.read("Patient", id) returns patient and etag. Other resource reads return resource and etag. Pass the current ETag to fhir.update, fhir.patch, or fhir.patchJson. If the API returns HTTP 412, reload the record and review the changes before saving again.

Use delegated access or app sessions

  • SMART on FHIR. Use createSmartAuthorization to start authorization with PKCE and exchangeSmartCode to exchange the callback code using the stored transaction. Your server must bind the transaction to the browser session and consume it once. Use createSmartClient with the organization UUID, client ID, API origin, and access token. The client defaults to reads. Esus applies the token’s scopes and launch context on every request.
  • App user sessions. Use createAppSessionClient({ url, appId }) for tenant app login and session methods. This part of the SDK is experimental. Store tokens server-side and persist both tokens returned by a successful refresh. Coordinate concurrent refresh calls in your app.

See Authentication for credential setup and the HTTP flows.

Handle failures

EsusError includes the HTTP status, plus a request ID and FHIR OperationOutcome when available. A status of 0 means the request failed before an HTTP response was received. A failed write with no response may still have reached Esus. Check the resulting state before repeating the write.

See the SDK package reference for files, webhooks, public health and plans, and tenant app management.