Validate file size during an API upload
Apply declared and measured upload-size gates, add SHA-256 integrity, and read the final measured byte size from Dosya’s stored-file receipt.
Last reviewed
Dosya checks the declared size before accepting bytes and measures the body while reading it. One-shot uploads require a decimal Content-Length; files above 50 MiB must use the upload-session flow.

Before you start
- Check the API key’s
max_file_size_bytes, the workspace storage quota, and the workspacemax_file_size_gbsetting. REST uploads do not apply a role-level file-size cap. - The key must reach the selected workspace, and the member role must allow
upload_filesplusaccess_uploadfor this REST upload surface. - Compute a lowercase 64-character SHA-256 digest when end-to-end byte identity matters.
- Treat
Content-Lengthas a gate, not proof of the stored size; the final receipt reflects bytes actually received.
Steps
- Measure the source before transfer. Use one-shot upload at or below 50 MiB; initialize a resumable upload above that boundary.
- For one-shot transfer, send a digits-only
Content-Length. Missing or malformed length returns 411; an amount over 50 MiB returns 413 withFiles over 50 MB use /api/upload/init and the resumable upload. - Add
X-Dosya-Sha256: digestwhen available. A mismatch returns 400 withhash_mismatchand expected and received values. - Send the body while retaining the status and JSON error. A measured body that crosses the allowed buffer can return 413 with
upload_declared_size_mismatch. - Handle policy refusals literally:
This API key does not permit files larger than N bytes (max_file_size_bytes),File exceeds the X GB size limit for this workspace, or the storage-quota response. - On success, read the receipt’s actual stored size and file ID, then fetch metadata. Do not reject a successful upload merely because fewer bytes arrived than declared; decide against the measured result and digest.

Expected result
An allowed body produces a file receipt whose measured size matches metadata, and an optional SHA-256 confirms the bytes. Oversized, disallowed, or corrupt input fails before it becomes a trusted result.
Troubleshooting
The server returns 411. Supply the actual decimal byte length; chunked one-shot transfer does not replace that requirement.
The file is over 50 MiB. Use /api/upload/init, 10 MiB parts, and the completion route.
Declared and stored sizes differ. Treat the receipt as authoritative, compare the digest, and investigate the producer before continuing.
Related guides
Was this helpful?
Loading helpfulness results…
Voting saves a necessary ballot cookie for up to 180 days so you can change your answer.
Questions and replies
Ask about the steps in this guide. Questions are reviewed before publication.
Keep account details, credentials and private files out of public questions. Use private support for those.
Loading questions…
Sign in to your Dosya account to ask a question.