Hi @Akash Sharma,
Welcome to the Microsoft Q&A and thank you for posting your questions here.
I want to implement pagination with the REST API. How can I achieve this?
To implement pagination with the Azure Resource Graph REST API, you can use the skip Token parameter.
Here’s how you can modify your code to handle pagination:
- Make the initial request as you are currently doing.
- If the response contains a skip Token, use it to fetch the next set of results.
- Continue making requests using the skip Token until there are no more tokens.
Here’s an updated version of your code snippet to include pagination:
import requests
import json
GRAPH_API_ENDPOINT = "https://management.azure.com/providers/Microsoft.ResourceGraph/resources?api-version=2021-03-01"
query = """
resources
| where type == "microsoft.azurearcdata/sqlmanagedinstances"
"""
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
}
payload = {
"subscriptions": [subscription_id],
"query": query
}
results = []
skip_token = None
while True:
if skip_token:
payload["options"] = {"$skipToken": skip_token}
response = requests.post(
GRAPH_API_ENDPOINT,
headers=headers,
data=json.dumps(payload)
)
response_data = response.json()
results.extend(response_data.get("data", []))
skip_token = response_data.get("skipToken")
if not skip_token:
break
# Now `results` contains all the paginated results
print(results)
For more information, please refer the document: https://learn.microsoft.com/en-us/azure/governance/resource-graph/paginate-powershell?source=recommendations#paginate-azure-resource-graph-query-results
Hope this helps. Do let us know if you any further queries.
If this answers your query, do click Accept Answer
and Yes
for was this answer helpful. And, if you have any further query do let us know.