External API call support in Office Scripts - Office Scripts (2024)

  • Article

Scripts support calls to external services. Use these services to supply data and other information to your workbook.

Caution

External calls may result in sensitive data being exposed to undesirable endpoints. Your admin can establish Information Rights Management (IRM) or firewall protection against such calls.

Important

Calls to external APIs can only be made through the Excel application, not through Power Automate under normal circ*mstances. External calls are also not supported for scripts stored on a SharePoint site.

Configure your script for external calls

External calls are asynchronous and require that your script is marked as async. Add the async prefix to your main function and have it return a Promise, as shown here:

async function main(workbook: ExcelScript.Workbook) : Promise <void>

Note

Scripts that return other information can return a Promise of that type. For example, if your script needs to return an Employee object, the return signature would be : Promise <Employee>

You'll need to learn the external service's interfaces to make calls to that service. If you are using fetch or REST APIs, you need to determine the JSON structure of the returned data. For both input to and output from your script, consider making an interface to match the needed JSON structures. This gives the script more type safety. You can see an example of this in Using fetch from Office Scripts.

Limitations with external calls from Office Scripts

  • There is no way to sign in or use OAuth2 type of authentication flows. All keys and credentials have to be hardcoded (or read from another source).
  • There is no infrastructure to store API credentials and keys. This will have to be managed by the user.
  • Document cookies, localStorage, and sessionStorage objects are not supported.
  • External calls may result in sensitive data being exposed to undesirable endpoints, or external data to be brought into internal workbooks. Your admin can establish firewall protection against such calls. Be sure to check with local policies prior to relying on external calls.
  • Be sure to check the amount of data throughput prior to taking a dependency. For instance, pulling down the entire external dataset may not be the best option and instead pagination should be used to get data in chunks.

Retrieve information with fetch

The fetch API retrieves information from external services. It is an async API, so you need to adjust the main signature of your script. Make the main function async. You should also be sure to await the fetch call and json retrieval. This ensures those operations complete before the script ends.

Any JSON data retrieved by fetch must match an interface defined in the script. The returned value must be assigned to a specific type because Office Scripts do not support the any type. You should refer to the documentation for your service to see what the names and types of the returned properties are. Then, add the matching interface or interfaces to your script.

The following script uses fetch to retrieve JSON data from the test server in the given URL. Note the JSONData interface to store the data as a matching type.

