File.create:after hook – catch callback from external API

Hi! I have a theoretical question:

Is it possible to somehow catch a callback from an external API after executing a file.create:after hook within the panel without the need of a whole field-extending plugin?

In detail:
After a file(s) has/have been created its URL will be sent to a some other website which fetches and copies the file it automatically to their server. I can probably send this URL to the the other website inside the file.create:after hook easily (I guess) via regular PHP.

However I’d like to append an indicator somewhere in the panel that the file(s) has/have successfully been fetched by the other page. And therefore need to catch their API callback.

So, the best case would be:

–> file.create:after hook fired after file(s) has/have been created in a files field/section 
    –> append spinning indicator somewhere in panel 
        –> other website sends API callback that it finished fetching/copying the file(s) 
            –> catch that callback in the panel
                –> replace spinner with checkmark or something in panel

Thanks for any input!

The hook runs server-side, so you cannot make any changes to the frontend from such a hook. The only thing you can do from a hook, is throw an exception, which could also be a success message.

1 Like

Ah ok thx!
If you may:
• would the success message be formatted like the native modal overlay or something else?
• is it possible to catch the API response from the other server within the hook or do you maybe know of another way to catch the response?


If you send a request to a server, you always get a response.

hm yeah, but I don’t really get how to process the response :

return [
    'hooks' => [
        'file.create:after' => function ($file) {
             // the servers credentials
            $someAPIconnection('my_key', 'my_secret');
            // url to be fetched
            $params['url'] = 'theuploadedfiletothepanel.mp4';
            // fetched successfully (or maybe not)
            $response = json_encode($upload/$responseInfo);

            // display $response as panel modal overlay but don't really know how since the lack of skills      
            something $response;

EDIT: Also I guess, the file.create:after hook is not really waiting for the response or? If the response of a successful fetch is 30 seconds after the file creation, the hook won’t process the response any more, no?

What is this supposed to do? Does that call a kirby route?

Sorry, deleted it. It’s more like
$api= new Jwplayer\JwplatformAPI('INSERT API KEY', 'INSERT API SECRET');
which is to communicate with their API

It’s actually this library which is supposed to give easy access to their API:

Ah, ok, well, you definitely may not json_encode this api call.

Then you have to check what you get back, try

$response = $botr_api->call('/videos/create', $params);
throw new Exception(dump($response));

Or use a debugger to explore the response.

1 Like

Ah ok thx, I’ll see if this is within the range of my skills otherwise I’ll post it in the Jobs section I guess.

Before I try though two questions:
• would the file.create:after hook also throw the exception, say 30 seconds, after creating the file. Because the fetching by the other server might take some time and therefore also the other server’s response.
• is the exception displayed as regular modal overlay like all the other modal overlays?


Yes, you can try that yourself by letting the hook sleep for a while.

The downside is that the exception modal always says error…

Try this little hook as example:

'hooks' => [
	'file.create:after' => function ($file) {
		$response = Remote::get('');
		throw new Exception(json_encode($response->json(true)['entries']), 200);
1 Like

Thanks for the tip!

But then it kind of makes no sense to use it, no? If it never says “success”. Or am I misunderstanding something?

Maybe not, but throwing an Exception is currently the only way to return something from a hook.

Otherwise, you need to program something custom.

But why not do it the other way round, throw the exception when something goes wrong, when no exception is thrown, everything is fine.

1 Like

Yeah that would be a solution as well however, there wouldn’t be any indication for the user that the fetching by the other server succeeded.

I guess the sleep() is set in hardcoded seconds, so the time when the Exception would thrown has to be estimated like a fake API callback?