Expand my Community achievements bar.

Using fetch in Adobe IO


Level 1


I'm trying to use simple script to make POST API call to external endpoint using Adobe IO. Under this link (https://developer.adobe.com/runtime/docs/guides/reference/runtimes/) I found information that node-fetch should be built in and work out of the box. However, when I try to run my action I'm receiving this error:


"error": "fetch is not defined"


If I'm trying to import it using "import fetch from 'node-fetch';" I'm receiving:


"error": "Initialization has failed due to: SyntaxError: Cannot use import statement outside a module\n at NodeActionRunner.init (/nodejsAction/services/runner.js:91:109)\n at doInit (/nodejsAction/services/service.js:225:31)\n at initCode (/nodejsAction/services/service.js:161:24)\n at /nodejsAction/routes/action.js:25:13\n at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:95:5)\n at next (/node_modules/express/lib/router/route.js:144:13)\n at Route.dispatch (/node_modules/express/lib/router/route.js:114:3)\n at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:95:5)\n at /node_modules/express/lib/router/index.js:284:15\n at Function.process_params (/node_modules/express/lib/router/index.js:346:12)"

 And when I try to import it with "const fetch = require('node-fetch');" (which I'm quite sure worked for me a year or so ago), I'm getting:

    "error": "An error has occurred: Error [ERR_REQUIRE_ESM]: require() of ES Module /node_modules/node-fetch/src/index.js from /nodejsAction/services/runner.js not supported.\nInstead change the require of index.js in /nodejsAction/services/runner.js to a dynamic import() which is available in all CommonJS modules."


So what's going on with that and how can I use fetch in Adobe IO?

Here is my full code in case it's helpful:

async function main(params) {
  // User-provided parameters
  const parameter1 = params.parameter1 || 'example';
  const parameter2 = params.parameter2 || '123';

  // API endpoint URL
  const apiUrl = 'https://myurl.com/';

  // Create the request body
  const requestBody = JSON.stringify({
    event_code: parameter1,
    quantity: parameter2

  // Set up the fetch options
  const requestOptions = {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
	  'X-DES-AK-TENANT-TOKEN': 'abcdef'
    body: requestBody

  try {
    // Fetch data from the API
    const response = await fetch(apiUrl, requestOptions);
    const data = await response.json();

    // Process the API response data
    return { result: data };
  } catch (error) {
    // Handle any errors that occurred during the fetch
    console.log('Error:', error);
    return { error: error.message };

// exports.main = main;



Topics help categorize Community content and increase your ability to discover relevant content.

1 Reply



You are correct, node-fetch is built in, per https://developer.adobe.com/runtime/docs/guides/reference/runtimes/

What node version are you deploying to?


example from ext.config.yaml


  function: actions/app/index.js
  web: 'yes'
  runtime: nodejs:16





Also, how are you deploying your code?