An Elixir library for building applications with HTTP https://hex.pm/packages/liberator/
Find a file
renovate-bot 1967399e39
All checks were successful
renovate/stability-days Updates have met minimum release age requirement
ci/woodpecker/push/reuse Pipeline was successful
ci/woodpecker/push/test Pipeline was successful
chore(deps): update dependency erlang to v29.0.1
2026-06-03 12:02:45 +00:00
.woodpecker chore: use mise 2025-10-26 12:51:55 -06:00
bin chore: use mise 2025-10-26 12:51:55 -06:00
config Change license to AGPL-3.0-or-later 2024-07-07 14:11:21 -06:00
guides Change license to AGPL-3.0-or-later 2024-07-07 14:11:21 -06:00
lib Change license to AGPL-3.0-or-later 2024-07-07 14:11:21 -06:00
LICENSES Fix reuse issues 2025-05-25 19:19:00 -06:00
priv/gettext Change license to AGPL-3.0-or-later 2024-07-07 14:11:21 -06:00
test Change license to AGPL-3.0-or-later 2024-07-07 14:11:21 -06:00
.bumpversion.cfg Update licensing to use the REUSE Spec 2021-09-07 22:41:02 -06:00
.earthignore Update licensing to use the REUSE Spec 2021-09-07 22:41:02 -06:00
.editorconfig Update editorconfig 2021-09-11 17:19:58 -06:00
.formatter.exs Update licensing to use the REUSE Spec 2021-09-07 22:41:02 -06:00
.gitattributes Reduce potential changelog merge conflicts 2021-09-08 22:43:03 -06:00
.gitignore Update licensing to use the REUSE Spec 2021-09-07 22:41:02 -06:00
CHANGELOG.md Fix changelog, prepare for next release 2025-05-25 16:52:33 -06:00
code_of_conduct.md Update licensing to use the REUSE Spec 2021-09-07 22:41:02 -06:00
CONTRIBUTING.md Change license to AGPL-3.0-or-later 2024-07-07 14:11:21 -06:00
LICENSE.md Change license to AGPL-3.0-or-later 2024-07-07 14:11:21 -06:00
mise.toml chore(deps): update dependency erlang to v29.0.1 2026-06-03 12:02:45 +00:00
mix.exs chore(deps): Update gettext to 1.0 2026-05-28 11:02:28 -06:00
mix.lock chore(deps): Update gettext to 1.0 2026-05-28 11:02:28 -06:00
mix.lock.license Change license to AGPL-3.0-or-later 2024-07-07 14:11:21 -06:00
README.md fix(docs): update CI badge [ci skip] 2026-05-27 11:28:45 -06:00
renovate.json Update renovate.json 2026-05-27 16:01:25 +00:00
renovate.json.license Add license to renovate.json 2025-05-25 15:10:44 -06:00
REUSE.toml Fix reuse issues 2025-05-25 19:19:00 -06:00
SECURITY.md Change license to AGPL-3.0-or-later 2024-07-07 14:11:21 -06:00

Liberator

status-badge Hex.pm Hex.pm License

An Elixir library for building applications with HTTP.

Liberator is a port of the Liberator Clojure library that allows you to define a controller that adheres to the HTTP spec by providing just a few pieces of information. It implements a decision graph of simple boolean questions that lead your app to the correct HTTP status codes.

While Phoenix and Plug make routing easy, they don't do anything with content negotiation, or cache management, or existence checks, or anything like that, beyond calling the right controller function based on the HTTP method. There are a lot of decisions to make before returning the right HTTP status code, but Phoenix doesn't give you any additional power to do so. Liberator does.

Install

This package is available in Hex. Install it by adding liberator to your list of dependencies in mix.exs:

def deps do
  [
    {:liberator, "~> 1.4.0"}
  ]
end

Documentation can be generated with ExDoc and can be found online at https://hexdocs.pm/liberator.

Usage

For a basic GET endpoint, you can define an entire module in five lines of code. Technically you don't even need to implement these two, since sensible defaults are provided.

defmodule MyFirstResource do
  use Liberator.Resource

  def available_media_types(_), do: ["text/plain"]
  def handle_ok(_), do: "Hello world!"
end

It doesn't look like much, but behind the scenes, Liberator navigated a decision graph of content negotation, cache management, and existence checks before returning 200 OK. Liberator finds the best media type you support, and automatically encodes your return value. JSON is supported out of the box, and any additional types can be provided in a line of the config.

# in config.exs
config :liberator, media_types: %{
  "application/json" => Jason,
  "application/xml" => MyXmlCodec
}

# in your main body of code
defmodule MyJsonOrXmlResource do
  use Liberator.Resource

  def available_media_types(_), do: ["application/json", "application/xml"]
  def handle_ok(_), do: %{message: "hi!"}
end

A Liberator Resource implements the Plug spec, so you can forward requests to it in frameworks like Phoenix:

scope "/api", MyApp do
  pipe_through [:api]

  forward "/resources", MyFirstResource
end

Your results from questions are aggregated into the :assigns map on the conn, so you don't have to access data more than once.

defmodule MaybeExistingResource do
  use Liberator.Resource

  def exists?(conn) do
    case MyApp.Repo.get(MyApp.Post, conn.params["id"]) do
      nil -> false
      post -> %{post: post}
    end
  end
  def handle_ok(conn), do: conn.assigns[:post]
end

See more in the Getting Started guide, and in the documentation for Liberator.Resource.

Maintainer

This project was developed by Rosa Richter. You can get in touch with her on Matrix.

Thanks

Thanks to the maintainers of the original Clojure liberator project, Philipp Meier and Malcolm Sparks, for creating such a handy tool. Their great documentation was an immense help in porting it to Elixir. And thanks to the maintainers of Erlang's webmachine for inspiring them!

Contributing

Questions and pull requests are more than welcome. I follow Elixir's tenet of bad documentation being a bug, so if anything is unclear, please file an issue. Ideally, my answer to your question will be in an update to the docs.

Please see CONTRIBUTING.md for all the details you could ever want about helping me with this project.

Note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

License

MIT License

Copyright 2024 Rosa Richter

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.