async function main(workbook: ExcelScript.Workbook) { // Retrieve sample JSON data from a test server. let fetchResult = await fetch('https://jsonplaceholder.typicode.com/todos/1'); // Convert the returned data to the expected JSON structure. let json : JSONData = await fetchResult.json(); // Display the content in a readable format. console.log(JSON.stringify(json));}/** * An interface that matches the returned JSON structure. * The property names match exactly. */interface JSONData { userId: number; id: number; title: string; completed: boolean;}

Other fetch samples

  • The Use external fetch calls in Office Scripts sample shows how to get basic information about a user's GitHub repositories.
  • Samples in the Use JSON to pass data to and from Office Scripts article show how to pass data to and from fetch commands as JSON.
  • The Office Scripts sample scenario: Graph water-level data from NOAA demonstrates the fetch command being used to retrieve records from the National Oceanic and Atmospheric Administration's Tides and Currents database.
  • The second sample in Add images to a workbook contains a fetch call to get an image from a website.

Restrict external calls with Information Rights Management (IRM)

You can apply IRM settings to a workbook to prevent external calls being made by scripts. Disable the Copy/EXTRACT policy to prevent this behavior.

External calls from Power Automate

External API calls fail when a script is run through Power Automate. A fetch call will give the error message "Runtime error: Line X: fetch is not defined". 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.

Warning

External calls made through the Power Automate Excel Online connector fail in order to help uphold existing data loss prevention policies. However, scripts that are run through Power Automate are done so outside of your organization, and outside of your organization's firewalls. For additional protection from malicious users in this external environment, your admin can control the use of Office Scripts. Your admin can either disable the Excel Online connector in Power Automate or turn off Office Scripts for Excel through the Office Scripts administrator controls.

See also

  • Use JSON to pass data to and from Office Scripts
  • Using built-in JavaScript objects in Office Scripts
  • Use external fetch calls in Office Scripts
  • Office Scripts sample scenario: Graph water-level data from NOAA

As an expert in Office Scripts and external service integration within Excel, I can confidently discuss the concepts mentioned in the provided article. My extensive knowledge in this area is demonstrated through a deep understanding of the key principles and best practices outlined in the article.

The article primarily focuses on the use of scripts to support calls to external services in Excel, highlighting both the capabilities and limitations of such functionality. Let's break down the key concepts discussed:

  1. Caution and Information Rights Management (IRM):

    • The article emphasizes the potential exposure of sensitive data to undesirable endpoints when making external calls. It recommends using Information Rights Management (IRM) or firewall protection to safeguard against such risks.
  2. Power Automate Integration:

    • External API calls can only be made through the Excel application, not through Power Automate under normal circ*mstances. Scripts stored on a SharePoint site do not support external calls. This limitation is crucial for users intending to automate tasks through Power Automate.
  3. Configuring Scripts for External Calls:

    • External calls are asynchronous and require the script to be marked as async. The main function should have the async prefix and return a Promise. This ensures that the script waits for the external call to complete before proceeding.
  4. Interfaces for External Services:

    • To make calls to external services, one needs to understand the interfaces of those services. If using fetch or REST APIs, knowledge of the JSON structure of the returned data is essential. Creating interfaces for input and output data helps enhance type safety in the script.
  5. Limitations and Best Practices:

    • There are limitations to external calls, such as the absence of support for OAuth2 authentication flows. API credentials and keys must be hardcoded or retrieved from another source. Storing credentials and keys is the responsibility of the user, as there is no built-in infrastructure for it.
    • The article advises against using document cookies, localStorage, and sessionStorage objects.
    • External calls may result in exposing sensitive data or bringing external data into internal workbooks, necessitating caution and adherence to local policies.
    • Considerations for data throughput are crucial, and pagination is recommended to handle large datasets efficiently.
  6. Using Fetch API:

    • The fetch API is introduced as a means to retrieve information from external services. The article provides a sample script demonstrating the use of fetch to retrieve JSON data from a test server. It emphasizes the need to match the JSON data structure with defined interfaces.
  7. IRM Settings to Restrict External Calls:

    • Information Rights Management (IRM) settings can be applied to a workbook to prevent external calls made by scripts. Disabling the Copy/EXTRACT policy helps in preventing this behavior.
  8. Power Automate Considerations:

    • External API calls fail when a script is run through Power Automate. The article advises users to check for fetch references in scripts before integrating them into a flow. It suggests using HTTP with Azure AD or equivalent actions for external data interactions.
  9. External Calls through Power Automate Excel Online Connector:

    • External calls made through the Power Automate Excel Online connector fail to uphold data loss prevention policies. Admins can control the use of Office Scripts to enhance protection in the external environment.
  10. Related Samples and Scenarios:

    • The article provides additional samples and scenarios, such as using JSON to pass data to and from Office Scripts, using built-in JavaScript objects, and a scenario graphing water-level data from NOAA.

In summary, the article provides comprehensive guidance on incorporating external service calls into Office Scripts in Excel, covering best practices, limitations, and considerations for various scenarios.

External API call support in Office Scripts - Office Scripts (2024)

FAQs

How do I call an external API in power automate? ›

How to Call an API in Power Automate?
  1. Step 1: Obtain an API Key. To obtain an API key in Power Automate, follow these steps: ...
  2. Step 2: Create an HTTP Request. To create an HTTP request in Power Automate, follow these steps: ...
  3. Step 3: Parse the Response.

What is the limitation of office script? ›

Each user is limited to 1,600 calls to the Run script action per day. This limit resets at 12:00 AM UTC. There's a 120-second timeout for synchronous Power Automate operations.

Can Excel perform API calls? ›

To call Excel JavaScript APIs from a custom function, you first need a context. Use the Excel. RequestContext object to get a context. Then use the context to call the APIs you need in the workbook.

How do I call an external REST API? ›

Steps To Perform To Call An External REST API

Configure REST Adapter Connection: Drag and drop the REST Adapter onto the integration canvas. Configure the connection details, including the base URL of the external REST API, authentication credentials, and any required headers.

Does Excel support APIs? ›

An API is a 'bridge' through which one app obtains/sends information from/to another app. This means that you can load miscellaneous data from your source app to your Excel workbook using the REST API. Moreover, you can even schedule data loads via the API to Excel sheet without any VBA coding!

How do I integrate APIs into Excel? ›

Importing public web API to Excel (no coding required)
  1. To import this on excel go to Data>Get Data>From other Sources> From Web or simply Data>From Web.
  2. Paste the API URL on the prompt then click OK.
  3. Then select Into Table on the Convert tab.
  4. Select the Value of Data then right click>Drill Down.
Mar 15, 2020

Can Excel consume REST API? ›

A REST API for Excel Services enables operations against Excel workbooks by using operations specified in the HTTP standard. This allows for a flexible, secure, and simpler mechanism to access and manipulate Excel Services content.

How to query an API from Excel? ›

How to extract the API data in an Excel sheet
  1. Step 1: retrieve the API data URL.
  2. Step 2: create a new, blank Excel document.
  3. Step 3: paste the API data URL in the Excel document.
  4. Step 4: Enter the credentials.
  5. Step 5: load the API data in the Excel.
Oct 28, 2021

How to use API in VBA? ›

Step by Step Approach
  1. First, you'll need to download the VBA-Web . ...
  2. Unzip the content, and copy the file "VBA-Web - Blank. ...
  3. Rename the file to something more meaningful to your intention, e.g. PIAPIWeb, AFStats, etc.
  4. Open the file.
  5. Copy the content of the Authenticator from GitHub, here.

How do I export API responses to Excel? ›

Steps
  1. Query the API request of your choice in the tool of your choice. ...
  2. Save the response. ...
  3. Inside Excel, create a new project or sheet, Navigate to 'Data > Get Data'
  4. Select 'Get Data > From File > From JSON' (or file of choice)
  5. Select your file you saved from (2)
  6. The query editor wizard will appear.
Nov 1, 2023

How do I call a custom API in power app? ›

  1. Create a custom API record. In your solution, click New > More > Other > custom API from the drop-down. ...
  2. Create any Request Parameters. ...
  3. Create any Response Properties. ...
  4. Observe the result in the service document. ...
  5. Test your custom API. ...
  6. Update the custom API Plugin Type. ...
  7. Other ways to create custom APIs.
Aug 8, 2023

How do I import API into Power Automate? ›

Import the OpenAPI definition for Power Automate and Power Apps
  1. Sign in to Power Apps or Power Automate.
  2. On the left pane, select Data > Custom connectors.
  3. Select New custom connector, and then select Import an OpenAPI file.
Apr 19, 2024

How do I call another API? ›

Constructs the URL for the external API by appending “/api/employee/get-all-employees” to the base URL ( otherApiBaseUrl ). Sets up the necessary HTTP headers, specifying that the client expects JSON responses. Creates an HttpEntity encapsulating the headers.

How do you call an API in power query? ›

Open Power BI Desktop: Open your Power BI Desktop application. Access Power Query Editor: In Power BI Desktop, go to the "Home" tab, and click on "Transform data." This will open the Power Query Editor. Create a Custom Function: In the Power Query Editor, you can create a custom function to make API calls.

Top Articles
6 Benefits Of Drones And Drone Technology - Droneblog
Saving Money On Groceries
Foxy Roxxie Coomer
Duralast Gold Cv Axle
Truist Bank Near Here
Is pickleball Betts' next conquest? 'That's my jam'
Chase Bank Operating Hours
Bucks County Job Requisitions
Los Angeles Craigs List
Gwdonate Org
Tracking Your Shipments with Maher Terminal
Shreveport Active 911
Kris Carolla Obituary
2016 Ford Fusion Belt Diagram
Gon Deer Forum
Bitlife Tyrone's
Overton Funeral Home Waterloo Iowa
Driving Directions To Bed Bath & Beyond
Clear Fork Progress Book
라이키 유출
Tygodnik Polityka - Polityka.pl
A Biomass Pyramid Of An Ecosystem Is Shown.Tertiary ConsumersSecondary ConsumersPrimary ConsumersProducersWhich
Georgia Cash 3 Midday-Lottery Results & Winning Numbers
Cpt 90677 Reimbursem*nt 2023
Craigslist Ludington Michigan
Pixel Combat Unblocked
Tottenham Blog Aggregator
Pfcu Chestnut Street
Metro By T Mobile Sign In
Graphic Look Inside Jeffrey Dresser
2016 Honda Accord Belt Diagram
Does Iherb Accept Ebt
Synchrony Manage Account
Myql Loan Login
Mcgiftcardmall.con
2008 DODGE RAM diesel for sale - Gladstone, OR - craigslist
Paperless Employee/Kiewit Pay Statements
Anhedönia Last Name Origin
Amc.santa Anita
Strange World Showtimes Near Century Stadium 25 And Xd
Port Huron Newspaper
Tacos Diego Hugoton Ks
Phmc.myloancare.com
Dying Light Mother's Day Roof
Das schönste Comeback des Jahres: Warum die Vengaboys nie wieder gehen dürfen
Mlb Hitting Streak Record Holder Crossword Clue
Random Warzone 2 Loadout Generator
Quest Diagnostics Mt Morris Appointment
Julies Freebies Instant Win
Fallout 76 Fox Locations
Goosetown Communications Guilford Ct
Latest Posts
Article information

Author: Otha Schamberger

Last Updated:

Views: 5503

Rating: 4.4 / 5 (75 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Otha Schamberger

Birthday: 1999-08-15

Address: Suite 490 606 Hammes Ferry, Carterhaven, IL 62290

Phone: +8557035444877

Job: Forward IT Agent

Hobby: Fishing, Flying, Jewelry making, Digital arts, Sand art, Parkour, tabletop games

Introduction: My name is Otha Schamberger, I am a vast, good, healthy, cheerful, energetic, gorgeous, magnificent person who loves writing and wants to share my knowledge and understanding with you.