Execute JavaScript in Power Automate

You can run JavaScript code directly within a CustomJS action in your Microsoft Power Automate flow. Perfect for data transformations, API calls, and custom business logic.

๐Ÿ“– Want more examples? Check out our JavaScript in Power Automate Guide with real-world use cases and advanced techniques.

Quick Start Guide

Step 1: Get Your API Key

  1. Visit customjs.space and click Sign in
  2. Complete registration by either providing your email and password or using your Google account
  3. Once registered, you will be redirected to the CustomJS app dashboard
  4. Click Show under API Key to reveal your API key
  5. Copy your API key for the next step
CustomJS API Key Dashboard
CustomJS API Key Dashboard

Step 2: Create Connection in Power Automate

  1. Open your Power Automate flow
  2. Add a new action and search for "CustomJS"
  3. Select "Inline JavaScript" action
  4. When prompted, create a new connection:
    • Connection Name: CustomJS (or any name you prefer)
    • API Key: Paste your API key from Step 1
  5. Click Create
Create CustomJS Connection in Power Automate
Create CustomJS Connection in Power Automate

Step 3: Find the Inline JavaScript Module

  1. In your Power Automate flow, click + New step
  2. Search for "CustomJS"
  3. Select the "Inline JavaScript" action from the list
Find CustomJS Inline JavaScript Module
Find CustomJS Inline JavaScript Module

Step 4: Write Your JavaScript Code

  1. In the Input field, provide data as JSON or plain text
  2. In the JavaScript Code field, write your code
  3. Make sure to end with a return statement
  4. The action returns the result to use in subsequent steps
Write JavaScript Code in Power Automate
Write JavaScript Code in Power Automate

Video Tutorial

Watch this step-by-step video guide on how to execute JavaScript in Power Automate:

How it works

The customJS action consists of four fields: Connection, Input, JavaScript code and Return Type.


FieldDescriptionHow to use it
ConnectionLinks the action to the CustomJS service, enabling JavaScript execution.Select or create a connection (e.g., Coding Service). Usually set up once and reused across flows.
InputData passed into the JavaScript code, available as the input object.Enter plain text, arrays, or valid JSON strings. For Power Automate arrays, you may need to convert them to JSON first.
JavaScript CodeThe script that will be executed by CustomJS. Must follow JavaScript syntax.Ensure the code ends with a return statement so data is passed back to Power Automate.

Input variables

Inputs are accessed through the input object. For complex scenarios, you can:

  • Pass JSON objects as input (must be valid JSON strings).
  • Convert Power Automate arrays to JSON format if needed.
Inputs passed to your script are accessible as an input object. If you're passing multiple variables, itโ€™s recommended to structure them as a JSON object for clarity and ease of use.
Inline JavaScript in Power Automate
Inline JavaScript in Power Automate

Await async results before returning

When using asynchronous code (like fetch, database calls, or API requests), await the result and then return it. If you forget await or donโ€™t return the resolved value, Power Automate will just get a pending Promise, not usable data.


Without awaitWith await
Code
// Incorrect: returns a Promise, not data
return fetch(input.url).then(res => res.json());
// Correct: resolve the Promise before returning
const response = await fetch(input.url);
const data = await response.json();

return { statusCode: response.status, data: data };

ResultPower Automate receives a Promise instead of usable data. The next action cannot work with unresolved Promises.Power Automate receives the actual data (JSON object with status and content). The next action can directly use this result.

Remove inline comments

Inline comments usually work if you place them correctly with line breaks. However, misplaced comments can prevent the code from running as expected. Consider removing them if you have issues.


Inline comments Without inline comments
With inline comments
// return full name
return input.firstName + " " + input.lastName;
return input.firstName + " " + input.lastName;
Result Power Automate may not receive the intended output if the comment interrupts code execution. Comments cannot interrupt code execution.