# Is it possible to use ad-hoc functions to query endpoints by looping?

**URL:** <https://community.dynatrace.com/t/is-it-possible-to-use-ad-hoc-functions-to-query-endpoints-by-looping/40680>\
**Category:** Developer Q&A Forum\
**Tags:** coding-assistance, ad-hoc-functions\
**Created:** [July 13, 2023, 1:57pm UTC](https://community.dynatrace.com/t/is-it-possible-to-use-ad-hoc-functions-to-query-endpoints-by-looping/40680 "2023-07-13T13:57:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MarcioKaue](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@MarcioKaue](https://community.dynatrace.com/u/MarcioKaue)\
**Post date:** [July 13, 2023, 1:57pm UTC](https://community.dynatrace.com/t/is-it-possible-to-use-ad-hoc-functions-to-query-endpoints-by-looping/40680/1 "2023-07-13T13:57:44Z")

</div>

Hey guys,

We are testing the potential of using the new Dashboards with the use of Javascript, so you can concentrate the telemetry and governance data of my Managed Clusters in the SelfMonitoring environment.

We would like to have a Governance panel for tokens created on the Dynatrace platform and for that we perform the GET ALL in the respective API and then we need to perform a loop to obtain the details of each id returned in the vector. I would not like to use an external solution with python for this, is the ad-hoc function capable of doing this?

Our code example follows below, where an “HTTP Error 540: Unknown” error is displayed and no further details of the actual reason for the error are reported:

```javascript
export default async function () {
  const environment = "{}/endpoint"
  const token = "{token}";
  const params = '/api/v2/apiTokens?pageSize=600&sort=-creationDate';

  const uri = environment + params;
  const response = await fetch(uri, {
  headers: {
    Accept: "application/json",
    Authorization: "Api-Token " + token
  }});

  const result = await response.json();
  const apiTokens = result.apiTokens;

  const secondAPIresults = [];

  await Promise.all(apiTokens.map(async (token) => {
    const uri2 = environment + '/api/v2/apiTokens/${token.id}';
    const response2 = await fetch(uri2, {
      headers: {
        Accept: "application/json",
        Authorization: "Api-Token " + token
      }});

    const data = await response2.json();
    const { name, lastUsedDate } = data;
    secondAPIResults.push({ name, lastUsedDate });
  }));

  return secondAPIResults;
}

```

---

<div class="post-metadata">

**Author:** ![educampver](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/educampver/32/29917_2.png) [@educampver](https://community.dynatrace.com/u/educampver)\
**Post date:** [July 13, 2023, 2:24pm UTC](https://community.dynatrace.com/t/is-it-possible-to-use-ad-hoc-functions-to-query-endpoints-by-looping/40680/2 "2023-07-13T14:24:51Z")

</div>

Hi&nbsp;[@MarcioKaue](https://community.dynatrace.com/user/55571), the error 540 means there’s an issue with the code. At the moment, you can debug these errors by catching them in your ad-hoc function and returning the error message as part of the response. Here’s an example&nbsp;[Handle errors in app functions | Dynatrace Developer](https://developer.dynatrace.com/develop/functions/error-handling/#custom-error-reporting).

At first glance, it looks like a typo. You’re declaring a variable called ` secondAPIresults`with lowercase `r`, but then using ` secondAPIResults` with uppercase `R`.

---

<div class="post-metadata">

**Author:** ![MarcioKaue](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@MarcioKaue](https://community.dynatrace.com/u/MarcioKaue)\
**Post date:** [July 13, 2023, 3:09pm UTC](https://community.dynatrace.com/t/is-it-possible-to-use-ad-hoc-functions-to-query-endpoints-by-looping/40680/3 "2023-07-13T15:09:05Z")

</div>

Hello [@educampver](https://community.dynatrace.com/user/47555)&nbsp;, the variable actually had this incorrect character, but the code does not work with the suggested adjustment.

Thank you for coming back with the documentation, we will evaluate it in order to have a more detailed troubleshooting of the reason for the error.
