Founding churches get 50% off for 12 monthsFounding churches: 50% offApply as a founding churchApply

Idempotency

Networks fail halfway. An idempotency key lets you send the same request again and get the first answer back instead of a second teaching, note or question.

Which routes need a key#

Routes that start work require an Idempotency-Key header. Without it the answer is 422 idempotency_key_required.

RouteWhy
POST /teachingsStarts transcription.
POST /teachings/{id}/notesStarts a draft note, which uses AI budget.
POST /askStarts an answer, which uses AI budget.
POST /uploads and POST /uploads/{id}/completeCreates an upload, and creates the teaching.
POST /webhooksCreates an endpoint and its secret.
POST /webhooks/{id}/rotate-secret, /test and /deliveries/{id}/replayCreate a new secret or a new delivery.

Routes that only read, change or delete (GET, PATCH, DELETE) do not take one, because repeating them is already safe.

How it works#

  • Choose a unique value for each piece of work, such as a UUID.
  • Send the same value on the original request and on every retry of it.
  • For 24 hours, a repeat of the same key, on the same route, with the same body returns the response stored the first time, with the same status. The work is not done again.
  • The same key with a different body answers 422, so you notice a bug where two different requests share a key.
  • The key is tied to the route (including ids in the path). The same value on a different route is a different request.
KEY=$(uuidgen)

curl -X POST "$LECTERN_BASE_URL/v1/api/o/$LECTERN_SLUG/teachings" \
  -H "Authorization: Bearer $LECTERN_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: $KEY" \
  -d '{"url": "https://www.youtube.com/watch?v=jNQXAC9IVRw"}'

curl -X POST "$LECTERN_BASE_URL/v1/api/o/$LECTERN_SLUG/teachings" \
  -H "Authorization: Bearer $LECTERN_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: $KEY" \
  -d '{"url": "https://www.youtube.com/watch?v=jNQXAC9IVRw"}'

What is remembered#

Lectern stores the response to a request once it has produced one, successful or not. That is deliberate, and it has one consequence you should plan for:

A remembered failure stays remembered

If a request fails with a 4xx error, repeating it with the same key returns the same error for the next 24 hours, even after you fix the cause. After you fix the problem, send the request again with a new key.

  • Server errors (5xx) and 401 are not remembered. They are safe to retry with the same key.
  • A replay never repeats a secret. When you retry creating or rotating a webhook with the same key, the response has secret_shown_once: true in place of secret. If you did not store the secret from the first response, rotate it.
  • A replayed response carries the same body. It does not repeat the Location header, so keep the id from the body.
  • The body is compared byte for byte after hashing, so reformatting JSON between retries counts as a different body.

Choosing keys#

  • Generate the key before the first attempt and store it with the job you are retrying. A key made inside the retry loop defeats the purpose.
  • For work you want at most once per real-world event, derive the key from that event. “complete-” plus the upload id makes finishing an upload safe to repeat.
  • Do not reuse a key for a different request, even after 24 hours, unless you mean the same request.
Webhooks are at-least-once

Idempotency keys protect your calls to Lectern. The matching protection for events Lectern sends you is to de-duplicate on the event id. See Webhooks.

Retrying a 429

A 429 from the request rate limit happens before the request is handled and is not remembered. A 429 for a spend cap is remembered like any other 4xx.

Next steps