cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
plaridel1
Frequent Visitor

ReferenceError: fetch is not defined from running Office Script (API request)

Hi,

 

SUMMARY

 

I wrote an Office Script and it is running fine when you manually run it on Excel Online.

However, when I create a flow and run it, it gives me a fetch undefined error.

 

Do I need to require a fetch? Do I need to add another line of code?

Or is this a bug in Power Automate and Office Script connection?

 

DETAILS

The script I coded is to retrieve data from an external API. I got no problem accessing and retrieving the data when running the script manually on Excel Online.

 

Response from the API GET request is successful...

 

plaridel1_0-1623854236881.png

 

Script completes...

 

plaridel1_1-1623854267140.png

 

I also made sure to catch the error when requesting from the API source. This is my Office Script code which causes the error "fetch is undefined."

 

 

 

 

async function listItems(_url: string, _page? : number): Promise<Response> {  
    const page = (_page) ? '&page=' + _page.toString()     : '';

    try {
      //Note: This works when running it manually on Excel Online
      const response = await fetch(encodeURI(_url + page), {
        method: 'GET',
        headers: {
          'Accept': 'application/json',
          'Authorization': `Bearer ${theCog}`
        }
      }
      );
      //Power Automate flow doesnt even reach here, which means the error occurs on fetch
      console.log(`Request URL: ${response.url}`);

      console.log(`Response Status: ${response.status} ${response.statusText}`);
      return response;
    } catch (e) {

      console.log('Fetch Error: ', e);   
      throw new Error('HTTP Bad Request | Reason: ' + e);

    }
  }

 

 

 

 
This is the output log from my flow.

 

 

 

{
  "message": "We were unable to run the script. Please try again.\nRuntime error: HTTP Bad Request | Reason: ReferenceError: fetch is not defined\r\nclientRequestId: <redacted>",
  "logs": [
    "[2021-06-16T14:42:43.1160Z] Script has started...",
    "[2021-06-16T14:42:43.5380Z] Folder ID: 19<redacted>68(BICOL)",
    "[2021-06-16T14:42:43.5530Z] Retrieving data from API source...",
    "[2021-06-16T14:42:43.5840Z] Fetch Error: "
  ]
}

 

 

 

My flow is pretty simple and my script DO NOT require additional parameters.

What is missing here? I welcome any suggestion or solutions. 😀

 

1 ACCEPTED SOLUTION

Accepted Solutions
plaridel1
Frequent Visitor

It seems that fetching data from an external source through an Office Script is currently not supported by the Power Platform.

 

The only way is to create a custom connector or use the premium HTTP connector.

View solution in original post

3 REPLIES 3
plaridel1
Frequent Visitor

It seems that fetching data from an external source through an Office Script is currently not supported by the Power Platform.

 

The only way is to create a custom connector or use the premium HTTP connector.

View solution in original post

Hi there,

 

Do you happen to have a reference from Microsoft to confirm that fetch via Office Script is actually not supported?

Thanks

Note that my issue above occurs when running Office Script via Power Automate. Below statement proves this.

My alternative was to create a Custom Connector to retrieve the API data.

 

https://docs.microsoft.com/en-us/office/dev/scripts/develop/external-calls#external-calls-from-power...

 

"Any external API call fails when a script is run with Power Automate. This is a behavioral difference between running a script through the Excel application and through Power Automate. Be sure to check your scripts for such references before building them into a flow.

You'll have to use HTTP with Azure AD or other equivalent actions to pull data from or push it to an external service."

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.

Top Solution Authors
Users online (3,212)