# Toggle Monitoring State of a Host using App Function

**URL:** <https://community.dynatrace.com/t/toggle-monitoring-state-of-a-host-using-app-function/52599>\
**Category:** Dynatrace API\
**Tags:** dynatrace-api\
**Created:** [July 22, 2025, 7:45pm UTC](https://community.dynatrace.com/t/toggle-monitoring-state-of-a-host-using-app-function/52599 "2025-07-22T19:45:09Z")\
**Posts on this page:** 5\
**Page:** 1

<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 22, 2025, 7:45pm UTC](https://community.dynatrace.com/t/toggle-monitoring-state-of-a-host-using-app-function/52599/1 "2025-07-22T19:45:09Z")

</div>

I have written this AppFunction to toggle the monitoring state of a host, and I have given it all the required

scope, but I am still getting this error below. Any idea? I am planning to develop this further to be able to use in a workflow.

Any feedback is much appreciated.

------------------------

Given this permission:

-----------------------

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:run

The Function:

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 get this Error:----------------

```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>)"}<br><br><br>

```

---

<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, 11:35am UTC](https://community.dynatrace.com/t/toggle-monitoring-state-of-a-host-using-app-function/52599/2 "2025-07-23T11:35:45Z")

</div>

I also get the same error when running from a workflow

Message: HOST-F6DC785465402DD8 - Failed to update monitoring state.  
Debug info:  
Error name: 403  
Error message: OAuth token is missing required scope. Use one of: [settings:objects:write]  
Stack trace: 403: OAuth token is missing required scope. Use one of: [settings:objects:write]  
at SettingsObjectsClient.postSettingsObjects (file:///opt/sdk\_modules/@dynatrace-sdk/client-classic-environment-v2/esm/index.js:23556:19)  
at eventLoopTick (ext:core/01\_core.js:177:7)  
at async default (file:///script.ts:8:22)

---

<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 24, 2025, 10:21am UTC](https://community.dynatrace.com/t/toggle-monitoring-state-of-a-host-using-app-function/52599/3 "2025-07-24T10:21:38Z")

</div>

I gift this function to the community, as it is fully working; Enjoy!

import { execution } from ‘@dynatrace-sdk/automation-utils’;  
import { settingsObjectsClient } from ‘@dynatrace-sdk/client-classic-environment-v2’; // Make sure this import is present

export default async function () {

// We need to obtain the list of Hosts and iterate through it  
// The list can be an API call or an array in this function.

const hostId = “HOST-F6DC785465402DD8”; // Replace with actual host ID

try {  
const response = await settingsObjectsClient.postSettingsObjects({  
body: [  
{  
schemaId: “builtin:host.monitoring”,  
scope: hostId,  
value: {  
enabled: true,  
},  
},  
],  
});

const ex = await execution();  
const message = `${hostId} - Monitoring state updated successfully.`;

console.log(‘Message:’, message);  
console.log(‘Automated script execution on behalf of’, ex.trigger);

return { triggeredBy: ex.trigger };

} catch (error: unknown) {  
let errorDetails = “”;

if (error instanceof Error) {  
errorDetails = `Error name: ${error.name}\n` +  
`Error message: ${error.message}\n` +  
(error.stack ? `Stack trace: ${error.stack}` : “No stack trace available.”);  
} else {  
errorDetails = `Unknown error object: ${JSON.stringify(error, null, 2)}`;  
}

const ex = await execution();  
const message = `${hostId} - Failed to update monitoring state.\nDebug info:\n${errorDetails}`;

console.log(‘Message:’, message);

return { triggeredBy: ex.trigger };  
}  
}

---

<div class="post-metadata">

**Author:** ![p\_devulapalli](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/p_devulapalli/32/41590_2.png) [@p\_devulapalli](https://community.dynatrace.com/u/p_devulapalli)\
**Post date:** [July 24, 2025, 10:29am UTC](https://community.dynatrace.com/t/toggle-monitoring-state-of-a-host-using-app-function/52599/4 "2025-07-24T10:29:56Z")

</div>

[@MarwanC](https://community.dynatrace.com/user/64346)&nbsp;So, what was reason it was throwing the error before? Just interested to know and thanks for sharing the working code&nbsp;👍

---

<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 24, 2025, 11:27am UTC](https://community.dynatrace.com/t/toggle-monitoring-state-of-a-host-using-app-function/52599/5 "2025-07-24T11:27:46Z")

</div>

added settings:objects:write permission. Enjoy
