How to version your Phoenix Framework API.
Published in
1 min readJul 11, 2017
Versioning your API is very important as it allows you to scope implementations to a specific number in case you want to change the way things are done later down the line.
This is a short article that will show you how to version the API in your Phoenix Framework application.
In your router.ex
file, scope the API to a specific version.
scope "/api/v1", MyApp.Web.Api.V1, as: :api_v1 do
pipe_through :api get "/articles/:id/content", ArticleController, :content
end
Next, place your controller in this sort of structure for convenience.
Your controller needs to be namespaced properly as well.
defmodule MyApp.Web.Api.V1.ArticleController do
use MyApp.Web, :controller def content(conn, params) do
text conn, "It's working"
end
end
That’s all there is to it! You now have a nicely versioned API.