Skip to content
A simple plug for incrementally transforming an API into Phoenix. Check out the blog post:
Branch: master
Clone or download
Latest commit cc78b21 Jun 5, 2017
Type Name Latest commit message Commit time
Failed to load latest commit information.
config Init Aug 8, 2016
lib Update deps and add moduledoc (#18) May 11, 2017
test Convert test to use Plug.Router in the Terraformer (#10) Aug 11, 2016
.gitignore Init Aug 8, 2016
.tool-versions Update deps and add moduledoc (#18) May 11, 2017 Add license (#5) Aug 8, 2016 update version (#23) Jun 5, 2017
circle.yml Update deps and add moduledoc (#18) May 11, 2017
mix.exs Released v1.0.1 Jun 2, 2017
mix.lock Update deps and add moduledoc (#18) May 11, 2017

terraform Hex CircleCI

Terraform is a simple Plug designed to work with Phoenix. Terraform allows you to incrementally transform an older API into one powered by Phoenix - one endpoint at a time.

View the demo Phoenix app.


Add terraform to your list of dependencies in mix.exs:

def deps do
  [{:terraform, "~> 1.0.1"}]


First, add it to web/router.ex:

defmodule MyApp.Router do
  use Terraform, terraformer: MyApp.Terraformers.Foo

  # ...

Then, define a new Terraformer, which uses Plug.Router. Any request that goes to a route that isn't defined on your Phoenix app will hit this plug, and you can then handle it using a familiar DSL:

defmodule MyApp.Terraformers.Foo do
  alias MyApp.Clients.Foo # example client made with HTTPoison
  use Plug.Router

  plug :match
  plug :dispatch

  # match specific path
  get "/v1/hello-world", do: send_resp(conn, 200, "Hello world")

  # match all `get`s
  get _ do
    %{method: "GET", request_path: request_path, params: params, req_headers: req_headers} = conn
    res = Foo.get!(request_path, req_headers, [params: Map.to_list(params)])
    send_response({:ok, conn, res})

  def send_response({:ok, conn, %{headers: headers, status_code: status_code, body: body}}) do
    conn = %{conn | resp_headers: headers}
    send_resp(conn, status_code, body)
You can’t perform that action at this time.