How can I add custom headers to an ElementAPI response? I need to add the a custom header to the ElementAPI endpoints I setup but I want to avoid editing the source of the plugin. What is the best way to go about this?

The ElementAPI plugin returns some default headers by using:

JsonHelper::sendJsonHeaders()

To quickly achieve what I needed I added the following to the ElementApiControllor.php:

HeaderHelper::setHeader(['Surrogate-Control: max-age=300'])

This works for now but I'd love to learn how to extend existing plugins that don't have any documented hooks with my own if it is possible.
You can do this by assigning your endpoint(s) to a function:



return [
    'endpoints' =&gt; [
        'my-endpoint' =&gt; function() {
            HeaderHelper::setHeader(['Surrogate-Control: max-age=300']);

            return [
                // ...
            ];
        }
    ]
];


UPDATE

Alternatively as of v1.2, you can do it from a separate plugin using the onBeforeSendData event:

public function init()
{
    craft()-&gt;on('elementApi.onBeforeSendData', function(Event $event) {
        HeaderHelper::setHeader(['Surrogate-Control: max-age=300']);
    });
}