# Implement a Fieldnote v1 receiver

Read this as a scoped integration task. Inspect the site's framework, storage, existing authentication and deployment instructions. Add a receiver using the existing stack. Ask the owner which page should show the reading. Keep authoring access separate from public reads.

## HTTP contract

Use HTTPS. The default endpoint is /api/fieldnote. An explicitly configured path also works. Serve it directly, without redirects, CMS setup pages or login HTML. Use Cache-Control: no-store on every API response. Use JSON numbers for all numeric fields. All times are Unix milliseconds.

- GET without a valid owner bearer returns 200 and JSON null when empty, paused, or now >= measured + 1800000. Otherwise return {"bpm":72,"measured":1790000000000,"expires":1790001800000}. Never expose revision, credentials, or history to public readers.
- GET with Authorization: Bearer <owner token> returns 200 and {"revision":0,"reading":null} initially. Subsequently return the current integer revision and the same fresh reading or null. The iPhone uses this response to verify pairing.
- POST requires the owner bearer. Reject missing or invalid tokens with 401 before reading the body. Publish {"revision":1,"bpm":72,"measured":1790000000000}. Accept a positive safe-integer revision, integer BPM from 1 through 400, and a positive safe-integer measurement timestamp up to 60 seconds ahead of server time. Reject malformed input or partial nulls with 400.
- Pause is {"revision":2,"bpm":null,"measured":null}. Retain the revision and clear the reading. First pairing may send a pause before any reading.
- Atomically compare the incoming revision with the single stored row. Accept higher revisions and exact retries of the current payload. Reject older revisions or conflicting payloads at the same revision with 409. Concurrent updates must never resurrect a paused reading. A transaction or conditional upsert is appropriate.
- On success return HTTP 200 and {"accepted":true,"received":1790000001000} with the server's current timestamp. The app expects both fields. Reject unsupported methods with 405.

## Integration and acceptance

Generate a strong random bearer token on the owner's machine. Put it in the host's secret store and the app's secure pairing field. Never put it in client JavaScript, commits, logs, example URLs or query strings. Use one token and one publisher per receiver. Store only the latest reading in durable storage. Keep request body logging disabled.

For a public display, fetch the unauthenticated endpoint on load and periodically while visible (15 seconds is sufficient). Show BPM and age from measured. Hide on null, expiry or request failure. Enforce expiry on an already open page even between requests.

Use synthetic data in a disposable environment. Verify fresh install, authenticated versus public reads, invalid bearer, malformed input, publish, identical retry, conflicting retry, pause followed by delayed publish, concurrent revisions, and expiry. Open the normal public page and verify its stale and hidden states. Document setup, token rotation, verification and rollback. Follow the owner's deployment policy before publishing.

A working Cloudflare Worker and D1 schema accompany this file. Adapt to the site's own stack if appropriate.
