Medications
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:
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" }
}]
}]
}'
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,encounterIdandrequesterIdare input-only. The response serializes them as standard FHIR references (subject,encounter,requester).
Searching
curl "https://api.esus.health/fhir/MedicationRequest?patient=3fa85f64-5717-4562-b3fc-2c963f66afa6&status=active" \
-H "Authorization: Bearer YOUR_TOKEN"
const { resources, total } = await esus.fhir.search("MedicationRequest", {
"patient": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"status": "active"
});
| Parameter | Matching |
|---|---|
patient / subject | Exact patient id (<uuid> or Patient/<uuid>) |
encounter, requester, _id | Exact id |
status, intent, priority | Exact code |
medication | Partial text match on medicationCodeableConcept or medicationReference (e.g. an RxNorm code or display) |
category | Partial text match |
date | authoredOn 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
| Status | Meaning |
|---|---|
draft | Not yet active |
active | The order is currently active (default) |
on-hold | Temporarily suspended |
completed | The order has been fulfilled |
cancelled | The order was cancelled before any action |
stopped | The order was stopped early |
entered-in-error | Recorded in error |
unknown | Status unknown |
Error Handling
Errors come back as an OperationOutcome whose diagnostics carries the (English) message.
| Code | Meaning |
|---|---|
400 | A referenced Patient/Encounter/Practitioner doesn’t exist in your organization, or a PATCH body fails schema validation |
401 | Missing or invalid token / API key |
403 | Your role or API-key scopes don’t allow this action |
404 | The MedicationRequest doesn’t exist |
412 | If-Match version mismatch |
422 | POST/PUT body fails schema validation (missing intent/subjectId, invalid status, wrong types) |