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

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

Reply
Anonymous
Not applicable

API Pagination securely - and without a Next Page link

Hi,

 

I already have a solution to get an unknown number of pages of data from an API, but need some help to improve the security of it.  This API does not provide a 'Next Page' link, so what the following query does is firstly, a query to count the number of records.  It then loops through and retrieves pages of 2000 records until it reaches the count value + 2000.  This works fine right now, other than the security aspect...

 

The following PowerQuery code works, but only if we hard-code a 'sessionID' or 'apiKey' parameter as part of the URL.  This is not great from a security perspective as the URL could be logged.  This API does allow you to send the sessionID/apiKey via the request Headers, but the developer who wrote the query for me couldn't get this to work with Power Query.  I believe this was related to the use of RelativePath. 

 

We need this query to be able to work to refresh data in the Power BI Service - not just Desktop.  I saw a different solution on a thread somewhere but the user said that it wouldn't work in the Power BI Service due to the use of a function?

 

So my main question I'd love some help onIs there a way to modify the below query (or replace it with a different solution) so that we can pass the sessionID/apiKey in the request Headers?  

 

If that's not possible, passing the sessionID in the URL would be OK enough, as that changes frequently.  It's hard-coded in the query for now, but I expect I could add it as another line where it first does an API call to do the login, which generates the sessionID.  We'd then need to use the result of that in the other API calls where they do the count and retrieve the actual records. 

 

Any guidance on this would be super appreciated! Thank you.

 

let
CountSource = Json.Document(Web.Contents("https://myapi.com/count?sessionID=331fbae3919942f79f14894865eaf47f")),
Count = List.First(Record.FieldValues(CountSource[data])),
TableList = List.Generate( () => [i = 1, sourcetable = Table.FromList(Json.Document(Web.Contents("https://myapi.com/search?sessionID=331fbae3919942f79f14894865eaf47f&$$LIMIT=2000&ID_Sort=asc&$$FIRST...", [RelativePath="1"]))[data], Splitter.SplitByNothing(), null, null, ExtraValues.Error)], each [i] <= Count+2000, each let upto = Number.ToText([i]), GetTable = (upto as text) => Table.FromList(Json.Document(Web.Contents("https://myapi.com/search?sessionID=331fbae3919942f79f14894865eaf47f&$$LIMIT=2000&ID_Sort=asc", [Query=[#"$$FIRST"=upto]]))[data], Splitter.SplitByNothing(), null, null, ExtraValues.Error) in [i = [i] + 2000, sourcetable = Function.Invoke(()=>GetTable(upto), {})], each [sourcetable]),
#"CombinedTable" = Table.Combine(TableList)
in
#"CombinedTable"

1 REPLY 1
v-alq-msft
Community Support
Community Support

Hi, @Anonymous 

 

You may set the second parameter for Web.Contents like below.

 

 

Web.Contents(
  "https://www.regonline.com/api/default.asmx/GetEvents",
  [
   Query=[ #"filter"="", #"orderBy"=""],
   Headers=[#"APIToken" = "insertAPITokenHere"]
  ]
)

 

 

 

For further information, you may refer to the following blog.

Using The RelativePath And Query Options With Web.Contents() In Power Query And Power BI M Code 

 

Best Regards

Allan

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 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.

Top Kudoed Authors