# API execution through JS

**URL:** <https://community.dynatrace.com/t/api-execution-through-js/51565>\
**Category:** Automations\
**Tags:** workflows, javascript\
**Created:** [May 9, 2025, 5:32pm UTC](https://community.dynatrace.com/t/api-execution-through-js/51565 "2025-05-09T17:32:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dyno193](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@Dyno193](https://community.dynatrace.com/u/Dyno193)\
**Post date:** [May 9, 2025, 5:32pm UTC](https://community.dynatrace.com/t/api-execution-through-js/51565/1 "2025-05-09T17:32:17Z")

</div>

Is it possible to execute API request right though Javascript within Workflows?

Example:

import fetch from ‘node-fetch’;

export default async function ({ executionId }) {  
const apiToken = ‘YOUR\_API\_TOKEN’; // Replace with your Dynatrace API token  
const apiUrl = ‘https://{environmentid}.live.dynatrace.com/api/config/v1/maintenanceWindows’;

const maintenanceWindow = {  
name: “Example Maintenance Window”,  
description: “Automatically created maintenance window”,  
type: “PLANNED”,  
suppression: “DETECT\_PROBLEMS\_DONT\_ALERT”,  
schedule: {  
scheduleType: “ONCE”,  
start: “2025-05-10T14:00:00Z”,  
end: “2025-05-10T16:00:00Z”,  
timeZone: “UTC”  
},  
scope: {  
entities: [“HOST-1234567890ABCDEF”] // Replace with actual entity IDs  
}  
};

const response = await fetch(apiUrl, {  
method: ‘POST’,  
headers: {  
‘Authorization’: `Api-Token ${apiToken}`,  
‘Content-Type’: ‘application/json’  
},  
body: JSON.stringify(maintenanceWindow)  
});

if (!response.ok) {  
const error = await response.json();  
console.error(‘Failed to create maintenance window:’, error);  
return {  
status: “error”,  
message: “Failed to create maintenance window”  
};  
}

const data = await response.json();  
console.log(‘Successfully created maintenance window:’, data);  
return {  
status: “completed”,  
message: “Successfully created maintenance window”,  
data: data  
};  
}

---

<div class="post-metadata">

**Author:** ![marco\_irmer](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/marco_irmer/32/3038_2.png) [@marco\_irmer](https://community.dynatrace.com/u/marco_irmer)\
**Post date:** [May 9, 2025, 10:28pm UTC](https://community.dynatrace.com/t/api-execution-through-js/51565/2 "2025-05-09T22:28:47Z")

</div>

Rather than interacting with the API via HTTP inside your workflow, you can actually make use of the Typescript SDKs to interact with things like settings more directly. In your case, I would think that the [settingsObjectsClient](https://developer.dynatrace.com/develop/sdks/client-classic-environment-v2/#postsettingsobjects) should do the trick.

Here’s a code sample from the documentation:

```javascript
import { settingsObjectsClient } from "@dynatrace-sdk/client-classic-environment-v2";

const data =
  await settingsObjectsClient.postSettingsObjects({
    body: [
      {
        schemaId:
          "builtin:container.built-in-monitoring-rule",
        scope: "HOST-D3A3C5A146830A79",
        value: {},
      },
    ],
  });

```

I hope this helps.

---

<div class="post-metadata">

**Author:** ![MarwanC](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/marwanc/32/34480_2.png) [@MarwanC](https://community.dynatrace.com/u/MarwanC)\
**Post date:** [July 23, 2025, 7:24am UTC](https://community.dynatrace.com/t/api-execution-through-js/51565/3 "2025-07-23T07:24:42Z")

</div>

Tried similar things for trying setting the host mode via an AppFunction&nbsp; to test it before I add it to my workflow, but is not working despite I am giving it the right scope and more:

import { settingsObjectsClient } from “@dynatrace-sdk/client-classic-environment-v2”;  
export default async function () : Promise\<{ statusCode: number; message: string }\>{&nbsp; const hostId = “HOST-F6DC785465402DD8”; // Replace with your actual host ID&nbsp; try {&nbsp; &nbsp; const response = await settingsObjectsClient.postSettingsObjects({&nbsp; &nbsp; &nbsp; body: [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; schemaId: “builtin:host.monitoring”,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scope: hostId,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; monitoringEnabled: false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp;],&nbsp; &nbsp; });&nbsp; &nbsp; return { statusCode: 200, message: “{hostId} - Monitoring state updated successfully.” };  
&nbsp; } catch (error: unknown) {&nbsp; &nbsp; let errorDetails = “”;&nbsp; &nbsp; if (error instanceof Error) {&nbsp; &nbsp; &nbsp; errorDetails = `Error name: ${error.name}\n` +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;`Error message: ${error.message}\n` +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(error.stack ? `Stack trace: ${error.stack}` : “No stack trace available.”);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; errorDetails = `Unknown error object: ${JSON.stringify(error, null, 2)}`;&nbsp; &nbsp; }&nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; statusCode: 500,&nbsp; &nbsp; &nbsp; message: `${hostId} - Failed to update monitoring state.\nDebug info:\n${errorDetails}`,&nbsp; &nbsp; };&nbsp; }}I am using platform token which has even more than it needs:Scopes:&nbsp;app-settings:objects:read, app-settings:objects:write, settings:objects:read, settings:objects:write, settings:schemas:read, app-engine:functions:run, app-engine:apps:runBut I get this error that this SDK call does not have the settings:objects:write

```auto
{"statusCode":500,"message":"HOST-F6DC785465402DD8 - Failed to update monitoring state.\nDebug info:\nError name: 403\nError message: OAuth token is missing required scope. Use one of: [settings:objects:write]\nStack trace: 403: OAuth token is missing required scope. Use one of: [settings:objects:write]\n at se.postSettingsObjects (<a target="_blank" rel="noopener">file:///invoker.js:5:19821)\n</a> at eventLoopTick (ext:core/01_core.js:177:7)\n at async Ds (<a target="_blank" rel="noopener">file:///invoker.js:5:47784</a>)"}

```

Any Idea what I am doing wrong here?

---

<div class="post-metadata">

**Author:** ![MarwanC](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/marwanc/32/34480_2.png) [@MarwanC](https://community.dynatrace.com/u/MarwanC)\
**Post date:** [September 8, 2025, 11:39am UTC](https://community.dynatrace.com/t/api-execution-through-js/51565/4 "2025-09-08T11:39:25Z")

</div>

This is now resolved I share the results here for others: basically the right schema is now used&nbsp;builtin:host.monitoring and it seems to work.

for (const value of values)  
{  
const id = value;  
hostId = id;

// The API call that enable or disable the state  
const response = await settingsObjectsClient.postSettingsObjects({  
body: [  
{  
schemaId: “builtin:host.monitoring”,  
scope: hostId,  
value: {  
enabled: true,  
},  
},  
],  
});  
console.log(`Processing Host ${count + 1}: ${id}`);  
count++;  
}
