{
  "id": 22381459,
  "title": "State Machines for Email Verification APIs",
  "url": "https://urgent.news/2026/11/28/state-machines-for-email-verification-apis",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-11-28T14:23:52.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/kevindev27/state-machines-for-email-verification-apis-4eg6"
  },
  "original_language": null,
  "account": "Email verification often appears as a small feature, but it becomes complex when retries, expired tokens, duplicate requests, and delayed workers are involved. A REST API cannot fully explain these cases with a simple verified boolean. Persisting state machine rules provides explicit behavior, enforces constraints on PostgreSQL, and maintains consistent authentication.\n\nA verified flag alone cannot determine if a token was never issued, a second token was requested, the original token expired, a token was consumed, or delivery is temporarily failing. Each condition requires different responses and actions. Testing these edge cases helps prevent bugs later on.\n\nA potential state machine consists of the following states: pending, sent, verified, expired, and blocked. The transition rules ensure that a consumed token never becomes valid again, and an expired token can only transition to the sent state after a new token is created. A blocked account should not revert to pending if a delayed worker arrives late.\n\nPersisting legal transitions in PostgreSQL involves creating a separate table for the current status and token event. The schema stores the status as an enumerated type, and the version column serves as an optimistic concurrency check. Updating the status is an atomic operation that depends on matching the current status, expiration time, token hash, and version. This ensures that the update cannot be applied twice by concurrent requests.\n\nTo make the REST API idempotent, a new endpoint should accept an Idempotency-Key header. Upon a successful transition, the response should be recorded in the same transaction or a durable store. Repeating a request with the same key returns the recorded result, while a different body triggers a conflict. A clean contract helps manage retries rather than relying on unreliable network assumptions.\n\nThe email adapter receives an immutable event after the database commit. For asynchronous delivery, an outbox row prevents the failure where the API reports success but the queue message was never created. A delayed worker must re-check the row version before sending to avoid sending a token that the user already replaced.\n\nWhen testing, cover scenarios like two confirmations with the same token, two confirmations with the same idempotency key, a token at the exact expiry boundary, a delayed send worker after a replacement token, a blocked account, and a malformed token. A simple fixture email address can help test delivery and cleanup in integration tests but should not be used to prove the production identity or authorization system's trustworthiness.",
  "summary": "State Machines for Email Verification APIs Email verification looks like a small feature until retries, expired tokens, duplicate requests, and delayed workers enter the picture. A single verified boolean cannot explain those cases. A persisted state machine gives a REST API explicit rules, makes PostgreSQL enforce part of the contract, and keeps authentication behavior predictable. This design…",
  "summary_source": "publisher",
  "key_points": [],
  "editors_take": null,
  "opinion": false,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "machine_translated": false,
  "ai_generated": true,
  "disclaimer": "Accounts, briefs, key points, the editor’s take and translations are written by software from other outlets’ reporting and may contain errors. A summary whose summary_source is \"publisher\" is the outlet’s own feed text. Always check the linked original."
}