JustPaid Workflows
Blocks

Code Block

Run custom JavaScript in a sandboxed environment for complex data transformations

The Code block lets you run custom JavaScript within your workflow. Use it for complex data transformations, calculations, API calls, or any logic that cannot be expressed with the built-in blocks.

Configuration

Code

Write JavaScript that processes input data and returns a result. The code runs in a sandboxed Node.js environment with access to the workflow context.

// Access input data from upstream blocks
const invoice = input.data;

// Transform data
const lineItems = invoice.items.map(item => ({
  description: item.name,
  amount: Math.round(item.price * 100), // Convert to cents
  quantity: item.quantity,
  total: Math.round(item.price * item.quantity * 100),
}));

// Calculate totals
const subtotal = lineItems.reduce((sum, item) => sum + item.total, 0);
const tax = Math.round(subtotal * 0.0825); // 8.25% tax
const total = subtotal + tax;

// Return structured output
return {
  lineItems,
  subtotal,
  tax,
  total,
  currency: 'usd',
};

Input Data

The code block receives data from upstream blocks through the input object. This contains the outputs of connected blocks.

Timeout

Maximum execution time for the code block. Default: 30 seconds. Set a lower value for simple calculations, higher for external API calls.

Outputs

OutputTypeDescription
resultJSONThe value returned by your code

Common Use Cases

Currency Conversion

const amount = input.data.amount;
const rate = input.data.exchangeRate;
return {
  originalAmount: amount,
  convertedAmount: Math.round(amount * rate),
  rate: rate,
};

Data Aggregation

const invoices = input.data.invoices;
return {
  totalAmount: invoices.reduce((sum, inv) => sum + inv.amount, 0),
  count: invoices.length,
  averageAmount: invoices.reduce((sum, inv) => sum + inv.amount, 0) / invoices.length,
  byStatus: invoices.reduce((acc, inv) => {
    acc[inv.status] = (acc[inv.status] || 0) + 1;
    return acc;
  }, {}),
};

Format Mapping

// Map Stripe invoice to QuickBooks format
const stripe = input.data;
return {
  CustomerRef: { value: stripe.customer },
  Line: stripe.lines.data.map(line => ({
    Amount: line.amount / 100,
    Description: line.description,
    DetailType: "SalesItemLineDetail",
  })),
  DueDate: new Date(stripe.due_date * 1000).toISOString().split('T')[0],
};

Best Practices

  • Keep code focused. Each Code block should do one thing well. Use multiple Code blocks for multi-step transformations.
  • Handle edge cases. Check for null values, empty arrays, and missing fields before processing.
  • Return structured data. Always return a JSON object so downstream blocks can reference specific fields.
  • Use descriptive variable names. Your code will be visible in logs and to other team members.

The Code block runs in a sandboxed environment. External network calls, file system access, and process-level operations are restricted.

Frequently Asked Questions

On this page