Connections, auth and API

Why do I get "fetch is not defined" in the Run JavaScript step, and how do I call an external API?

Run JavaScript runs server-side without browser APIs. Call the API with the HTTP connector or kf.api(), and use the script only to shape data.

Fix

Short answer: The Run JavaScript code action runs on the server, where browser APIs such as fetch() and File don't exist. Make the call with the HTTP connector, or with kf.api() from the Kissflow SDK.

Why it happens

  • The Run script connector runs your code as an isolated script on the server, not in a browser.
  • Browser objects such as fetch, File and window aren't available there. fetch() works only in client-side code, such as page components and form events.
  • The script also can't read Kissflow system variables such as kf.account._id or kf.context. Pass any value you need in as an input, or call kf.getContext(), which returns the account and app IDs.

How to fix it

To call an external API:

  1. Add a HTTP connector action, such as Make an HTTP call (POST) or Make an HTTP call (Custom), for the request.
  2. If the body needs shaping, add a Run JavaScript code step before it that builds the payload and returns it as an object.
  3. Map the script's output into the HTTP action's body.
  4. To process the response, add another Run JavaScript code step after the HTTP action and map the response into it.

To call an API from inside the script:

  1. Use kf.api(url, config) from the Kissflow SDK. It works for Kissflow APIs and external APIs.
  2. For Kissflow APIs, use the absolute URL, such as https://<subdomain>.kissflow.com/user/2/<account_id>, and pass the access key headers.

To check whether a URL is reachable, call it with the HTTP connector and act on the response.

Important

Calls made with kf.api() time out after 10 seconds. Use the HTTP connector for slow endpoints.

Related answers