Troubleshooting API Key Authentication Failures in Excel Web Queries

API key authentication failures in Power Query typically manifest as DataSource.Error: Access to the resource is forbidden (HTTP 403), HTTP 401 Unauthorized, or continuous credential prompt loops. These errors occur because Excel’s built-in “Web API” authentication modal sends credentials in a standard format that many modern APIs reject, or because dynamic URL concatenation triggers Power Query’s Data Privacy Firewall, stripping the authentication headers.

Fast Fix

Do not use Excel’s default “Web API” credential prompt. Instead, set the data source credential to Anonymous in Data Source Settings. In the Power Query Advanced Editor, manually declare your authentication key inside the Headers or Query record of Web.Contents() (e.g., Headers=[#"x-api-key"="YOUR_KEY"]). Use ApiKeyName only if the endpoint supports standard key parameters.


Diagnostic Snapshot

PropertyDetails
Typical Error CodesHTTP 401 Unauthorized / HTTP 403 Forbidden / Formula.Firewall
Core VulnerabilityExcel’s GUI “Web API” option transmits keys in a format many third-party REST services do not accept
Common Header Formatsx-api-key: [key], Authorization: Bearer [token], or URL parameter ?api_key=[key]
Privacy ImpactDynamic string concatenation in Web.Contents breaks refresh scheduling and header delivery

Why API Key Authentication Fails in Excel

When you select From Web in Excel and enter an API Key into the Web API authentication box, Power Query has to guess how the endpoint expects that key to be delivered.

[User Enters API Key in Excel GUI]
               │
               ▼
[Excel Sends: Authorization: <key>]  ──► [Target Web API Endpoint]
                                                     │
                                             EXPECTED EITHER:
                                             1. Header: 'x-api-key'
                                             2. Query: '?key=value'
                                                     │
                                                     ▼
                                          REJECTED: 401 / 403

Most modern REST APIs do not accept a bare API key in a generic authorization field. Instead, they require one of three specific patterns:

  1. Custom Request Headers: e.g., x-api-key: your_key_here or X-Auth-Token: your_key_here.
  2. Bearer Authorization: e.g., Authorization: Bearer your_token_here.
  3. Query String Parameters: e.g., https://api.service.com/v1/data?apikey=your_key_here.

If you enter your key into Excel’s dialog box, but the API expects a custom header like x-api-key, the request arrives at the server without the required header, resulting in an immediate rejection.


Step-by-Step Resolution

Step 1: Clear Stale or Incorrect Cached Credentials

First, remove any broken credentials that Excel has cached for the API domain:

  1. In Excel, navigate to Data > Get Data > Data Source Settings.
  2. Select Global permissions.
  3. Locate the root domain of your API (e.g., https://api.example.com).
  4. Click Clear Permissions.
  5. Click Close.
[Data Source Settings]
  └── Global permissions
        └── Select: <https://api.service.com> ──► [Clear Permissions]

Step 2: Implement the Key via Custom Headers in M-Code

If the API documentation specifies passing the key as a custom header (such as x-api-key), declare it directly inside the Headers option of Web.Contents.

  1. In Excel, go to Data > Queries & Connections and open your query in the Power Query Editor.
  2. Click Home > Advanced Editor.
  3. Rewrite your connection step using this format:
let
    // Base endpoint without dynamic query strings
    BaseUrl = "<https://api.example.com/v1>",

    Source = Json.Document(
        Web.Contents(
            BaseUrl,
            [
                RelativePath = "reports/daily",
                Headers = [
                    #"Content-Type" = "application/json",
                    #"x-api-key" = "YOUR_ACTUAL_API_KEY_HERE"
                ]
            ]
        )
    )
in
    Source
  1. Click Done.
  2. When Excel prompts you for credentials to access the web content, select Anonymous in the left menu and click Connect.

Why Anonymous? Because your M-code is already supplying the necessary security tokens directly inside the HTTP header payload. Selecting Anonymous tells Excel’s underlying network client not to interfere with the request.


Step 3: Implement the Key via Query String Parameters

If your API documentation requires the key in the URL path (e.g., ?access_token=...), do not concatenate it directly into the URL string like this:
Web.Contents("<https://api.com?key=>" & MyKey). That pattern triggers the Power Query Data Privacy Firewall and prevents scheduled cloud refreshes.

Instead, use the Query option inside Web.Contents:

let
    Source = Json.Document(
        Web.Contents(
            "<https://api.example.com/v1/data>",
            [
                Query = [
                    api_key = "YOUR_ACTUAL_API_KEY_HERE",
                    format = "json",
                    limit = "500"
                ]
            ]
        )
    )
in
    Source

Power Query safely encodes the query parameters into the URL request during transmission while maintaining privacy compliance. When prompted, select Anonymous authentication.


Step 4: Resolve Data Privacy Firewall Blocks on Keys

If your API key is stored in another query, an Excel cell, or an environmental variable, referencing it inside Web.Contents will trigger:
Formula.Firewall: Query references other queries...

To resolve this:

  1. Go to Data > Get Data > Query Options.
  2. Under Current Workbook, select Privacy.
  3. Select Ignore the Privacy levels and potentially improve performance.
  4. Click OK.

For full structural solutions to firewall blocks without compromising privacy settings, see How to Fix Power Query “Formula.Firewall: Query references other queries”.

If the API continues to return 403 Forbidden errors after headers are applied, review How to Resolve Power Query “Access to the resource is forbidden” (403 Error). If the web service returns internal 500 errors, see “DataSource.Error: Web.Contents failed to get contents” (500 Internal Error). If your stored credentials fail to persist across Excel restarts, refer to How to Resolve “Credential persistence” Errors in M365 Excel.