web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Script having external...
Power Automate
Unanswered

Script having external api calls using fetch is not working

(0) ShareShare
ReportReport
Posted on by 2

Hello,

We've created an office script, where we're using external API calls using fetch. But the API request got stuck in the Pending state. 

Following this documentation: Use external fetch calls in Office Scripts - Office Scripts | Microsoft Learn

There's no related error or warning in the console or debug panel.

Also, CORS headers are allowed as wildcards. 

 

Any suggestions are really appreciated. 

 

Regards

Yogesh

Categories:
I have the same question (0)
  • MichelleRanMSFT Profile Picture
    Microsoft Employee on at

    Hi @yogeshagarwal, sorry to hear you're running into issues with API calls! Could you share your script code (sensitive data removed as needed) so we can try to reproduce this issue? And to double check, are you running your script from Excel (and not from Power Automate)?

  • yogeshagarwal Profile Picture
    2 on at

    Hi @MichelleRanMSFT, Thanks for reverting back. 

    Yes I'm doing an API call in Excel Script. Below is the code. Attached is the Rest API activity Network tab.

     

    function main(workbook: ExcelScript.Workbook😞 void {
        const sourceSheet = workbook.getWorksheet('Sheet1');
        const apiUrl = 'https://API_URL';

        const requestOptions: RequestInit = {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
        };

        function sendData() {
            const sourceRange = sourceSheet.getUsedRange();
            const sourceValues = sourceRange.getValues();
            console.log(sourceValues);
            requestOptions.body = JSON.stringify(sourceValues);

            fetch(apiUrl, requestOptions)
                .then((response) => {
                    if (!response.ok) {
                        throw new Error(`HTTP error! Status: ${response.status}`);
                    }
                    return response;
                })
                .then((data) => {
                    console.log('success:');
                })
                .catch((error) => {
                    console.log('Error:', error);
                });
        }
        sendData();
    }
  • MichelleRanMSFT Profile Picture
    Microsoft Employee on at

    Since fetch returns a promise, you need to await it:

    async function main(workbook: ExcelScript.Workbook) {
     const sourceSheet = workbook.getWorksheet('Sheet1');
     const apiUrl = 'https://API_URL';
    
     const requestOptions: RequestInit = {
     method: 'POST',
     headers: {
     'Content-Type': 'application/json',
     },
     };
    
     function sendData() {
     const sourceRange = sourceSheet.getUsedRange();
     const sourceValues = sourceRange.getValues();
     console.log(sourceValues);
     requestOptions.body = JSON.stringify(sourceValues);
    
     return fetch(apiUrl, requestOptions)
     .then((response) => {
     if (!response.ok) {
     throw new Error(`HTTP error! Status: ${response.status}`);
     }
     return response;
     })
     .then((data) => {
     console.log('success:');
     })
     .catch((error) => {
     console.log('Error:', error);
     });
     }
     await sendData();
    }

     Does that work?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 501 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 323 Moderator

#3
abm abm Profile Picture

abm abm 237 Most Valuable Professional

Last 30 days Overall leaderboard