# My API script to use a flow as a template

**URL:** <https://connective.celigo.com/t/my-api-script-to-use-a-flow-as-a-template/3709>\
**Category:** AI & Automation in Action\
**Tags:** myapi\
**Created:** [March 7, 2025, 2:55pm UTC](https://connective.celigo.com/t/my-api-script-to-use-a-flow-as-a-template/3709 "2025-03-07T14:55:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![basvanditzhuijzen](https://sea1.discourse-cdn.com/flex001/user_avatar/connective.celigo.com/basvanditzhuijzen/32/4422_2.png) [@basvanditzhuijzen](https://connective.celigo.com/u/basvanditzhuijzen)\
**Post date:** [March 7, 2025, 2:55pm UTC](https://connective.celigo.com/t/my-api-script-to-use-a-flow-as-a-template/3709/1 "2025-03-07T14:55:47Z")

</div>

Hi all,

I wanted to share a script I've been working on recently, it allows you to use a normal integration flow as a template for a My API. This has the advantage that you can use all the visual builder and mapping tools to build your API, and then still run it synchronously.

It can use any webhook flow as a template, as long as it doesn't have branching.

 ![image](https://us1.discourse-cdn.com/flex001/uploads/celigo/original/2X/9/98a781722c8cd22ba47a2affc02d45bfe15de38e.png)

To use simply create a new script in [Integrator.io](http://Integrator.io), paste in the code from below and change the flow id to the flow you want to use as a template. Then create a My API, selecting your new script and the flowRunner function:

 ![image](https://us1.discourse-cdn.com/flex001/uploads/celigo/original/2X/4/4dedc842fa17dd5a44267ed52166fd16d6796088.png)

```auto
import { request, exports, imports, flows } from 'integrator-api'

/*
* flowRunner function:
*
* This function loads the integrator.io flow that is defined on line 16
* and calls every lookup/import step in sequence, and does the response mapping.
* The data posted to the api is passed to the steps, any exports are not run.
* It does not support branching
*/

function flowRunner (options) {
  
  var data = options.body;
  var response = {log: {}};
  
  var flow = flows.get("11cb00d30e1131d11823afe5"); //put your flow id here
  console.info(`Executing MyAPI call on flow ${flow._id}`);
  
  var finalData = flow.pageProcessors.reduce((flowData,step,i) => {

      let ioResponse;
      console.debug(`Step ${i}: ${step.type}`);

      try {
        if (step.type == "export") {
          ioResponse = exports.run({ _id:step._exportId, data:flowData });
          console.debug(`Ran export ${step._exportId}`);
        } else if (step.type == "import") {
          ioResponse = imports.run({ _id:step._importId, data:flowData });
          console.debug(`Ran import ${step._importId}`);
        }
        
        //map response back into data.
        step.responseMapping.fields.forEach(field => {
            flowData[field.generate] = extractField(field.extract,ioResponse); 
            console.debug(`Mapped ${field.generate} ${field.extract} >> ${flowData[field.generate]}`);
        });
        
      }catch(e) {
        ioResponse = JSON.stringify(e)
        console.error(`Error: ${ioResponse}`)
      }
      response.log[i] = {};
      response.log[i].response = ioResponse;

      return flowData;
  },data);
  
  console.debug(JSON.stringify(finalData));
  response.data = finalData;
  
  return {
    statusCode: 200,
    headers: { },
    body: response
  }
}

function extractField(extractStr, responseObject) {
  
  //split the extract into an array like [data 0 EmplName]
  let extractArray = extractStr.split('.');
  
  //traverse the response per entry of the array
  return extractArray.reduce( (node,key) => {
    return node[key];
  },responseObject);
  
}

```

The script will return the data returned by the steps as a single-record 'page' of data, and will also return a log object showing the intermediate results from the individual steps.

 ![This image shows a JSON structure with nested objects and arrays, including keys like "log," "response," "data," "dataURIs," "bob," and "mylookup." (Captioned by AI)](https://us1.discourse-cdn.com/flex001/uploads/celigo/original/2X/8/8649af16d8431bee6562f0646b63ca3dea556714.png)

---

<div class="post-metadata">

**Author:** ![tonycurcio](https://sea1.discourse-cdn.com/flex001/user_avatar/connective.celigo.com/tonycurcio/32/8202_2.png) [@tonycurcio](https://connective.celigo.com/u/tonycurcio)\
**Post date:** [March 10, 2025, 2:08am UTC](https://connective.celigo.com/t/my-api-script-to-use-a-flow-as-a-template/3709/2 "2025-03-10T02:08:07Z")

</div>

Hi @basvanditzhuijzen ... this is very interesting work! Quite clever!

On a similar page, we are a week or so away from the API Builder becoming available. You can read about it in our March release notes. This will allow users to build with the same tools and will run it on our new API engine ... also a synchronous runtime.

> **[Celigo March 2025 Release](https://www.celigo.com/blog/celigo-march-2025-release/)**
>
> Celigo's March release introduces a major refresh of the Flow Builder, and debuts API Builder, a powerful new tool to create APIs through a visual, low-code experience right in integrator.io.

Given you've gone deep into this API thread, I would love to get your feedback on the API Builder once we launch.
