Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
1 year ago

API for Subcription

Hi All,

         Is there any specific API to set the subcriptions for each reports in power bi. If not means, any ideas to do that through Automation.

3 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi, Anonymous 

    In Power BI, you can use the REST API to manage report subscriptions. Here are some relevant API operations:

    1.Create Subscription: Use the Add Subscription API to create a new subscription for a specific report.

     

    2.Get Subscriptions: Use the Get Subscriptions API to retrieve all subscriptions for a specific report.

     

    3.Update Subscription: Use the Update Subscription API to update an existing subscription.

     

    4.Delete Subscription: Use the Delete Subscription API to remove a specific subscription.

     

    Here is a screenshot from the relevant documentation:

     

    Here is the link to the relevant documentation:

    Subscriptions - REST API (Power bi report) | Microsoft Learn

    Subscriptions - Add Subscription - REST API (Power bi report) | Microsoft Learn

    Subscriptions - Delete Subscription - REST API (Power bi report) | Microsoft Learn

    Subscriptions - Execute Subscription - REST API (Power bi report) | Microsoft Learn

    Subscriptions - Get Subscription - REST API (Power bi report) | Microsoft Learn

     

    Best Regards,

    Leroy Lu

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

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Anonymous , I already tried with these API. None of them are working. Pls check this below code. I am getting this error.

      Failed to create subscription. Status Code: 404, Response: {
        "error":{
          "code":"","message":"No HTTP resource was found that matches the request URI}}


      --Code--

      import requests
      import json

      # Define constants
      CLIENT_ID = '#######################'
      CLIENT_SECRET = '#######################'
      TENANT_ID = '#######################'
      RESOURCE = 'https://analysis.windows.net/powerbi/api'
      API_URL = 'https://api.powerbi.com/v1.0/myorg/groups/{GROUP_ID}/reports/{REPORT_ID}/subscriptions'

      def get_access_token():
      url = f'https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0/token'
      headers = {
      'Content-Type': 'application/x-www-form-urlencoded'
      }
      body = {
      'grant_type': 'client_credentials',
      'client_id': CLIENT_ID,
      'client_secret': CLIENT_SECRET,
      'scope': RESOURCE + '/.default'
      }
      response = requests.post(url, headers=headers, data=body)
      if response.status_code == 200:
      return response.json().get('access_token')
      else:
      print(f"Failed to obtain access token. Status Code: {response.status_code}, Response: {response.text}")
      return None

      def create_subscription(access_token, group_id, report_id):
      url = API_URL.format(GROUP_ID=group_id, REPORT_ID=report_id)
      headers = {
      'Authorization': f'Bearer {access_token}',
      'Content-Type': 'application/json'
      }
      data = {
      "displayName": "My Subscription",
      "schedule": {
      "frequency": "Daily",
      "timeZone": "Pacific Standard Time",
      "times": ["07:00"]
      },
      "status": "Enabled"
      }
      response = requests.post(url, headers=headers, json=data)
      if response.status_code == 201:
      print("Subscription created successfully.")
      else:
      print(f"Failed to create subscription. Status Code: {response.status_code}, Response: {response.text}")

      if __name__ == "__main__":
      # Replace with your workspace and report IDs
      GROUP_ID = '########################'
      REPORT_ID = '#######################'

      token = get_access_token()
      if token:
      create_subscription(token, GROUP_ID, REPORT_ID)  



      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi, Anonymous 

        Thank you for your swift response. I appreciate the effort you have put into addressing this issue.

         

        However, I may not have explained clearly. The API I provided pertains to the report subscription API for Power BI Report Server. Based on the code you shared, I suspect you might be looking for the report subscription API for Power BI Service.

         

        You can try using Power Automate to automate the creation and sending of emails,there are still some limitations to this method, and you can choose according to your needs:

         

        1.Firstly, sign in to Power Automate and create a new flow. Choose an appropriate trigger, such as “When a new item is created” in a SharePoint list or “When an email arrives” in Outlook.

         

        2,Next, add the necessary actions to your flow. For instance, you can use the “Send an email” action to automate the email sending process. You can also include conditions to check specific criteria before sending the email.

         

        3.Finally, test your flow to ensure it works as expected, then save and activate it.

         

        For detailed guidance, please refer to the official documentation.

        Create flows for popular email scenarios in Power Automate - Power Automate | Microsoft Learn

         

        I think your idea of having an API to automate the creation of service subscriptions is fantastic.If you feel that performance needs to be improved, you can also submit an idea on

        https://ideas.fabric.microsoft.com/ and wait for users with the same needs as you to vote for you and help you realize the idea as soon as possible. Many features of our current products are designed and upgraded bed on customers’ feedback.  With requirements like this increase, the problem may well be released in the future.

         

        Thanks for your understanding.

         

        Best Regards,

        Leroy Lu

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