Skip to main content

Code Component

Author and run your own code

Component key: code · Changelog ↓

Description

The code component allows you to write your own short snippets of JavaScript code, and is handy for writing quick functions or data transformations that are specific to your product or industry. Please see the full article on the code component for information on when a code component is appropriate, and common use cases for a code component.

Code component steps should be succinct and integration-specific. If the code you write could be reused in other integrations, if it needs to handle credentials, or if the code is complex enough that it would benefit from unit tests, etc., you should write a custom component instead.

For some examples of code component usage, check out these quickstart guides:

Connections

API Key

key: apiKey
InputNotesExample
API Key

API Key

API Key Secret

key: apiKeySecret
InputNotesExample
API Key

API Key

API Secret

API Secret

OAuth 2.0 Authorization Code

key: authorizationCode
InputNotesExample
Authorize URL

The OAuth 2.0 Authorization URL for the API

Client ID

Client Identifier of your app for the API

Client Secret

Client Secret of your app for the API

Headers

Additional header to supply to authorization requests

Scopes

Space separated OAuth 2.0 permission scopes for the API

Token URL

The OAuth 2.0 Token URL for the API

Basic Username/Password

key: basic
InputNotesExample
Password

Password

Username

Username

OAuth 2.0 Client Credentials

key: clientCredentials
InputNotesExample
Client ID

Client Identifier of your app for the API

Client Secret

Client Secret of your app for the API

Headers

Additional header to supply to token requests

Scopes

Space separated OAuth 2.0 permission scopes for the API

Token URL

The OAuth 2.0 Token URL for the API

Private Key

key: privateKey
InputNotesExample
Private Key

Private Key

Username

Username

Triggers

Code Block Trigger

Author and run your own code as a trigger | key: runCodeTrigger

InputNotesExample
Code

The code to be executed

/*
  Access config variables by name through the configVars object. e.g.
    const apiEndpoint = `${configVars["App Base URL"]}/api`;

  Access the trigger payload using the payload argument. This includes
  headers, the body of the request, and more information about the request.

  You can return string, number or complex object data. e.g.
    return { payload: { foo: "Hello", bar: 123.45, baz: true } };

  You are also able to return an optional response to the webhook caller.
  For example, you can respond with a CSV response like the following:
    return {
      payload: { foo: "Hello", bar: 123.45, baz: true },
      response: { statusCode: 200, contentType: "text/csv", body: "hello,world" },
    }
*/

module.exports = async ({ logger, configVars }, payload) => {
  const response = {
    statusCode: 200,
    contentType: "text/plain",
    body: "hello",
  };
  return { payload, response };
};
Example Payload for Code Block Trigger
Loading…

Data Sources

Code Block JSON Form

Author and run your own code as a JSON Form data source | key: runCodeJsonForm | type: jsonForm

InputNotesExample
Code

The code to be executed

module.exports = async (context, { connection, contextValue }) => {
  const schema = {
    type: "object",
    properties: {
      companyName: {
        type: "string",
      },
      companyDescription: {
        type: "string",
        description: "You can enter multiple lines here",
      },
      numEmployees: {
        type: "integer",
        description: "Include employees in all offices",
      },
      continent: {
        type: "string",
        enum: [
          "North America",
          "South America",
          "Europe",
          "Asia",
          "Africa",
          "Australia",
        ],
      },
      biDirectionalSync: {
        type: "boolean",
      },
    },
    required: ["companyName"],
  };

  const uiSchema = {
    type: "VerticalLayout",
    elements: [
      {
        type: "Control",
        scope: "#/properties/companyName",
      },
      {
        type: "Control",
        scope: "#/properties/companyDescription",
        options: {
          multi: true,
        },
      },
      {
        type: "Control",
        label: "Employee Count",
        scope: "#/properties/numEmployees",
      },
      {
        type: "Control",
        scope: "#/properties/continent",
      },
      {
        type: "Control",
        label: "Sync Data Bi-Directionally?",
        scope: "#/properties/biDirectionalSync",
      },
    ],
  };

  const data = {
    companyName: "Example Company",
  };

  return Promise.resolve({
    schema,
    uiSchema,
    data,
  });
};
Connection
Context Value

Code Block Object Selection

Author and run your own code as a object selection data source | key: runCodeObjectSelection | type: objectSelection

InputNotesExample
Code

The code to be executed

module.exports = async (context, { connection, contextValue }) => {
  const options = [
    { object: { label: "Hello", key: "hello" } },
    { object: { label: "World", key: "world" } },
  ];
  return Promise.resolve({
    result: options,
  });
};
Connection
Context Value

Code Block Picklist

Author and run your own code as a picklist data source | key: runCodePicklist | type: picklist

InputNotesExample
Code

The code to be executed

module.exports = async (context, { connection, contextValue }) => {
  const options = [
    { label: "Hello", key: "hello" },
    { label: "World", key: "world" },
  ];
  return Promise.resolve({
    result: options,
  });
};
Connection
Context Value

Code Block String

Author and run your own code as a string data source | key: runCodeString | type: string

InputNotesExample
Code

The code to be executed


module.exports = async (context, { connection, contextValue }) => {
  // Random number between 1 and 100
  const randomNumber = Math.floor(Math.random() * 100 + 1)

  // Random To-Do item
  const response = await fetch(`https://jsonplaceholder.typicode.com/todos/${randomNumber}`);

  const { title } = await response.json();

  return { result: title };
};
Connection
Context Value

Actions

Code Block

Author and run your own code | key: runCode

InputNotesExample
Code

The code to be executed

/*
  Access config variables by name through the configVars object. e.g.
    const apiEndpoint = `${configVars["App Base URL"]}/api`;

  Access previous steps' results through the stepResults object. Trigger
  and step names are camelCased. If the step "Get Data from API" returned
  {"foo": "bar", "baz": 123}, you could destructure that data with:
    const { foo, baz } = stepResults.getDataFromApi.results;

  You can return string, number or complex object data. e.g.
    return { data: { foo: "Hello", bar: 123.45, baz: true } };
*/

module.exports = async ({ logger, configVars }, stepResults) => {
  return { data: null };
};
Example Payload for Code Block
Loading…