09 May 2025 06:32 PM - edited 09 May 2025 07:54 PM
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
};
}
Solved! Go to Solution.
09 May 2025 11:28 PM
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 should do the trick.
Here's a code sample from the documentation:
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.
23 Jul 2025 08:24 AM
Tried similar things for trying setting the host mode via an AppFunction to test it before I add it to my workflow, but is not working despite I am giving it the right scope and more:
{"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 (file:///invoker.js:5:19821)\n at eventLoopTick (ext:core/01_core.js:177:7)\n at async Ds (file:///invoker.js:5:47784)"}
08 Sep 2025 12:39 PM
This is now resolved I share the results here for others: basically the right schema is now used 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++;
}