Can a Event Service be coupled to a route I have created a custom service and have hooked into the "event_subscriber"  via tags in my custom service.yml 

  demo_api.exception_handler_subscriber:
      class: Drupal\demo_api\Response\Handler
      tags:
          - { name: 'event_subscriber' }


With in this custom service I'm listening into KernelEvents::RESPONSE

public static function getSubscribedEvents()
{
    // For this example I am using KernelEvents constants (see below a full list).
    $events[KernelEvents::RESPONSE][] = ['onResponse'];
    return $events;
}


The problem is I have some API request and I'm modifying the response for these request and returning a modified json format.

public function onResponse(FilterResponseEvent $event)
{
    $response = $event-&gt;getResponse();
    $apiFormatter = new ApiFormatter();
    $response-&gt;setContent(
        json_encode(
            $apiFormatter
            -&gt;wrapResult(
                json_decode(
                    $response-&gt;getContent()
                )
            )
        )
    );
}


This is causing any web pages to respond as json.

I would like to limit this service to these API calls only and not be called on each request.

Any advice would be grateful.
If you want to restrict the event subscriber to a route then check for the route name:

public function onResponse(FilterResponseEvent $event) {

  $route_name = $event-&gt;getRequest()-&gt;attributes-&gt;get('_route');
  if ($route_name !== 'my.route.name') {
    return;
  }

  ...

}


Although in Drupal it might be best practice to get the route name from the current_route_match service:

$route_name = $this-&gt;routeMatch-&gt;getRouteName();


which you then need to inject in the event subscriber.