gRPC API
The VPR gRPC API provides high-performance, type-safe access to all patient record operations.
Overview
The gRPC API is built using:
- tonic 0.12 - Rust gRPC framework
- Protocol Buffers - For message serialization
- Authentication - API key-based authentication via x-api-key header
Service Definition
The API is defined in crates/api-shared/vpr.proto.
Service: VPR
All RPC methods are grouped under the vpr.v1.VPR service.
Authentication
All requests require an x-api-key header:
grpcurl -H 'x-api-key: YOUR_API_KEY' localhost:50051 vpr.v1.VPR/Health
The API key is configured via the API_KEY environment variable.
Available RPCs
Health Check
Health- Returns service health status
Patient Management
CreatePatient- Creates a new patient record (legacy)ListPatients- Lists all patientsInitialiseFullRecord- Creates complete patient record (demographics, clinical, coordination)
Demographics
InitialiseDemographics- Initialises new demographics repositoryUpdateDemographics- Updates patient demographics (given names, last name, birth date)
Clinical
InitialiseClinical- Initialises new clinical repositoryLinkToDemographics- Links clinical repository to demographics via EHR statusNewLetter- Creates new clinical letter with markdown contentReadLetter- Retrieves letter content and metadataNewLetterWithAttachments- Creates letter with binary file attachmentsGetLetterAttachments- Retrieves letter attachments (metadata and binary content)
Coordination
InitialiseCoordination- Initialises new coordination repositoryCreateThread- Creates messaging thread with participantsAddMessage- Adds message to existing threadReadCommunication- Reads thread with ledger and all messagesUpdateCommunicationLedger- Updates thread participants, status, visibilityUpdateCoordinationStatus- Updates coordination lifecycle state and flags
Example Usage with grpcurl
Create Full Patient Record
grpcurl -plaintext -import-path crates/api-shared -proto vpr.proto \
-d '{
"given_names": ["Emily"],
"last_name": "Davis",
"birth_date": "1985-03-20",
"author_name": "Dr. Robert Brown",
"author_email": "robert.brown@example.com",
"author_role": "Clinician",
"author_registrations": [{"authority": "GMC", "number": "5555555"}],
"care_location": "City General Hospital"
}' \
-H 'x-api-key: YOUR_API_KEY' \
localhost:50051 vpr.v1.VPR/InitialiseFullRecord
Create Letter
grpcurl -plaintext -import-path crates/api-shared -proto vpr.proto \
-d '{
"clinical_uuid": "a701c3a94bf34a939d831d6183a78734",
"author_name": "Dr. Sarah Johnson",
"author_email": "sarah.johnson@example.com",
"author_role": "Clinician",
"author_registrations": [{"authority": "GMC", "number": "7654321"}],
"care_location": "GP Clinic",
"content": "# Consultation\\n\\nPatient presented with hypertension."
}' \
-H 'x-api-key: YOUR_API_KEY' \
localhost:50051 vpr.v1.VPR/NewLetter
Create Letter with Attachments
Binary attachments are sent as base64-encoded bytes:
# Encode file to base64
base64 -i /path/to/file.pdf
grpcurl -plaintext -import-path crates/api-shared -proto vpr.proto \
-d '{
"clinical_uuid": "a701c3a94bf34a939d831d6183a78734",
"author_name": "Dr. Chen",
"author_email": "chen@example.com",
"author_role": "Clinician",
"care_location": "Hospital Lab",
"attachment_files": ["<base64_content>"],
"attachment_names": ["lab_results.pdf"]
}' \
-H 'x-api-key: YOUR_API_KEY' \
localhost:50051 vpr.v1.VPR/NewLetterWithAttachments
Create Communication Thread
grpcurl -plaintext -import-path crates/api-shared -proto vpr.proto \
-d '{
"coordination_uuid": "da7e89a2a51647db89430dc3a781abb0",
"author_name": "Dr. Brown",
"author_email": "brown@example.com",
"author_role": "Clinician",
"care_location": "City Hospital",
"participants": [
{"id": "a701c3a94bf34a939d831d6183a78734", "name": "Dr. Brown", "role": "clinician"},
{"id": "d4c6547ee14a4255a568aa66d7335561", "name": "Emily Davis", "role": "patient"}
],
"initial_message_body": "Consultation scheduled.",
"initial_message_author": {
"id": "a701c3a94bf34a939d831d6183a78734",
"name": "Dr. Brown",
"role": "clinician"
}
}' \
-H 'x-api-key: YOUR_API_KEY' \
localhost:50051 vpr.v1.VPR/CreateThread
Message Types
Key message types defined in the protocol:
Author Registration
message AuthorRegistration {
string authority = 1; // e.g., "GMC", "NMC"
string number = 2; // Registration number
}
Message Author
message MessageAuthor {
string id = 1; // UUID
string name = 2; // Display name
string role = 3; // clinician, patient, system, etc.
}
Lifecycle States
Coordination lifecycle states:
active- Operational and accepting updatessuspended- Temporarily inactiveclosed- Permanently closed
Thread statuses:
open- Active communicationclosed- Concluded communicationarchived- Historical record
Sensitivity levels:
standard- Normal clinical communicationconfidential- Elevated privacyrestricted- Highest privacy level
Server Configuration
The gRPC server runs on port 50051 by default. Configuration via environment variables:
VPR_ADDR- Server bind address (default:0.0.0.0:50051)API_KEY- Required API key for authenticationVPR_ENABLE_REFLECTION- Enable gRPC reflection (default:false)RUST_LOG- Logging configuration
Implementation
The gRPC service is implemented in crates/api-grpc/src/service.rs.
Key characteristics:
- Authentication interceptor - Validates API key on all requests
- Author construction - Builds
Authorobjects from proto fields - Error handling - Maps Rust errors to gRPC status codes
- File handling - Writes attachments to temp directory, uses FilesService, cleans up
- Type conversions - Converts string enums to Rust enums (AuthorRole, ThreadStatus, etc.)
Error Handling
gRPC status codes used:
OK- SuccessUNAUTHENTICATED- Invalid or missing API keyINVALID_ARGUMENT- Invalid input parametersNOT_FOUND- Resource not foundINTERNAL- Server error
Error messages include descriptive details for debugging.