{"id":15370115,"url":"https://github.com/danschultzer/idempotency_plug","last_synced_at":"2025-10-06T11:36:42.017Z","repository":{"id":151919731,"uuid":"624687031","full_name":"danschultzer/idempotency_plug","owner":"danschultzer","description":"Plug that makes POST and PATCH requests idempotent","archived":false,"fork":false,"pushed_at":"2024-02-25T02:58:38.000Z","size":39,"stargazers_count":21,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-05-02T00:38:12.774Z","etag":null,"topics":["elixir","idempotency","idempotent-requests","plug"],"latest_commit_sha":null,"homepage":"https://danschultzer.com/posts/idempotencyplug-idempotent-post-requests","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/danschultzer.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-04-07T02:53:29.000Z","updated_at":"2024-04-23T14:48:49.000Z","dependencies_parsed_at":null,"dependency_job_id":"feeda658-0edd-430a-9150-7ceb24ca5914","html_url":"https://github.com/danschultzer/idempotency_plug","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danschultzer%2Fidempotency_plug","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danschultzer%2Fidempotency_plug/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danschultzer%2Fidempotency_plug/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danschultzer%2Fidempotency_plug/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danschultzer","download_url":"https://codeload.github.com/danschultzer/idempotency_plug/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249085474,"owners_count":21210267,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["elixir","idempotency","idempotent-requests","plug"],"created_at":"2024-10-01T13:39:58.876Z","updated_at":"2025-10-06T11:36:36.976Z","avatar_url":"https://github.com/danschultzer.png","language":"Elixir","readme":"# IdempotencyPlug\n\n[![Github CI](https://github.com/danschultzer/idempotency_plug/workflows/CI/badge.svg)](https://github.com/danschultzer/idempotency_plug/actions?query=workflow%3ACI)\n[![hex.pm](https://img.shields.io/hexpm/v/idempotency_plug.svg)](https://hex.pm/packages/idempotency_plug)\n\n\u003c!-- MDOC !--\u003e\n\nPlug that makes POST and PATCH requests idempotent using `Idempotency-Key` HTTP header.\n\nFollows the [IETF Idempotency-Key HTTP Header Field specification draft](https://datatracker.ietf.org/doc/draft-ietf-httpapi-idempotency-key-header/).\n\n\u003c!-- MDOC !--\u003e\n\n## Installation\n\nAdd `idempotency_plug` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:idempotency_plug, \"~\u003e 0.2\"}\n  ]\nend\n```\n\n## Usage\n\nFirst, add the request tracker to your supervision tree:\n\n```elixir\ndefmodule MyApp.Application do\n  # ..\n\n  def start(_type, _args) do\n    children = [\n      {IdempotencyPlug.RequestTracker, [name: MyAppWeb.RequestTracker]}\n      # ...\n    ]\n\n    Supervisor.start_link(children, strategy: :one_for_one, name: MyApp.Supervisor)\n  end\nend\n```\n\nNow add the plug to your pipeline:\n\n```elixir\ndefmodule MyAppWeb.Router do\n  # ...\n\n  pipeline :api do\n    plug :accepts, [\"json\"]\n    plug IdempotencyPlug, tracker: MyAppWeb.RequestTracker\n  end\n\n  # ...\nend\n```\n\nAll POST and PATCH requests will now be idempotent using the `Idempotency-Key` HTTP header value, storing responses with the default ETS store.\n\n### Persisted store\n\nThe ETS store is not persisted, so it's not production-ready. Instead, let's change the store to use Ecto.\n\nFirst, run `mix idempotency_plug.ecto.gen.migration`.\n\nNow update the configuration for the request tracker:\n\n```elixir\n{IdempotencyPlug.RequestTracker, [store: {IdempotencyPlug.EctoStore, repo: MyApp.Repo}]}\n```\n\nYou can also implement your own idempotent request store by using the behaviour in `IdempotencyPlug.Store`.\n\n## Scope `Idempotency-Key` to authenticated user\n\nIf you are authenticating users then you must scope the `Idempotency-Key` to the authenticated user:\n\n```elixir\nplug IdempotencyPlug,\n  tracker: MyAppWeb.RequestTracker,\n  idempotency_key: {__MODULE__, :scope_idempotency_key}\n\ndef scope_idempotency_key(conn, key), do: {conn.assigns.current_user.id, key}\n```\n\nIf you do not do this, you may have a security vulnerability (or conflict) where any user can access another user's cached responses when requests are identical.\n\n## Customize error response\n\nBy default, errors are raised and handled by the `Plug.Exception` protocol, but you can handle the errors by setting the `:with` option:\n\n```elixir\nplug IdempotencyPlug,\n  tracker: MyAppWeb.RequestTracker,\n  with: {__MODULE__, :handle_error}\n\ndef handle_error(conn, error) do\n  conn\n  |\u003e put_status(Plug.Exception.status(error))\n  |\u003e json(%{error: error.message})\n  |\u003e halt()\nend\n```\n\n## Phoenix tests\n\nFor your controller tests, you may want to add this helper to set up the idempotency key:\n\n```elixir\ndef setup_with_idempotency_key(%{conn: conn}) do\n  conn = Plug.Conn.put_req_header(conn, \"idempotency-key\", Ecto.UUID.bingenerate())\n\n  {:ok, conn: conn}\nend\n```\n\n```elixir\nsetup :setup_with_idempotency_key\n```\n\nThis ensures that all the tests succeed by generating a UUID for all requests.\n\n\u003c!-- MDOC !--\u003e\n\n## LICENSE\n\n(The MIT License)\n\nCopyright (c) 2023 Dan Schultzer \u0026 the Contributors\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanschultzer%2Fidempotency_plug","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanschultzer%2Fidempotency_plug","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanschultzer%2Fidempotency_plug/lists"}