Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List Phoenix API

I would like to have an endpoint /api/api_endpoints/ that list all the public api endpoints. Similar to mix phx.routes, but I only want to list things like

...
GET /api/users
PUT /api/users/name
...

Things like that. I looked in the documentation but did not see anything for listing routes.

like image 322
bexley Avatar asked May 16 '26 17:05

bexley


1 Answers

There is nothing built in, but for something as simple as you want you could always roll your own. All the routes are stored inside your router as a list of Phoenix.Router.Route stucts. For example in a project called Residential:

iex(1)> ResidentialWeb.Router.__routes__
[%Phoenix.Router.Route{assigns: %{}, helper: "page", host: nil, kind: :match,
opts: :index, path: "/", pipe_through: [:browser],
plug: ResidentialWeb.PageController, private: %{}, verb: :get},
%Phoenix.Router.Route{assigns: %{}, helper: "user", host: nil, kind: :match,
opts: :index, path: "/users", pipe_through: [:browser],
plug: ResidentialWeb.UserController, private: %{}, verb: :get},
%Phoenix.Router.Route{assigns: %{}, helper: "user", host: nil, kind: :match,
opts: :edit, path: "/users/:id/edit", pipe_through: [:browser],
plug: ResidentialWeb.UserController, private: %{}, verb: :get},

You can map over them and extract the verb and path and show them as you please.

This is pretty much how mix phx.routes works, if you take a look at the implementation

like image 97
Mario Avatar answered May 19 '26 04:05

Mario



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!