A code component for the platform, runs a piece of a JavaScript code inside your integration flow.
Pretty much the same way that you would use any other component in our system. It is deployed by default to production, so no need to deploy it yourself (although you could if you have extended it yourself). In our Dashboard start building your integration and include the Code component as well.

However, don’t let the simple look fool you - it has a full-fledged interface with useful features like the ones you would expect from your favourite desktop developing tool:
The technical notes page gives some technical details about Code component like changelog.
Here are the available variables and libraries that you can use within the context
of execution. The most up-to-date list can always be found in be used within the context of execution or in code.js of the component. Below is a sample for the reference.
Built-in Node.js global objects are also supported.
msg - incoming message containing the payload from the previous stepcfg - step’s configuration. At the moment contains only one property: code (the code, being executed)snapshot - step’s snapshotmessages - utility for convenient message creationemitter user to emit messages and errorswait(numberOfMilliscondsToSleep) - Utility function for sleepingrequest - Http Client (wrapped in co - this library so that it is pre-promisified)_ - Lodashstrong-soap - SOAP client for invoking web servicesTo use the code you can follow these examples:
async function run(msg) {
console.log('Incoming message is %s', JSON.stringify(msg));
const body = { result : 'Hello world!' };
// You can emit as many data messages as required
await this.emit('data', { body });
console.log('Execution finished');
}
async function run(msg, cfg, snapshot) {
return {
addition: 'You can use code',
keys: Object.keys(msg)
};
}
Please note: If you have a simple one-in-one-out function you can return a JSON object as a result of your function, it will be automatically emitted as data.
If you prefer to transform an incoming message with code then use following sample:
async function run(msg) => {
addition : "You can use code",
keys : Object.keys(msg)
}
You can code a small REST API call out of the Code component, see following example:
const axios = require('axios');
async function run(msg) {
const { data: res } = await axios.get('https://api.thatapp.io/v2/users/me', {
auth: {
username: process.env.ELASTICIO_API_USERNAME,
password: process.env.ELASTICIO_API_KEY
}
});
return {
fullName: `${res.data.attributes.first_name} ${res.data.attributes.last_name}`,
email: res.data.attributes.email,
userID: res.data.id
};
}
The Code component exposes the strong-soap client as soap. You can call SOAP operations using async/await. Create the client with a small promise wrapper, then invoke methods (they return promises).
Basic SOAP call (WSDL URL and operation args from incoming message):
function createSoapClient(wsdlUrl, options = {}) {
return new Promise((resolve, reject) => {
soap.createClient(wsdlUrl, options, (err, client) => {
if (err) reject(err);
else resolve(client);
});
});
}
async function run(msg, cfg, snapshot) {
const { wsdlUrl, operation, args } = msg.body;
const client = await createSoapClient(wsdlUrl);
const { result } = await client[operation](args || {});
await this.emit('data', { body: result });
}
Calling a specific service and port:
If the WSDL defines multiple services or ports, use the ServiceName.PortName.MethodName form (use the same createSoapClient helper as in the examples above):
async function run(msg, cfg, snapshot) {
const client = await createSoapClient(msg.body.wsdlUrl);
const { result } = await client.MyService.MyPort.MyFunction({ name: msg.body.inputName });
await this.emit('data', { body: result });
}