Forum Discussion

ravikiranmn's avatar
2 years ago

pull csv report via rest api

how to generate csv report and download using rest api
  • Anonymous's avatar
    Anonymous
    2 years ago

    You can use GET /report/links/{reportid} to get the list of links to the data. (warning, this endpoint is technically unsupported because LM decided it should be undocumented)

    You’re supposed to be able to POST to /report/reports/{reportid}/executions to get it to generate the report immediately. The response should have the result url in it. However, in my testing the report url was always null because the report was not done generating by the time the API sent the response. Also, with this request, you have to pass in an email address that you want to send the report to. Once the task is finished, even if you get a null value in the API response, you will get the email with the link to the report.

    So you should be able to

    1. POST to /report/reports/{reportid}/executions with a bogus email (or your own) to start generating the report.
    2. You probably want to wait a few seconds (maybe 30, maybe longer depending on the size of the report).
    3. Then you should be able to GET at /report/links/{reportid} and get the last item in the list `$.items[-1:].reportUrl`. Then it would be a get at that URL. Be careful, it looks like there are extra spaces around the reportUrl, so use a .trim() method to clean it up before trying to fetch the CSV data itself.

    Before you start, you could do the GET on /report/links/{reportid} first to get the id of the last report data. That would let you do repeated GETs at /report/links/{reportid} after doing your post and check if the new data is present by looking for a new ID after the known ID. That would let you wait a minimal amount of time before fetching the data again.