Platform
Pharmacy
Managing medication stock and dispensing with the Pharmacy API
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.
The Pharmacy API keeps a stock count for each medication + location pair. You can restock, dispense (with a stock check), adjust manually, and list items that are low on stock or close to expiring. Endpoints live under /pharmacy and return plain JSON stock rows, not FHIR resources.
Access: for users, the admin, pharmacist, and nurse roles can use these endpoints. API keys need Pharmacy.read for GET routes and Pharmacy.create for POST routes.
The Stock Row
Every endpoint returns stock rows with this shape:
{
"id": "cc33dd44-ee55-ff66-7788-990011223344",
"organizationId": "11112222-3333-4444-5555-666677778888",
"medicationId": "aa11bb22-cc33-dd44-ee55-ff6677889900",
"locationId": "bb22cc33-dd44-ee55-ff66-778899001122",
"quantity": 250,
"minQuantity": 0,
"maxQuantity": null,
"lotNumber": "LOT-2024-001",
"expirationDate": "2025-06-30",
"lastRestockedAt": "2024-01-10T08:00:00.000Z",
"createdAt": "2023-11-01T08:00:00.000Z",
"updatedAt": "2024-01-10T08:00:00.000Z"
}
medicationId is the id of a FHIR Medication, and locationId is the id of a Location. There is a single row per medication and location, so lotNumber and expirationDate hold the values from the most recent restock. Separate lots are not tracked.
List Stock
curl https://api.esus.health/pharmacy/stock \
-H "Authorization: Bearer TOKEN"
# Optional filters: locationId, medicationId
curl "https://api.esus.health/pharmacy/stock?locationId=bb22cc33-dd44-ee55-ff66-778899001122" \
-H "Authorization: Bearer TOKEN"
const stock = await esus.pharmacy.stock();
const atLocation = await esus.pharmacy.stock({
locationId: "bb22cc33-dd44-ee55-ff66-778899001122",
});
Returns an array of stock rows.
Restock
curl -X POST https://api.esus.health/pharmacy/restock \
-H "Authorization: Bearer TOKEN" \
-H "Content-Type: application/json" \
-d '{
"medicationId": "aa11bb22-cc33-dd44-ee55-ff6677889900",
"locationId": "bb22cc33-dd44-ee55-ff66-778899001122",
"quantity": 500,
"lotNumber": "LOT-2024-002",
"expirationDate": "2026-01-31"
}'
const result = await esus.pharmacy.restock({
"medicationId": "aa11bb22-cc33-dd44-ee55-ff6677889900",
"locationId": "bb22cc33-dd44-ee55-ff66-778899001122",
"quantity": 500,
"lotNumber": "LOT-2024-002",
"expirationDate": "2026-01-31"
});
Body: medicationId, locationId, and quantity (≥ 1) are required. lotNumber and expirationDate (YYYY-MM-DD) are optional. If the row exists, quantity is added to it and lastRestockedAt is updated. Otherwise, a new row is created. Returns 201 Created with the stock row.
Dispense
Checks that enough stock is available and deducts it:
curl -X POST https://api.esus.health/pharmacy/dispense \
-H "Authorization: Bearer TOKEN" \
-H "Content-Type: application/json" \
-d '{
"medicationId": "aa11bb22-cc33-dd44-ee55-ff6677889900",
"locationId": "bb22cc33-dd44-ee55-ff66-778899001122",
"quantity": 30
}'
const result = await esus.pharmacy.dispense({
"medicationId": "aa11bb22-cc33-dd44-ee55-ff6677889900",
"locationId": "bb22cc33-dd44-ee55-ff66-778899001122",
"quantity": 30
});
Body: medicationId, locationId, and quantity (≥ 1) are required. The schema also accepts optional patientId, encounterId, and note, but they are not stored. To record who received the medication, create a MedicationDispense (see below).
Response: 200 OK with the updated stock row (quantity already decremented).
If the stock is insufficient, or there is no row for that medication and location, the API returns 400 Bad Request:
{
"resourceType": "OperationOutcome",
"issue": [{
"severity": "error",
"code": "invalid",
"diagnostics": "Insufficient stock or stock entry not found for the specified medication and location"
}]
}
Manual Adjustment
For inventory corrections, damaged stock, or expired write-offs. quantity is a signed delta: positive adds, negative removes.
curl -X POST https://api.esus.health/pharmacy/adjust \
-H "Authorization: Bearer TOKEN" \
-H "Content-Type: application/json" \
-d '{
"medicationId": "aa11bb22-cc33-dd44-ee55-ff6677889900",
"locationId": "bb22cc33-dd44-ee55-ff66-778899001122",
"quantity": -15,
"reason": "Damaged stock disposal"
}'
const result = await esus.pharmacy.adjust({
"medicationId": "aa11bb22-cc33-dd44-ee55-ff6677889900",
"locationId": "bb22cc33-dd44-ee55-ff66-778899001122",
"quantity": -15,
"reason": "Damaged stock disposal"
});
Body: medicationId, locationId, quantity, and reason are all required. reason is validated but not stored. The resulting quantity never goes below 0. If no stock row exists for that pair, the API returns 404. Returns 200 OK with the updated row.
Low Stock
Returns rows whose quantity is at or below minQuantity (treated as 0 when unset). Optional query filter: locationId.
curl https://api.esus.health/pharmacy/low-stock \
-H "Authorization: Bearer TOKEN"
const lowStock = await esus.pharmacy.lowStock();
New rows start with minQuantity: 0, and the API has no endpoint to change it. In practice, this list shows items that are out of stock.
Expiring Items
Returns rows with an expirationDate on or before today + days (default 30). This includes items that have already expired. There is no location filter.
curl "https://api.esus.health/pharmacy/expiring?days=90" \
-H "Authorization: Bearer TOKEN"
const expiring = await esus.pharmacy.expiring({ days: 90 });
Recording the Dispense in FHIR
The stock endpoints only move counts. For the clinical record, create a MedicationDispense. Patient and encounter go as flat ids (subjectId, encounterId):
curl -X POST https://api.esus.health/fhir/MedicationDispense \
-H "Authorization: Bearer TOKEN" \
-H "Content-Type: application/fhir+json" \
-d '{
"resourceType": "MedicationDispense",
"status": "completed",
"medicationCodeableConcept": {
"coding": [{ "system": "http://snomed.info/sct", "code": "372687004" }]
},
"subjectId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"authorizingPrescription": [{ "reference": "MedicationRequest/c1a2e3d4-5b6f-7890-abcd-ef1234567890" }],
"quantity": { "value": 30, "unit": "capsule" },
"whenHandedOver": "2024-01-15T11:00:00Z"
}'
const created = await esus.fhir.create("MedicationDispense", {
"status": "completed",
"medicationCodeableConcept": {
"coding": [
{
"system": "http://snomed.info/sct",
"code": "372687004"
}
]
},
"subjectId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"authorizingPrescription": [
{
"reference": "MedicationRequest/c1a2e3d4-5b6f-7890-abcd-ef1234567890"
}
],
"quantity": {
"value": 30,
"unit": "capsule"
},
"whenHandedOver": "2024-01-15T11:00:00Z"
});