Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
sean-hum
New Member

ApiKeyName Not Allowing Authentication

Hello,

 

I am attempting to secure my Web API connection in Power BI by taking my bearer token out of the M query using the ApiKeyName option in Web.Contents. I am able to connect to the dataset using this M query:

= Json.Document(Web.Contents(
"https://website.com",
[
Headers = [#"X-Authorization"="ABCD"],
RelativePath = "/api/blahblahblah"
]
))

But when I attempt to use this query:

= Json.Document(Web.Contents(
"https://website.com",
[
ApiKeyName= "X-Authorization",
RelativePath = "/api/blahblahblah"
]
))

 

...the authentication fails after entering the key under the "API Key" option in the credentials dialog box. What am I doing wrong here?

1 ACCEPTED SOLUTION
Ahmed-Elfeel
Solution Sage
Solution Sage

Hi @sean-hum,

I hope you are doing well today ☺️❤️

 

You are not doing anything wrong but also you are too close....but the ApiKeyName doesnt add the API key to request headers (It only tells Power BI the name of a query-string parameter (?api_key=something)

And your call is pssing the token as a header like this 

Headers = [ #"X-Authorization" = "ABCD" ]

Then when you switch to (ApiKeyName = "X-Authorization")....Power BI tries to send the key as ?X-Authorization=… in the URL and since the API expects it in the header authentication will fails that is why it breaks..You could find more about Web.Contents & PowerQueryM

 

So to solve this here is some Approaches you should try:

 

First Approach: If the API supports a query parameter

  • If the API accepts the key in the URL use the name instead:
Json.Document(
  Web.Contents(
    "https://website.com",
    [
      RelativePath = "/api/blahblahblah",
      ApiKeyName   = "api_key"   // whatever parameter name your API expects
    ]
  )
)

 

Second Approach: If the API requires the key in a header (Thats is more Secure)

if this post helps, then I would appreciate a thumbs up and mark it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
sean-hum
New Member

Thanks for the thorough reply!

Ahmed-Elfeel
Solution Sage
Solution Sage

Hi @sean-hum,

I hope you are doing well today ☺️❤️

 

You are not doing anything wrong but also you are too close....but the ApiKeyName doesnt add the API key to request headers (It only tells Power BI the name of a query-string parameter (?api_key=something)

And your call is pssing the token as a header like this 

Headers = [ #"X-Authorization" = "ABCD" ]

Then when you switch to (ApiKeyName = "X-Authorization")....Power BI tries to send the key as ?X-Authorization=… in the URL and since the API expects it in the header authentication will fails that is why it breaks..You could find more about Web.Contents & PowerQueryM

 

So to solve this here is some Approaches you should try:

 

First Approach: If the API supports a query parameter

  • If the API accepts the key in the URL use the name instead:
Json.Document(
  Web.Contents(
    "https://website.com",
    [
      RelativePath = "/api/blahblahblah",
      ApiKeyName   = "api_key"   // whatever parameter name your API expects
    ]
  )
)

 

Second Approach: If the API requires the key in a header (Thats is more Secure)

if this post helps, then I would appreciate a thumbs up and mark it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.