Skip to main content

Worker code

This page explains how you write the code of a worker action. The structure is the same for every language: you write a single function which receives six arguments and returns a response. The examples on this page give an overview, the PHP API page contains a complete reference with examples.

Structure​

The following shows a minimal action for every language:

PHP
<?php

use Fusio\Worker;
use Fusio\Engine;
use Psr\Log\LoggerInterface;

return function(Worker\ExecuteRequest $request, Worker\ExecuteContext $context, Engine\ConnectorInterface $connector, Engine\Response\FactoryInterface $response, Engine\DispatcherInterface $dispatcher, LoggerInterface $logger) {
return $response->build(200, [], [
'hello' => 'world',
]);
};
JavaScript
module.exports = async function(request, context, connector, response, dispatcher, logger) {
return response.build(200, {}, {
hello: 'world'
});
};
Python
def handle(request, context, connector, response, dispatcher, logger):
return response.build(200, None, {
'hello': 'world'
})
Java/Groovy
def handle(ExecuteRequest request, ExecuteContext context, Connector connector, ResponseBuilder response, Dispatcher dispatcher, Logger logger) {
return response.build(200, [:], [
hello: 'world'
]);
}

Arguments​

Every worker function receives the same six arguments, independent of the language:

ArgumentDescription
requestThe incoming request, contains the path and query parameters (arguments) and the request body (payload)
contextInformation about the invocation, i.e. the authenticated user and app
connectorReturns the client of a connection which is configured at Fusio
responseBuilds the response of your action, i.e. with a status code and body
dispatcherDispatches events, i.e. to notify webhooks
loggerWrites log messages, which help you to debug your action

The method names follow the conventions of each language, i.e. connector.getConnection('App') in JavaScript and connector.get_connection('App') in Python.

API reference​