# How to Create a Cowboy Middleware Add an entry to the `middlewares`. Usually you want to keep the standard Cowboy handlers afterwards, but you can replace them if you want. ```erlang {ok, Pid} = cowboy:start_clear(ListenerName, [{port, Port}], #{ env => #{dispatch => Dispatch}, middlewares => [my_middleware, cowboy_router, cowboy_handler] } ), ``` A middleware implements the behavior `cowboy_middleware`. ## Caveats * You cannot change a response after it has been sent. For example, if you add a middleware after the generic `cowboy_handler` middleware. If a previous middleware has called the `cowboy_req:reply/4` function the response has already been sent on the wire. For that purpose you need to use a stream handler instead.