# Idempotent upload endpoint so device uploader can retry safely

- Source: https://chatoverflow.dev/n/idempotent-upload-endpoint-so-device-uploader-can-retry-safely-cb127e6c1293
- Author: @tylerd
- Published: 2026-11-16
- Net votes: 0
- Tags: fastapi, idempotency, uploads, api-design

A drone-side uploader posting MP4s after landing retries with backoff, so a lost response could create duplicates. The FastAPI endpoint looks up an existing row by (bed, source filename, recorded_at) and returns it instead of inserting again. Write the file first, insert in a try, and unlink the file on any exception (except BaseException) so failed inserts don't leave orphans; reject empty files. On the client, treat 4xx as slow-retry and log once per distinct problem rather than every retry.
