Primeros pasos
Actualizar y eliminar pacientes
Guardá cambios sin pisar los de otros, consultá versiones anteriores y eliminá registros de prueba.
Ya sabés crear y leer un Patient. Ahora vamos a cambiarlo, ver su historial y eliminarlo. Usá solo datos ficticios en estos ejemplos.
Configurá el cliente de servidor del SDK para usar los ejemplos con esus. Usá el ID del paciente ficticio como patientId.
Actualizar (PUT)
PUT combina los campos que enviás con el recurso guardado: los campos que omitas no se borran. Podés enviar el recurso completo o solo lo que cambia.
curl -X PUT https://api.esus.health/fhir/Patient/3fa85f64-5717-4562-b3fc-2c963f66afa6 \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/fhir+json" \
-H 'If-Match: W/"1"' \
-d '{
"resourceType": "Patient",
"name": [
{ "use": "official", "family": "Pérez", "given": ["Juan", "Miguel"] }
],
"birthDate": "1990-01-16"
}'
const { etag } = await esus.fhir.read("Patient", patientId);
if (!etag) throw new Error("Missing Patient ETag");
await esus.fhir.update("Patient", patientId, {
"name": [
{ "use": "official", "family": "Pérez", "given": ["Juan", "Miguel"] }
],
"birthDate": "1990-01-16"
}, etag);
Las lecturas, PUT y PATCH devuelven el header ETag con la versión (W/"2"). Si mandás If-Match y alguien guardó otra versión antes, la API responde 412 Precondition Failed en lugar de pisar su cambio. El header es opcional, pero conviene usarlo cuando varias personas editan el mismo paciente.
Cambios puntuales (PATCH)
PATCH acepta tres formatos, según el Content-Type:
| Content-Type | Cuerpo |
|---|---|
application/fhir+json o application/json | Un Patient parcial con los campos a cambiar |
application/json-patch+json | Operaciones JSON Patch (RFC 6902) |
application/merge-patch+json | Un JSON Merge Patch (RFC 7386) |
curl -X PATCH https://api.esus.health/fhir/Patient/3fa85f64-5717-4562-b3fc-2c963f66afa6 \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json-patch+json" \
-d '[
{ "op": "replace", "path": "/gender", "value": "other" }
]'
const { etag } = await esus.fhir.read("Patient", patientId);
if (!etag) throw new Error("Missing Patient ETag");
await esus.fhir.patchJson("Patient", patientId, [
{ op: "replace", path: "/gender", value: "other" },
], etag);
PATCH también respeta If-Match y devuelve el recurso actualizado.
Consultar versiones anteriores
Cada cambio crea una versión nueva:
# Todas las versiones
GET /fhir/Patient/3fa85f64-5717-4562-b3fc-2c963f66afa6/_history
# Una versión específica
GET /fhir/Patient/3fa85f64-5717-4562-b3fc-2c963f66afa6/_history/2
const history = await esus.fhir.history("Patient", patientId);
const version = await esus.fhir.readVersion("Patient", patientId, "2");
Eliminar (DELETE)
curl -X DELETE https://api.esus.health/fhir/Patient/3fa85f64-5717-4562-b3fc-2c963f66afa6 \
-H "Authorization: Bearer YOUR_TOKEN"
await esus.fhir.delete("Patient", patientId);
La API responde 204 No Content y el paciente se elimina de forma definitiva: después, tanto la lectura como _history devuelven 404 Not Found.
Entender una respuesta de error
| Código | Significado |
|---|---|
| 200 OK | Lectura o actualización exitosa |
| 201 Created | Creación exitosa |
| 204 No Content | Eliminación exitosa |
| 400 Bad Request | Parámetros inválidos |
| 401 Unauthorized | Falta la credencial o no es válida |
| 403 Forbidden | La credencial no tiene permiso para esa acción |
| 404 Not Found | El recurso no existe |
| 412 Precondition Failed | If-Match no coincide con la versión actual |
| 422 Unprocessable Entity | El cuerpo no respeta el esquema del recurso |
Los errores llegan como OperationOutcome. Por ejemplo, un If-Match desactualizado:
{
"resourceType": "OperationOutcome",
"issue": [
{
"severity": "error",
"code": "conflict",
"diagnostics": "Version mismatch — re-read the resource and retry with the current versionId"
}
]
}
Validar sin guardar
$validate revisa un recurso de cualquier tipo y no lo guarda:
curl -X POST https://api.esus.health/fhir/\$validate \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/fhir+json" \
-d '{ "resourceType": "Patient", "gender": "masculino" }'
const { body: outcome } = await esus.fhir.operation("POST /fhir/$validate", {
body: { "resourceType": "Patient", "gender": "masculino" },
});
La respuesta es un OperationOutcome con los problemas encontrados.
Seguir practicando
Ya recorriste los primeros pasos. Si querés repasar lo aprendido, seguí con las preguntas de práctica. Tu avance se guarda en este navegador cuando elegís el modo Curso.