Using Power BI REST API

Loading

Using Power BI REST API – Comprehensive Guide

Power BI REST API allows developers to interact programmatically with Power BI service, automate tasks, embed reports, manage workspaces, and more. This guide provides a detailed, step-by-step walkthrough of using the Power BI REST API.


🔹 Prerequisites

Before working with Power BI REST API, ensure you have the following:

  1. Power BI Pro or Premium License
    • Required for accessing API endpoints.
  2. Azure Active Directory (Azure AD) Application
    • Used for authentication.
  3. Power BI Service Account
    • An account with access to Power BI reports and workspaces.
  4. API Permissions
    • Permissions must be granted for API access in Azure AD.
  5. Power BI Workspace and Reports
    • The API will interact with workspaces, datasets, reports, etc.

🔹 Step 1: Register an Azure AD App for Power BI API Access

Power BI REST API requires authentication via an Azure AD app. Follow these steps to register an app in Azure Portal.

1️⃣ Register the App in Azure AD

  1. Go to Azure Portal.
  2. Navigate to Azure Active Directory → App registrations.
  3. Click New Registration.
  4. Enter the app name (e.g., PowerBI-API-App).
  5. Choose Accounts in this organizational directory only.
  6. Click Register.

2️⃣ Generate Client Secret

  1. Open your registered app in Azure AD.
  2. Navigate to Certificates & Secrets → Click New Client Secret.
  3. Set expiration (e.g., 1 year, 2 years, etc.).
  4. Click Add, then Copy the Secret Value.

3️⃣ Assign API Permissions

  1. Open API Permissions in the Azure app.
  2. Click Add a permissionAPIs my organization uses.
  3. Search for Power BI Service and select it.
  4. Add the following Delegated Permissions:
    • Dashboard.ReadWrite.All
    • Dataset.ReadWrite.All
    • Report.ReadWrite.All
    • Workspace.ReadWrite.All
    • Content.Create
  5. Click Grant Admin Consent to enable permissions.

4️⃣ Capture App Credentials

Save the following details for API requests:

  • Tenant ID: Found in Azure AD Overview.
  • Client ID: Found in App Overview.
  • Client Secret: Generated earlier.

🔹 Step 2: Authenticate Using Power BI REST API

Power BI REST API uses OAuth 2.0 Authentication. Follow these steps:

1️⃣ Get Access Token

Use the following API request to get a token:

Request (POST)

https://login.microsoftonline.com/{tenant-id}/oauth2/token

Headers

Content-Type: application/x-www-form-urlencoded

Body (Form-Data)

grant_type=client_credentials
client_id={your-client-id}
client_secret={your-client-secret}
resource=https://graph.microsoft.com/

Response (Example)

{
  "token_type": "Bearer",
  "expires_in": 3600,
  "access_token": "eyJ0eXA... (truncated)"
}

Save the access_token, as it is required for API calls.


🔹 Step 3: Using Power BI REST API

Once authenticated, you can perform actions like managing workspaces, reports, datasets, and more.


📌 Example 1: Get List of Workspaces

Request (GET)

https://api.powerbi.com/v1.0/myorg/groups

Headers

Authorization: Bearer {access_token}
Content-Type: application/json

Response

{
  "value": [
    {
      "id": "group-id",
      "name": "Sales Workspace",
      "isReadOnly": false
    }
  ]
}

📌 Example 2: Get Reports in a Workspace

Request (GET)

https://api.powerbi.com/v1.0/myorg/groups/{workspace-id}/reports

Response

{
  "value": [
    {
      "id": "report-id",
      "name": "Sales Report",
      "webUrl": "https://app.powerbi.com/groups/report-id"
    }
  ]
}

📌 Example 3: Embed a Report

Request (GET)

https://api.powerbi.com/v1.0/myorg/groups/{workspace-id}/reports/{report-id}

Response

{
  "id": "report-id",
  "name": "Sales Report",
  "embedUrl": "https://app.powerbi.com/reportEmbed?reportId=report-id"
}

Use embedUrl in SharePoint or Teams for embedding.


📌 Example 4: Refresh Dataset

Request (POST)

https://api.powerbi.com/v1.0/myorg/groups/{workspace-id}/datasets/{dataset-id}/refreshes

Response

{
  "id": "refresh-id",
  "status": "InProgress"
}

📌 Example 5: Create a New Workspace

Request (POST)

https://api.powerbi.com/v1.0/myorg/groups

Body

{
  "name": "New Workspace"
}

Response

{
  "id": "new-workspace-id",
  "name": "New Workspace"
}

🔹 Automating Power BI Tasks

You can automate: ✅ Dataset Refreshing – Schedule using Power Automate.
User Management – Assign roles via API.
Embedding Reports – Automate dashboards in SharePoint or Teams.
Workspace Management – Create, delete, and modify workspaces.


🔹 Power BI REST API Best Practices

Use Service Principal – Instead of user authentication.
Cache Access Tokens – Tokens last for 1 hour, avoid frequent requests.
Monitor API Limits – Power BI API has rate limits (100 requests per minute).
Secure Credentials – Never expose client secret in frontend apps.
Use Power Automate – To trigger API calls automatically.


🔹 Common Errors and Troubleshooting

ErrorSolution
401 UnauthorizedEnsure access token is valid and permissions are granted.
403 ForbiddenCheck if the user has Power BI Pro license and API permissions.
404 Not FoundVerify the workspace/report ID is correct.
429 Too Many RequestsAPI rate limit exceeded. Use retry logic.

Conclusion

Power BI REST API is a powerful tool for automating data management, embedding reports, and enhancing Power BI functionality. With proper authentication and API calls, you can efficiently integrate Power BI with custom applications, SharePoint, and Teams.

Now you can start automating and embedding Power BI like a pro!

Leave a Reply

Your email address will not be published. Required fields are marked *