Skip to main content

Understanding Source Code in Processes

In the YepCode editor, users can craft the code to implement their processes.

Depending on the language selected, the script is executed in a specific engine:

  • JavaScript We use the NodeJS v20 engine. This allows you to utilize nearly all functions supported by NodeJS. The code is wrapped in an async function, enabling the use of await throughout the function.
  • Python We use the Python v3.12 engine. This supports the use of almost all functions provided by Python.

The most compelling components to leverage in this source code are the integrations. Each integration is backed by a NodeJS or Python package, enabling the use of external services within YepCode processes. Initialization configurations are utilized to avoid storing any credentials in your source code.

For those who prefer not to reinvent the wheel, explore the included libraries to take advantage of them in both languages.

To combat the challenge of spaghetti code, we've included a module to create and use your own Modules.

The source code editor supports key shortcuts, code formatting, and autocomplete features. The autocomplete is configured to take advantage of the included integrations. If you start typing YepCode..., you'll see some useful snippets.

Snippets are available

The snippets included in the autocomplete feature are the same ones documented in the integrations docs page.

The process source code is executed sequentially.

tip

In JavaScript versions, each process execution will wait while any active promise is still running.

As with any other script, you can define functions and structure your code with good practices. Please follow the principles of clean code.

Internal Helpers

YepCode allows you to use information from the current execution in your process.

Access to Execution and Process Info

The basic information available for all executions includes the id of the current execution, the comment (if you wrote one), and the id and name of the process being executed. The code to obtain them would be:

const { id, comment } = yepcode.execution;

const { id: processId, name: processName } = yepcode.execution.process;

Access to Scheduled Process Info

When you schedule a process, you also have access to the id of the schedule and its comment (if it exists). The code to obtain them would be:

const { id: scheduleId, comment: scheduleComment } = yepcode.execution.schedule;

An interestion use case for this information is to build the exact execution link to send it by email:

const executionLink = `https://cloud.yepcode.io/{your-team-slug}/executions/${yepcode.execution.id}`;

Access to Team Timezone

You can access the current timezone set in your team. The code to do it would be:

note

The team timezone will also be the timezone used for any date manipulation in the execution.

const timezone = yepcode.team.timezone;

Return Value

YepCode allows your processes to return a value. This is very insteresting, especially when starting executions using webhooks or forms. You could manage this result value in your client. The syntax to return one object would be:

return { message: "Hello from YepCode!" };

You can view it on the execution detail page and also utilize it with the sync webhooks feature.

Return Custom Status Codes

Custom status code and a custom message are also supported. This is useful in many cases, but the most common one is probably error handling. For example, if you want to return a 404 error, but don't want the execution to end in an error status.

To do that, you just need to return an object with the following structure:

try {
// simulate an error
throw new Error("Oops! Something went wrong.");
} catch (e) {
return {
status: 418,
body: {
error: {
message: e.message,
stack: e.stack,
},
},
headers: {
"X-Custom-Header": "I am a custom header",
},
};
}

The magic lies in status, body and headers properties.

tip

You can return a JSON or plain string as body. The Content-Type header will be set automatically.

Transient Return Values

In some cases, you may want to not store the return value of your process, for example, when sensitive data is returned.

For this cases, YepCode allows you to set a result as transient, so this result is not stored in the database.

For these cases, the stored result will be the [transient] replacement.

tip

If you call your process by a webhook in sync mode, you will be able to get the result in that call.

To make a result transient, just follow this structure:

return {
isTransient: true,
body: { foo: "bar" },
};

Of course, you can combine this with previously shown properties. For example:

return {
isTransient: true,
status: 201,
body: { foo: "bar" },
headers: {
"X-Custom-Header": "I am a custom header",
},
};

Logging

YepCode allows to generate log entries that can be seen then in execution detail view.

In JavaScript you may use the console logger methods:

console.log(`This is an INFO message`);
console.debug(`This is a DEBUG message`);
console.info(`This is an INFO message`);
console.warn(`This is a WARNING message`);
console.error(`This is an ERROR message`);

This is how you'll see the logs in the execution detail: