To connect Power Query to a Web API, use the Get Data > From Web option, enter the API endpoint URL, and then use the Power Query Editor to transform the resulting JSON or XML response into a usable table. This process allows you to automate the retrieval of real-time data from external services directly into Excel or Power BI without manually downloading files.
What You'll Need
- Microsoft Excel or Power BI Desktop installed on your machine.
- A valid API Endpoint URL (the web address provided by the service you are connecting to).
- API Authentication credentials, such as an API Key or a Bearer Token, if the data is not public.
- Basic knowledge of JSON structure, as most modern APIs return data in this format.
Step-by-Step
- Initiate the Connection: Open Excel or Power BI. In the ribbon, navigate to the Data tab (Excel) or the Home tab (Power BI). Select Get Data and then choose From Web. Note that menu labels may vary slightly depending on your software version or region.
- Enter the API URL: A dialog box will appear. Select the Basic radio button and paste the full API endpoint URL into the text field. If the API requires a simple key as part of the URL string (a query parameter), include it here. Click OK.
- Configure Authentication: If the API is protected, a prompt will appear asking how you want to access the web content.
- Anonymous: Use this for public APIs that require no login.
- Basic: Use this if you have a username and password.
- API Key: Select this if the service provided a specific key. You will be prompted to enter the key in the provided field.
- Organizational account: Use this for Microsoft 365 based services.
- Parse the JSON Response: Once connected, Power Query will open the Power Query Editor. Most APIs return a JSON object. You will likely see a single entry labeled "Record" or "List". Click on the word Record or List to drill down into the data.
- Convert List to Table: If the API returned a list of items, you will see a column of "Records". To make this usable, go to the Transform tab in the ribbon and select To Table. In the resulting dialog, leave the default settings and click OK.
- Expand the Columns: You will now have a table with one column containing "Record" values. Look for the small icon with two opposing arrows in the top-right corner of the column header. Click this icon. A list of all available fields from the API will appear. Uncheck "Use original column as prefix" to keep your headers clean, then click OK.
- Set Data Types: Power Query often imports everything as "Any" type. Click the data type icon (e.g., ABC/123) next to each column header and assign the correct type, such as Decimal Number for currency or Date for timestamps.
- Load the Data: Once the data is cleaned and formatted, go to the Home tab and select Close & Apply (Power BI) or Close & Load (Excel). The data will now populate your spreadsheet or data model.
Common Mistakes
One of the most frequent errors is forgetting to convert a List to a Table. Users often see a long list of records and assume the process failed, when in reality, they simply need to use the To Table transformation to unlock the column expansion feature. Another common mistake is ignoring the data type step. If a date is left as a "Text" type, you will be unable to perform time-series analysis or use date filters in your reports.
Authentication errors are also common. Users often try to put an API key into the "Basic" username/password fields instead of using the dedicated "API Key" authentication method. Additionally, some users attempt to connect to an API that requires a POST request (sending data to the server) using the standard "From Web" interface, which only performs GET requests. For POST requests, you must use the Advanced Editor to write an Web.Contents function with a Content option.
Tips
To handle APIs that use pagination (where data is split across multiple pages), you will need to create a custom function in Power Query. This involves identifying the "next page" token in the API response and using a recursive loop or List.Generate to fetch all pages until the token is null. This ensures you aren't just seeing the first 50 or 100 records provided by the default API limit.
For security, never hard-code your API keys directly into the M code in the Advanced Editor if you plan to share the file. Instead, create a separate Parameter in Power Query (Home > Manage Parameters) and use that parameter in your web request. This allows you to change the key in one place without digging through the code, and it makes the workbook easier to manage across different environments.
If the API response is very large, consider using the Remove Columns step early in your process. By removing unnecessary fields immediately after expanding the record, you reduce the memory load on your machine and speed up the refresh time of the dataset.
Troubleshooting
If the connection fails with a "403 Forbidden" or "401 Unauthorized" error, the first step is to verify your API key. Copy the URL and paste it into a web browser; if the browser cannot load the data, the issue is with the API key or the account permissions, not Power Query. Ensure that the API key has not expired and that you have the correct scopes enabled for the data you are requesting.
If you encounter a "DataFormat.Error: We were unable to find a table" message, it usually means the API returned an error message in JSON format (like an error code) instead of the expected data. Open the Advanced Editor and check the Source step. If the API returned a 404 or 500 error, the JSON structure will be different from the successful response, causing the subsequent "Expand" steps to fail. Check the API service status page to see if the server is currently down.
When the data refreshes slowly or times out, check if the API has a rate limit. Many services limit the number of requests per minute. If you are refreshing multiple tables from the same API, Power Query may be sending requests too quickly. You can try to consolidate your requests into a single call or use a more efficient API endpoint that returns a larger batch of data per request.
Frequently Asked Questions
Can I refresh API data automatically?
Yes. In Power BI, you can schedule refreshes via the Power BI Service. In Excel, you can set the connection to refresh every X minutes or refresh upon opening the file via the Connection Properties menu.
What is the difference between Basic and API Key authentication?
Basic authentication uses a username and password pair. API Key authentication uses a single unique string provided by the developer to identify the requester and grant access.
Why does my API data show up as "Record"?
This happens because APIs return data in JSON format, which is hierarchical. The "Record" label is a placeholder for a set of fields that must be expanded into columns using the expand button in the column header.
Can I use Power Query to send data to an API?
Power Query is primarily designed for data extraction (GET). While it is technically possible to trigger a POST request using Web.Contents, it is not the intended use case and can be difficult to manage without advanced M coding.
Do I need to install any plugins to connect to an API?
No. The "From Web" connector is built directly into Power Query in both Excel and Power BI, so no external plugins or drivers are required for standard REST APIs.