Explore documentation

Creating MedicationRequests via API

How to create and manage medication orders in Esus

Set up the SDK server client to run the examples using esus. The REST examples use a session token. To run them with the SDK’s API key, send X-API-Key instead of Authorization.

Creating a MedicationRequest

POST /fhir/MedicationRequest with a Bearer token (or X-API-Key with the MedicationRequest.create scope). Only intent and subjectId are required:

REST
curl -X POST https://api.esus.health/fhir/MedicationRequest \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/fhir+json" \
  -d '{
    "status": "active",
    "intent": "order",
    "priority": "routine",
    "medicationCodeableConcept": {
      "coding": [{
        "system": "http://www.nlm.nih.gov/research/umls/rxnorm",
        "code": "197361",
        "display": "Amoxicillin 500mg capsule"
      }]
    },
    "subjectId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
    "requesterId": "a7b1c2d3-e4f5-6789-abcd-ef0123456789",
    "dosageInstruction": [{
      "text": "Take 1 capsule by mouth 3 times a day for 10 days",
      "timing": {
        "repeat": {
          "frequency": 3,
          "period": 1,
          "periodUnit": "d"
        }
      },
      "route": {
        "coding": [{
          "system": "http://snomed.info/sct",
          "code": "26643006",
          "display": "Oral"
        }]
      },
      "doseAndRate": [{
        "doseQuantity": { "value": 500, "unit": "mg" }
      }]
    }]
  }'
SDK
const created = await esus.fhir.create("MedicationRequest", {
  "status": "active",
  "intent": "order",
  "priority": "routine",
  "medicationCodeableConcept": {
    "coding": [
      {
        "system": "http://www.nlm.nih.gov/research/umls/rxnorm",
        "code": "197361",
        "display": "Amoxicillin 500mg capsule"
      }
    ]
  },
  "subjectId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
  "requesterId": "a7b1c2d3-e4f5-6789-abcd-ef0123456789",
  "dosageInstruction": [
    {
      "text": "Take 1 capsule by mouth 3 times a day for 10 days",
      "timing": {
        "repeat": {
          "frequency": 3,
          "period": 1,
          "periodUnit": "d"
        }
      },
      "route": {
        "coding": [
          {
            "system": "http://snomed.info/sct",
            "code": "26643006",
            "display": "Oral"
          }
        ]
      },
      "doseAndRate": [
        {
          "doseQuantity": {
            "value": 500,
            "unit": "mg"
          }
        }
      ]
    }
  ]
});

Other accepted fields: encounterId, identifier[], category[], medicationReference, authoredOn, reasonCode[], note[]. Fields outside this model are not stored.

Response

A successful creation returns 201 Created with the resource (abbreviated):

{
  "resourceType": "MedicationRequest",
  "id": "c1a2e3d4-5b6f-7890-abcd-ef1234567890",
  "meta": {
    "versionId": "1",
    "lastUpdated": "2026-04-21T10:30:00.000Z"
  },
  "status": "active",
  "intent": "order",
  "priority": "routine",
  "subject": { "reference": "Patient/3fa85f64-5717-4562-b3fc-2c963f66afa6" },
  "requester": { "reference": "Practitioner/a7b1c2d3-e4f5-6789-abcd-ef0123456789" },
  "dosageInstruction": [{ "text": "Take 1 capsule by mouth 3 times a day for 10 days", "...": "..." }]
}

Flat input refs vs. nested output refs: subjectId, encounterId and requesterId are input-only. The response serializes them as standard FHIR references (subject, encounter, requester).

Searching

REST
curl "https://api.esus.health/fhir/MedicationRequest?patient=3fa85f64-5717-4562-b3fc-2c963f66afa6&status=active" \
  -H "Authorization: Bearer YOUR_TOKEN"
SDK
const { resources, total } = await esus.fhir.search("MedicationRequest", {
  "patient": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
  "status": "active"
});
ParameterMatching
patient / subjectExact patient id (<uuid> or Patient/<uuid>)
encounter, requester, _idExact id
status, intent, priorityExact code
medicationPartial text match on medicationCodeableConcept or medicationReference (e.g. an RxNorm code or display)
categoryPartial text match
dateauthoredOn date (YYYY-MM-DD); without a prefix it matches the whole day. Supports gt, ge, lt, le and ne; repeat it for a range

Other parameters are ignored.

Updating the Order

Change status with a PATCH (or a PUT, which keeps omitted fields):

curl -X PATCH https://api.esus.health/fhir/MedicationRequest/c1a2e3d4-5b6f-7890-abcd-ef1234567890 \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -H 'If-Match: W/"1"' \
  -d '{ "status": "stopped" }'

If the If-Match version is not the current one, the API returns 412.

Status Values

StatusMeaning
draftNot yet active
activeThe order is currently active (default)
on-holdTemporarily suspended
completedThe order has been fulfilled
cancelledThe order was cancelled before any action
stoppedThe order was stopped early
entered-in-errorRecorded in error
unknownStatus unknown

Error Handling

Errors come back as an OperationOutcome whose diagnostics carries the (English) message.

CodeMeaning
400A referenced Patient/Encounter/Practitioner doesn’t exist in your organization, or a PATCH body fails schema validation
401Missing or invalid token / API key
403Your role or API-key scopes don’t allow this action
404The MedicationRequest doesn’t exist
412If-Match version mismatch
422POST/PUT body fails schema validation (missing intent/subjectId, invalid status, wrong types)