# `AshJsonApi.Router`
[🔗](https://github.com/ash-project/ash_json_api/blob/v1.6.4/lib/ash_json_api/router.ex#L5)

Use this module to create a router for your AshJsonApi.

To use this, create a module and do the following:

```elixir
defmodule YourRouter do
  use AshJsonApi.Router,
    domains: [YourDomain, YourOtherDomain],
    # these next two are optional, only add them if you want those endpoints
    open_api: "/open_api",
    json_schema: "/json_schema",
    # tell us where it is mounted in your router
    prefix: "/api/json"
end
```

Then in your Phoenix router or plug pipeline, forward to this plug.
In phoenix, that looks like this:

```elixir
    forward "/api", YourRouter
```

## Customizing request handling

You can provide the `before_dispatch` option to customize request handling.
This can also be used to do things like set monitoring/observability information,
like which domain/resource/route is handling the request.

For example:

```elixir
use AshJsonApi.Router,
  ...,
  before_dispatch: {__MODULE__, :before_dispatch, []}

def before_dispatch(conn, route_info) do
 ...
end
```

`route_info` will be one of the following:

- `:open_api` - The open api is being requested
- `:json_schema` - The json schema is being requested
- `:not_found` - No matching route was found
- A map containing the keys: `domain`, `resource`, `route`, `params`

---

*Consult [api-reference.md](api-reference.md) for complete listing*
