Web.Contents()
Learn how Web.Contents fetches data from a URL, the options that control headers and query parameters, and why RelativePath matters for query folding and refresh in the Service.
Web.Contents()
Web.Contents() fetches raw content from a URL — the function behind every web-based connector, from a simple public JSON endpoint to an authenticated REST API.
Web.Contents(
url as text,
optional options as nullable record
) as binaryIt returns raw binary content — almost always piped straight into Json.Document() or Xml.Tables() to parse the response into something Power Query can work with.
Basic Example
Source = Json.Document(Web.Contents("https://api.example.com/products"))Web.Contents fetches raw bytes
|
| Json.Document parses them
|
A record or list, ready to convert to a table with Table.FromRecords or similarQuery Parameters: Use the Query Option, Not String Concatenation
Source = Json.Document(
Web.Contents(
"https://api.example.com/products",
[Query = [category = "bikes", limit = "50"]]
)
)This builds ?category=bikes&limit=50 correctly, including proper URL encoding — string-concatenating query parameters onto the URL directly works for simple cases but breaks silently the moment a value contains a character that needs encoding.
RelativePath: Why It Matters for the Service
Source = Json.Document(
Web.Contents(
"https://api.example.com",
[RelativePath = "products", Query = [category = "bikes"]]
)
)Splitting the URL into a base (https://api.example.com) and a RelativePath gives the same request, but it matters once the report is published. Before a scheduled refresh, the Power BI Service works out each data source from the query text, without running it. A fixed base URL as the first argument is something it can read. A URL built from other values (a page number, a date, a value from another query) can't be known until the query runs, so the Service reports a dynamic data source and won't schedule refresh.
Keep the first argument a fixed text value, and put everything that varies into RelativePath and Query. Credentials are then set once for that base URL.
Headers and Authentication
Source = Json.Document(
Web.Contents(
"https://api.example.com/products",
[Headers = [#"Authorization" = "Bearer " & apiKey, #"Accept" = "application/json"]]
)
)For anything beyond a public, unauthenticated endpoint, credentials typically belong in Data Source Settings (Anonymous, Basic, Web API key, or Organizational account) rather than hard-coded directly in the query — hard-coding a key here means it travels with the query text itself, including into version control if the .pbix is stored there.
Handling Non-200 Responses
By default, any HTTP response outside the 200 range throws an error, which stops the query entirely. ManualStatusHandling opts out of that, letting the query inspect the response itself.
Response = Web.Contents(
"https://api.example.com/products",
[ManualStatusHandling = {404, 500}]
),
StatusCode = Value.Metadata(Response)[Response.Status]Useful for an API where a 404 is a meaningful, expected response (e.g., "no data for this date") rather than a genuine failure that should stop the refresh.
Common Mistakes
Concatenating Query Parameters Into the URL String
"https://api.example.com/products?category=" & category works until category contains a space, an &, or another character that needs URL encoding — the Query record option handles this automatically.
Building the URL Dynamically Instead of Using RelativePath
Web.Contents("https://api.example.com/data?page=" & Text.From(page)) works in Desktop, then can't be scheduled in the Service, because the data source isn't known until the query runs. Keep the base URL fixed and move the variable parts into RelativePath and Query. See Web.Contents with a dynamic URL.
Not Handling Formula.Firewall When Combining With Other Sources
Passing a value from another query or source into Web.Contents() per row (inside a custom column, for example) is the cross-source pattern that triggers the Formula.Firewall error — see the Formula.Firewall error for why, and how to restructure the queries.
Best Practices
- Use the
Queryrecord option for query parameters, never manual string concatenation. - Keep the first argument a fixed base URL and put the varying parts in
RelativePathandQuery, so scheduled refresh works in the Service. - Keep credentials in Data Source Settings, not hard-coded into the query text.
- Use
ManualStatusHandlingonly for response codes that are genuinely expected and meaningful, not to silently swallow real failures.
Next Steps
Continue learning Power Query:
- List.Generate
- M Language
- M Function Reference
- Json.Document() — parsing the response this usually feeds into
Getting a "Formula.Firewall" error combining this with another source? See the Formula.Firewall error.
Scheduled refresh turned off because the dataset "includes a dynamic data source"? See Web.Contents with a dynamic URL for the RelativePath fix.
List.Accumulate()
Learn how List.Accumulate reduces a list to a single value by carrying state across every item — the general-purpose function behind running totals and custom aggregations that no built-in function covers.
Sql.Database()
Learn how Sql.Database connects to SQL Server, why pasting a native SQL query breaks query folding, the auth issues that only surface once a report moves to a gateway, and Import vs. DirectQuery for SQL sources.