Binary file upload
MedBackend stores file attachments in Azure Blob Storage and references them from FHIR resources. A single GraphQL mutation, UploadFile, accepts the file, stores it, and returns a storage path you attach to a DocumentReference or Media resource.
The UploadFile mutation
UploadFile(
data: Upload!
filename: String
forEntity: String!
): UploadFileResponse
| Argument | Required | Meaning |
|---|---|---|
data | yes | The file itself, sent as a multipart upload (see below). |
forEntity | yes | The entity the file is for, exactly in the form ResourceType/id — for example Patient/123. You may only upload for an entity you could create a DocumentReference or Media about (see Who may upload). It is not looked up on the FHIR server, so a reference to a resource that does not exist is not refused for that reason. |
filename | no | A custom name for the stored file. It determines the extension used for the stored blob. Read the stored name back from the response rather than assuming this value was used verbatim. |
The response:
type UploadFileResponse {
url: String # Temporary presigned download URL (expires in 60 minutes)
fileId: String # The unique ID of the file in storage
contentType: String # The content type of the file
filename: String # The original filename
storagePath: String # The path to use in FHIR resources (azure-blob://...)
}
Sending the request
UploadFile follows the GraphQL multipart request specification. Send a multipart/form-data POST to /graphql with three parts: operations (the query and variables), map (which form field carries which variable), and the file itself.
POST /graphql HTTP/1.1
Authorization: Bearer <access token>
X-Project-ID: <your project uid>
Content-Type: multipart/form-data; boundary=----boundary
------boundary
Content-Disposition: form-data; name="operations"
{"query": "mutation UploadFile($data: Upload!, $id: String!) { UploadFile(forEntity: $id, data: $data) { url fileId storagePath contentType } }", "variables": {"id": "Patient/123", "data": null}}
------boundary
Content-Disposition: form-data; name="map"
{"0": ["variables.data"]}
------boundary
Content-Disposition: form-data; name="0"; filename="checkup.pdf"
Content-Type: application/pdf
<file bytes>
------boundary--
Authentication is the same as for every other GraphQL request — see the Authentication documentation. The X-Project-ID header names the project the request belongs to. It is required, and it must match the project_uid claim in your access token; a mismatch is rejected before the mutation runs.
Who may upload
An upload must pass two checks, and both must pass.
- The
UploadFilerule. A built-in system rule grantsUploadFileto the Patient, Practitioner, Device and RelatedPerson client roles, in every project. To restrict uploads further, add your own rule onUploadFile/readfor the role (an upload is evaluated as that operation). A project rule is evaluated in addition to the system rule, and any rule that denies denies the request. - The attachment rule. The caller must be allowed to create a
DocumentReferenceor aMediaaboutforEntity, under your project's own rules. A file is useful only once one of those resources points at it, so an upload is refused where that create would be.- A Patient may upload wherever your rules let them create one of those resources about
forEntity. Under apatient_compartmentrule, that means their own record only. - Practitioners, devices and related persons may upload wherever your project's rules let that role create a
DocumentReferenceorMedia.
- A Patient may upload wherever your rules let them create one of those resources about
With the default Forbidden access policy, the system rule alone is not enough: a role with no DocumentReference or Media create rule cannot upload, so add one of those rules for every role that uploads.
Avoid putting patient identifiers in filenames. A filename travels with the request and is stored alongside the file, so it should not itself be clinical data.
Referencing the file from a FHIR resource
Uploading a file does not create a FHIR resource. Take storagePath from the response — it uses the azure-blob:// scheme — and set it as the attachment URL on the resource you create yourself:
mutation AttachDocument($resource: DocumentReferenceCreateInput!) {
DocumentReferenceCreate(resource: $resource) {
id
}
}
{
"resource": {
"status": "current",
"subject": { "reference": "Patient/123" },
"content": [
{
"attachment": {
"title": "Discharge summary",
"contentType": "application/pdf",
"url": "<value from storagePath>"
}
}
]
}
}
The equivalent mutation for Media is MediaCreate(resource: MediaCreateInput!), with the attachment URL at content.url.
Copy storagePath through verbatim rather than constructing it yourself. Do not store the url from the upload response: it is a temporary link that stops working after 60 minutes, whereas storagePath is permanent.
Reading the file back
When MedBackend returns a DocumentReference or Media whose attachment URL uses the azure-blob:// scheme, it attempts to replace that path with a freshly generated download URL. This happens on reads and searches, and also in the response to the create and update mutations above. Clients follow the returned URL directly to Azure Blob Storage.
A generated download URL grants read access only and expires 60 minutes after it is issued. A new one is produced each time the resource is returned, so a stale link is refreshed by re-reading the resource.
A download URL is always served as an attachment (Content-Disposition: attachment): a browser that navigates to it saves the file rather than displaying it. To show a stored image inside your own interface, use the URL as an <img> or media source. To show a PDF, fetch the bytes from your server and serve them yourself; opening the URL in a tab or frame downloads it instead.
If that step fails for any reason — most commonly because the storage credentials for it are not configured — the attachment URL is returned unchanged, as the literal azure-blob:// string. Clients should therefore handle both forms rather than assuming every attachment URL is fetchable.
Where files are stored
Files are stored under a blob path scoped to the project the upload was made in. Treat storagePath as an opaque value: its exact shape is an internal detail and is not part of the API contract.
What the platform refuses
Uploads are checked before anything is written. A refused upload leaves nothing behind: no blob is created and no storagePath is returned.
A size cap
A file larger than the configured maximum is refused, and the error names both the size and the limit:
Upload rejected: file is 214748364 bytes, the limit is 104857600 bytes.
The default limit is 104857600 bytes (100 MB). It is a platform setting, not a per-request option, and it is enforced by the API itself rather than only by whatever HTTP stack sits in front of it.
It is a resolver-level check, so the request body has already been received by the time it fires — it caps what gets stored, not what gets transferred. If you need to bound the transfer itself, do that at your proxy.
A deny-list of extensions
Checked first and unconditionally, whatever the bytes turn out to be:
| Class | Extensions |
|---|---|
| Executables and libraries | .exe .dll .so .dylib .msi .app .com .scr .jar |
| Shells and interpreted scripts | .bat .cmd .ps1 .psm1 .sh .bash .vbs .vbe .wsf .hta .js .mjs .py .php .rb .pl .lua |
| Markup a browser will execute | .svg .html .htm .xhtml .shtml |
| Archives | .zip .rar .7z .tar .gz .bz2 .xz |
The refusal reads Upload rejected: '<ext>' files are not accepted. Convert to PDF, PNG or JPEG.
The extension is worked out from the filename after normalisation — Unicode compatibility folding, trailing dots and spaces removed, and anything after the leading alphanumeric run discarded. So payload.exe., payload.exe::$DATA and a fullwidth payload.exe are all refused as .exe.
A declared-versus-actual consistency check
An extension that is not in the table below is accepted, unsniffed. What is refused is a file claiming to be one of these formats and not being it. Do not read this as a list of permitted upload types.
| Extension | Must begin with |
|---|---|
.jpg, .jpeg | JPEG signature |
.png | PNG signature |
.gif | GIF87a or GIF89a |
.tif, .tiff | TIFF signature, either byte order |
.pdf | %PDF |
.dcm | DICM, after the 128-byte preamble |
.mp4 | ftyp, at offset 4 |
A mismatch is refused with Upload rejected: the content is not a valid '<ext>' file.
A malware scan
Every upload that passes the checks above is scanned for malware before it is stored. A file the scan flags is refused with Upload rejected: the file failed the content safety scan. The response does not say what was detected.
If the scan cannot run, the upload is refused rather than stored unscanned, with Upload rejected: content scanning is unavailable. This is temporary; retry later.
The Content-Type is derived, not accepted
The Content-Type stored on the blob is what a browser obeys when it follows the download link, so it is not taken from the upload and not guessed from the filename. It is derived from the extension whose signature was just verified:
| Verified extension | Served as |
|---|---|
.jpg, .jpeg | image/jpeg |
.png | image/png |
.gif | image/gif |
.tif, .tiff | image/tiff |
.pdf | application/pdf |
.dcm | application/dicom |
.mp4 | video/mp4 |
Anything whose type was not verified — which includes every accepted extension outside the table above — is stored as application/octet-stream, so it downloads rather than renders. An unrecognised file type is still accepted; it simply does not get to choose how a browser treats it.
The caller remains responsible for sending something the consuming client can actually render.
What the API does not do
The upload API is deliberately small. It has no options for server-side encryption type, custom link lifetimes, or IP allow-lists, and there is no separate SAS token, expiry timestamp, or content hash in the response — the download link in url already carries its own credentials and expiry. It does not let the caller choose the Content-Type the file is served back as — see What the platform refuses above. MedBackend does not delete unreferenced uploads on a schedule, so deleting a FHIR resource does not remove the underlying blob.