Phoenix Router Pipelines: Separate Browser and JSON API Requests
When a Phoenix app serves both browser pages and a JSON API, separate request paths and Plug pipelines in the router, keep controllers focused on HTTP input/output, and put data rules in Contexts. Browser requests may need sessions and CSRF protection; API requests often need JSON handling instead. Separate routes do not automatically add authentication or authorization.
When a Phoenix app serves both browser pages and a JSON API, separate request paths and Plug pipelines in the router, keep controllers focused on HTTP input/output, and put data rules in Contexts. Browser requests may need sessions and CSRF protection; API requests often need JSON handling instead. Separate routes do not automatically add authentication or authorization.
This guide follows Phoenix 1.8.16 and covers router scopes and pipelines, controller actions, Context boundaries, and ConnCase HTTP tests. Actual plugs and authentication vary by application.
Define pipelines by request type
The Phoenix router matches HTTP method and path to a controller action, then applies a Plug pipeline to a route group. A generated browser pipeline commonly includes session and CSRF plugs. An API pipeline can accept JSON and use a different response setup.
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :protect_from_forgery
end
pipeline :api do
plug :accepts, ["json"]
endThis is a structural example; generated apps may include additional plugs. Inspect the existing pipeline before changing it.
Connect scopes and routes
A scope groups routes under a URI prefix, module alias, and pipeline. For example, place browser pages at the root and API routes under /api. Check each scope's pipe_through setup. If a route returns 404 or reaches the wrong action, run mix phx.routes and compare method, URI, pipeline, and controller action.
Keep controllers at the HTTP boundary
A controller action receives conn and params and shapes a response. Put reusable data access and domain validation in a Context rather than duplicating it across controllers or LiveViews. The controller can call a Context and select HTML/JSON response and status.
def index(conn, _params) do
posts = Blog.list_posts()
json(conn, %{data: posts})
endThis is illustrative. A real app may use a view or API resource for JSON serialization and should translate Context or changeset errors into appropriate HTTP responses. Do not persist raw client input without validation.
Verify with ConnCase
Phoenix uses ExUnit and ConnCase for HTTP request tests. ConnCase builds a test conn and provides verified routes and Phoenix.ConnTest helpers; it can configure SQL Sandbox when tests use a database. Sending a request through the route exercises method, pipeline, action, and serialization together.
test "GET /api/posts returns JSON", %{conn: conn} do
conn = get(conn, ~p"/api/posts")
assert json_response(conn, 200)["data"] == []
endAdapt fixtures and response expectations to the app. If an API redirects to HTML or hits session/CSRF plugs unexpectedly, inspect its pipeline and Accept/content type. Test success, validation failure, and authorization failure as distinct contracts. The snippets were not executed.
Troubleshoot in order: inspect mix phx.routes for method, URI, and action; ensure the scope uses the intended pipeline; compare accepts plugs and controller/view serialization when JSON differs; move scattered data rules into Contexts/changesets; exercise the actual route through ConnCase and assert status, headers, and body.
Router pipelines apply shared Plug processing, scopes define where it applies, and Controllers and Contexts separate HTTP presentation from domain work. Verify the route list together with ConnCase request tests.
References: official Phoenix 1.8.16 guides on overview, routing, controllers, contexts, and testing.
References
[Phoenix 1.8.16: Overview](https://hexdocs.pm/phoenix/overview.html)
[Phoenix 1.8.16: Routing](https://hexdocs.pm/phoenix/routing.html)
[Phoenix 1.8.16: Controllers](https://hexdocs.pm/phoenix/controllers.html)
[Phoenix 1.8.16: Contexts](https://hexdocs.pm/phoenix/contexts.html)
[Phoenix 1.8.16: Testing](https://hexdocs.pm/phoenix/testing.html)