# DQL query syntax while using in API calls

**URL:** <https://community.dynatrace.com/t/dql-query-syntax-while-using-in-api-calls/54663>\
**Category:** DQL\
**Tags:** dql\
**Created:** [January 17, 2026, 12:41pm UTC](https://community.dynatrace.com/t/dql-query-syntax-while-using-in-api-calls/54663 "2026-01-17T12:41:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![harinhs](https://avatars.discourse-cdn.com/v4/letter/h/c4cdca/32.png) [@harinhs](https://community.dynatrace.com/u/harinhs)\
**Post date:** [January 17, 2026, 12:41pm UTC](https://community.dynatrace.com/t/dql-query-syntax-while-using-in-api-calls/54663/1 "2026-01-17T12:41:05Z")

</div>

I am trying to use this query to fetch remote environment data, but not working for remote environment through API call. But this works&nbsp;in local environment to get the host availability,

fetch dt.entity.host  
| filterOut isMonitoringCandidate == true or standalone == true  
| lookup [  
timeseries m1=max(dt.host.availability, rollup: sum),  
filter: availability.state == “up”,  
by: {availability.state,dt.entity.host},  
from: toTimestamp($dt\_timeframe\_from),  
to: toTimestamp($dt\_timeframe\_to) - 2m  
| parse replaceString(toString(m1),“null”,“0”), “”“'['ARRAY{ DOUBLE:i ', '}{1,10000}:m1Pop”“”  
| fieldsAdd intervalToMinutes = toLong(interval) / 60 / power(10, 9)  
| fieldsAdd availability = toDouble(arraySum(m1Pop) / arraySize(m1Pop) \* 100) / intervalToMinutes  
], sourceField: id, lookupField: dt.entity.host, fields: {availability}  
| fields id, availability = coalesce(availability, 0)  
| summarize avg(availability)

How to parse this query in the API call section, I tried giving this like other queries but getting 400 Bad request error.

const credentialId = “CREDENTIALS\_VAULT-XXXXXXXX”; // Replace with your credential vault ID.  
const url = “https://{environmentid}.apps.dynatrace.com/platform/storage/query/v1/query:execute”; // Replace with API URL.  
const query = "\<how to give the above DQL query as string here\> "; // Replace with your query.

Could you please help? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![t\_pawlak](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/t_pawlak/32/14991_2.png) [@t\_pawlak](https://community.dynatrace.com/u/t_pawlak)\
**Post date:** [January 18, 2026, 7:49pm UTC](https://community.dynatrace.com/t/dql-query-syntax-while-using-in-api-calls/54663/2 "2026-01-18T19:49:05Z")

</div>

Hi,  
try this:

```markup
fetch dt.entity.host
| filterOut isMonitoringCandidate == true or standalone == true
| lookup [
    timeseries m1 = max(dt.host.availability, rollup: sum),
      filter: availability.state == "up",
      by: { availability.state, dt.entity.host },
      from: now() - 24h,
      to: now() - 2m
    | parse replaceString(toString(m1), "null", "0"), "'['ARRAY{ DOUBLE:i ', '}{1,10000}:m1Pop"
    | fieldsAdd intervalToMinutes = toLong(interval) / 60 / power(10, 9)
    | fieldsAdd availability = toDouble(arraySum(m1Pop) / arraySize(m1Pop) * 100) / intervalToMinutes
  ],
  sourceField: id,
  lookupField: dt.entity.host,
  fields: { availability }
| fields id, availability = coalesce(availability, 0)
| summarize avg(availability)

```

The query works locally because $dt\_timeframe\_from and $dt\_timeframe\_to are dashboard-only variables. They are not resolved when executing DQL via the Query API, which results in a 400 Bad Request.

Remove $dt\_timeframe\_from / $dt\_timeframe\_to from the DQL

Use now() - … in the query or pass the timeframe via the query:execute request body

Replace the parse “”" … “”" with a normal quoted string (“…”) to avoid JSON parsing issues

After these changes, the query executes correctly via the API.

 ![t_pawlak_0-1768765729511.png](https://global.discourse-cdn.com/dynatrace/original/2X/6/6de00503cdbd1d4caeaf037b85eec00306acdda3.png)
