Forum Discussion

jonnokc098's avatar
jonnokc098
New Member
8 years ago
Solved

Dynamic Nested API Calls

Hello, 

 

I am wondering if it is possible to dynamically pull in data from a web API. 

 

Because of how the API is structure I am not able to pull in the data I need in one call. 

 

The top level of the API URL is structure like "http://url/api/rawData"

 

The top level is important because it includes the output of different versions available. The real data I need to access is located at the following URL level... "http://url/api/rawData/[LocationID]/[VersionID]/Data"

 

I know the location ID, but the VersionID is different and new versions are added all the time. 

 

Is there a way I can use the top level URL to get all the different LocationIDs and VersionIDs from within and then somehow loop through either all of them or say the 5 most recent (highest version ID numbers) to get the data from them? Or is it possible to somehow create a report that would allow the user to set which version(s) they are interested in and then initiate the call(s)?

 

10 Replies

  • ImkeF's avatar
    ImkeF
    Community Champion

    Yes, that's all possible.

     

    You transform the record or whatever is returned from the first call to a table, sort and filter to your desired selections and then add a column to that table that makes the subsequent calls to pull the detail-data. This will return a column with the results from all calls that you can simply expand. No loops required.

     

    If your first result is a (Json) record, you might find this article helpful to get you started: http://www.thebiccountant.com/2017/08/30/how-to-open-a-complex-json-record-in-power-bi-and-power-query/

     

    Kind regards,

    Imke Feldmann

    http://www.thebiccountant.com/

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi @imke,

       

      Can you please help me understand how to make the API calls dynamically from the list obtained after the first API call.

       

      Thanks,

      Yakshana

       

       

      • ImkeF's avatar
        ImkeF
        Community Champion

        It works as I said above: Transform the list into a table and add a column, referencing the relevant items from your list (that's now your first column).

        Provided your list contains the key elements that you need for further calls.

         

        If you're struggling with that, please paste your code and pic from your list.

    • ppraveenk's avatar
      ppraveenk
      Frequent Visitor

      Hello ImkeF ,


      Firstly thanks for your reponse on this thread.
      I am aware this is an old discussion, but hoping that you can guide me in right direction. 

       

      We are building a Power BI connector to extract data from one of our systems that expose data via Rest API.

      API structure as follows:

      baseURI: https://api.myapp.com/api/rest/

      API end point for document list: https://api.myapp.com/api/rest/documents/ => (returns a list of docIDs)

      API end point for document users: https://api.myapp.com/api/rest/documents/docID/users

      After getting response from List API, I am try to add a new column by invoking a custom function that accepts docID as parameter and then invoke document users API. 

       

      I get the following error: Expression.Error: Access to the resource is forbidden.

       

      Tried changing the Privacy levels at Data source settings but no luck!

      It works fine when I try to hard code any docID instead of passing as a parameter. Kindly find my code below and request you to help. Thanks!

      Query1:
      let
      DefaultRequestHeaders = [
      #"Accept" = "application/json", // column name and values only
      #"Authorization" = "****",
      #"client_id" = "****",
      #"client_secret" = "****",
      ],

      source = Web.Contents("https://api.myapp.com/api/rest/", [ RelativePath = "documents", Headers = DefaultRequestHeaders ]),
      json = Json.Document(source),
      docList = json[docList],
      #"Converted to Table" = Table.FromList(docList, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
      #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"id"}, {"id"}),
      #"Invoked Custom Function" = Table.AddColumn(#"Expanded Column1", "Query2", each Query2([id]))

      in
      #"Invoked Custom Function"

      Query2:
      let
      GetUsers = (docId as text) as table =>
      let
      DefaultRequestHeaders = [
      #"Accept" = "application/json", // column name and values only
      #"Authorization" = "****",
      #"client_id" = "****",
      #"client_secret" = "****",
      ],
      source = Web.Contents("https://api.myapp.com/api/rest/", [ RelativePath = "documents/"&docId&"/users", Headers = DefaultRequestHeaders ]),
      json = Json.Document(source),
      #"Converted to Table" = Record.ToTable(json),
      #"Pivoted Column" = Table.Pivot(#"Converted to Table", List.Distinct(#"Converted to Table"[Name]), "Name", "Value")
      in
      #"Pivoted Column"
      in
      GetUsers