How can handle Pagination with Azure Resource graph rest api?

Akash Sharma 5 Reputation points
2024-12-11T06:14:24.8866667+00:00

I am trying to fetch details from Azure Resource Graph Explorer via a REST API call. By default, it displays only 1000 entities, but I want to implement pagination with the REST API. How can I achieve this?

Here’s the current code snippet I’m using:

GRAPH_API_ENDPOINT = "https://management.azure.com/providers/Microsoft.ResourceGraph/resources?api-version=2021-03-01"

query = """

resources

| where type == "microsoft.azurearcdata/sqlmanagedinstances"
```"""

headers = {

```javascript
"Authorization": f"Bearer {token}",

"Content-Type": "application/json",
```}

payload = {

```yaml
"subscriptions": [subscription_id],

"query": query
```}

response = requests.post(

```sql
GRAPH_API_ENDPOINT,

headers=headers,

data=json.dumps(payload)
```)

Azure SQL Database
{count} votes

1 answer

Sort by: Most helpful
  1. Mahesh Kurva 5,025 Reputation points Microsoft External Staff Moderator
    2024-12-11T16:23:23.33+00:00

    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.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.