# Creating maintenance window using setting API

**URL:** <https://community.dynatrace.com/t/creating-maintenance-window-using-setting-api/39897>\
**Category:** Alerting\
**Tags:** problem-notification, anomaly-detection, maintenance-window, problems-classic\
**Created:** [May 24, 2023, 3:41pm UTC](https://community.dynatrace.com/t/creating-maintenance-window-using-setting-api/39897 "2023-05-24T15:41:22Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Naveen](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/naveen/32/2570_2.png) [@Naveen](https://community.dynatrace.com/u/Naveen)\
**Post date:** [May 24, 2023, 3:41pm UTC](https://community.dynatrace.com/t/creating-maintenance-window-using-setting-api/39897/1 "2023-05-24T15:41:22Z")

</div>

Hi,

I am trying to create a maintenance window using settings API, but getting an error, could anyone please help?

**Body:**

```python
[
  {
    "schemaId": "builtin:alerting.maintenance-window",
    "displayName": "Maintenance windows",
    "description": "Maintenance windows are typically planned, recurring periods of system downtime during which your DevOps team can perform preventative maintenance and system upgrades outside of peak traffic hours.",
    "scope": "environment",
    "value": {
      "enabled": true,
      "generalProperties": {
        "name": "Example Window",
        "description": "An example Maintenance window",
        "maintenanceType": "UNPLANNED",
        "suppression": "DONT_DETECT_PROBLEMS",
        "disableSyntheticMonitorExecution": true
      },
      "schedule": {
        "scheduleType": "ONCE",
        "OnceRecurrence": {
            "startTime": "13:45:00",
            "endTime": "14:45:00",
            "timeZone": "Europe/Vienna"
        }
      }
    }
  }
]

```

**Error:**

```javascript
"code": 400,
"error": {
    "code": 400,
    "message": "Validation failed for 2 Validators.",
    "constraintViolations": [
        {
            "path": "builtin:alerting.maintenance-window/0/schedule/onceRecurrence",
            "message": "Given property 'onceRecurrence' with value: 'null' does not comply with required NonNull of schema",
            "parameterLocation": "PAYLOAD_BODY",
            "location": null
        },
        {
            "path": "builtin:alerting.maintenance-window/0/schedule/OnceRecurrence",
            "message": "Given property 'OnceRecurrence' with value: '{startTime=13:45:00, endTime=14:45:00, timeZone=Europe/Vienna}' was not found in the schema",
            "parameterLocation": "PAYLOAD_BODY",
            "location": null
        }
    ]
}

```

---

<div class="post-metadata">

**Author:** ![mgome](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@mgome](https://community.dynatrace.com/u/mgome)\
**Post date:** [May 24, 2023, 4:37pm UTC](https://community.dynatrace.com/t/creating-maintenance-window-using-setting-api/39897/2 "2023-05-24T16:37:14Z")

</div>

You can generate a sample payload by creating a maintenance window in the console and using the API option under the “…” button in the upper right corner of the maintenance window configuration.&nbsp; Open the “Add as new value” section and copy the result, which has the JSON payload as part of the curl command.

When I dumped the JSON for one of my maintenance windows, the onceRecurrence looked like this:

“onceRecurrence”: {  
“startTime”: “2020-09-26T22:50:00”,  
“endTime”: “2020-09-27T00:00:00”,  
“timeZone”: “America/Los\_Angeles”  
}

You’re missing the date in your start and end times.

 ![mgome_0-1684946866119.png](https://global.discourse-cdn.com/dynatrace/original/2X/d/dd238b339c2c2e00af95dcc8c8eb7db61bcaf8dd.png)

The local\_date\_time is of type&nbsp;java.time.LocalDateTime

> **[LocalDateTime (Java Platform SE 8 )](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html)**

---

<div class="post-metadata">

**Author:** ![ChadTurner](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/chadturner/32/161_2.png) [@ChadTurner](https://community.dynatrace.com/u/ChadTurner)\
**Post date:** [May 24, 2023, 5:22pm UTC](https://community.dynatrace.com/t/creating-maintenance-window-using-setting-api/39897/3 "2023-05-24T17:22:44Z")

</div>

I highly recommend making one via the UI, then do an API pull to grab the json for that Maintenance Window. Then once you do that you can break it down and templatize the MW. That’s how we did it and inserted that Json into the CICD Pipeline.

---

<div class="post-metadata">

**Author:** ![Naveen](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/naveen/32/2570_2.png) [@Naveen](https://community.dynatrace.com/u/Naveen)\
**Post date:** [May 25, 2023, 1:40pm UTC](https://community.dynatrace.com/t/creating-maintenance-window-using-setting-api/39897/4 "2023-05-25T13:40:32Z")

</div>

Thanks&nbsp;[@ChadTurner](https://community.dynatrace.com/user/14877)&nbsp;and&nbsp;[@mgome](https://community.dynatrace.com/user/8132)&nbsp;I have tried the method suggested by you but getting below error in the request  
I am using powershell to get time and invoking API.

```python
Invoke-RestMethod : [{"code":400,"error":{"code":400,"message":"Validation failed for 6 Validators.","constraintViolations":[{"
path":"builtin:alerting.maintenance-window/0/schedule/onceRecurrence/startTime","message":"Given property 'startTime' with 
value: '2023-05-25 11.35.09' violates the following constraint: Must be of type local_date_time.","parameterLocation":"PAYLOAD_
BODY","location":null},{"path":"builtin:alerting.maintenance-window/0/schedule/onceRecurrence/endTime","message":"Given 
property 'endTime' with value: '2023-05-25 12.35.09' violates the following constraint: Must be of type local_date_time.","para
meterLocation":"PAYLOAD_BODY","location":null},{"path":"builtin:alerting.maintenance-window/0/generalProperties/disableSyntheti
cMonitorExecution","message":"Given property 'disableSyntheticMonitorExecution' with value: 'true' violates the following 
constraint: Must be of type boolean.","parameterLocation":"PAYLOAD_BODY","location":null},{"path":"builtin:alerting.maintenance
-window/0/filters/0/entityId","message":"Given property 'entityId' with value: 'null' violates the following constraint: 
Please use a format of TYPE-ID.","parameterLocation":"PAYLOAD_BODY","location":null},{"path":"builtin:alerting.maintenance-wind
ow/0/filters/0/entityId","message":"Given property 'entityId' with value: 'null' violates the following constraint: Invalid 
entity id..","parameterLocation":"PAYLOAD_BODY","location":null},{"path":"builtin:alerting.maintenance-window/0/enabled","messa

```

Could you please help, I have tried putting up different time format like&nbsp;2023-05-25T15.30.54 or&nbsp;1685009069

---

<div class="post-metadata">

**Author:** ![Naveen](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/naveen/32/2570_2.png) [@Naveen](https://community.dynatrace.com/u/Naveen)\
**Post date:** [May 25, 2023, 3:28pm UTC](https://community.dynatrace.com/t/creating-maintenance-window-using-setting-api/39897/5 "2023-05-25T15:28:23Z")

</div>

Thanks&nbsp;[@ChadTurner](https://community.dynatrace.com/user/14877)&nbsp;and&nbsp;[@mgome](https://community.dynatrace.com/user/8132)&nbsp;I have tried posting it using the powershell but unable to format the starttime and endtime, could you please let me know what time format is it.

I have tried&nbsp;([DateTimeOffset]$localTime).ToUnixTimeSeconds() or&nbsp;$localTime.ToString(“yyyy-MM-ddTHH:mm:ss”) but it’s throwing an error.

---

<div class="post-metadata">

**Author:** ![mgome](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@mgome](https://community.dynatrace.com/u/mgome)\
**Post date:** [May 25, 2023, 4:03pm UTC](https://community.dynatrace.com/t/creating-maintenance-window-using-setting-api/39897/6 "2023-05-25T16:03:17Z")

</div>

The proper key is “onceRecurrence”. You have “OnceRecurrence” with a capital “O” in your payload.

---

<div class="post-metadata">

**Author:** ![Naveen](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/naveen/32/2570_2.png) [@Naveen](https://community.dynatrace.com/u/Naveen)\
**Post date:** [May 26, 2023, 8:10am UTC](https://community.dynatrace.com/t/creating-maintenance-window-using-setting-api/39897/7 "2023-05-26T08:10:24Z")

</div>

Hey&nbsp;[@mgome](https://community.dynatrace.com/user/8132)&nbsp;still the same error

**Invoke-RestMethod : [{“code”:400,“error”:{“code”:400,“message”:"Validation failed for 6**  
**Validators.",“constraintViolations”:[{“path”:“builtin:alerting.maintenance-window/0/schedule/onceRecurrence”,“message”:"Given**  
**property ‘onceRecurrence’ with value: ‘null’ does not comply with required NonNull of schema",“parameterLocation”:"PAYLOAD\_BODY**  
**",“location”:null},{“path”:“builtin:alerting.maintenance-window/0/schedule/OnceRecurrence”,“message”:"Given property**  
**‘OnceRecurrence’ with value: ‘{endTime=2023-05-26T14.39.44, timeZone=Europe/Copenhagen, startTime=2023-05-26T13.39.44}’ was**  
**not found in the schema",“parameterLocation”:“PAYLOAD\_BODY”,“location”:null},{“path”:"builtin:alerting.maintenance-window/0/gen**  
**eralProperties/disableSyntheticMonitorExecution",“message”:"Given property ‘disableSyntheticMonitorExecution’ with value:**  
**‘true’ violates the following constraint: Must be of type boolean.",“parameterLocation”:“PAYLOAD\_BODY”,“location”:null},{“path”**  
**:“builtin:alerting.maintenance-window/0/filters/0/entityId”,“message”:"Given property ‘entityId’ with value: ‘null’ violates**  
**the following constraint: Please use a format of TYPE-ID.",“parameterLocation”:“PAYLOAD\_BODY”,“location”:null},{“path”:"builtin**  
**:alerting.maintenance-window/0/filters/0/entityId",“message”:"Given property ‘entityId’ with value: ‘null’ violates the**

---

<div class="post-metadata">

**Author:** ![ChadTurner](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/chadturner/32/161_2.png) [@ChadTurner](https://community.dynatrace.com/u/ChadTurner)\
**Post date:** [May 26, 2023, 12:02pm UTC](https://community.dynatrace.com/t/creating-maintenance-window-using-setting-api/39897/8 "2023-05-26T12:02:40Z")

</div>

[@Naveen](https://community.dynatrace.com/user/51963)&nbsp;there are several Syntax errors in your Json. please use the following to post your MZs via the API. You will need to update the start/end time tho.

```markup
[
{
  "scope": "environment",
  "schemaId": "builtin:alerting.maintenance-window",
  "schemaVersion": "2.14.2",
  "value": {
    "enabled": true,
    "generalProperties": {
      "name": "Maintenance windows",
      "description": "Maintenance windows are typically planned, recurring periods of system downtime during which your DevOps team can perform preventative maintenance and system upgrades outside of peak traffic hours.",
      "maintenanceType": "UNPLANNED",
      "suppression": "DONT_DETECT_PROBLEMS",
      "disableSyntheticMonitorExecution": true
    },
    "schedule": {
      "scheduleType": "ONCE",
      "onceRecurrence": {
        "startTime": "2023-07-25T13:45:00",
        "endTime": "2023-08-22T14:45:00",
        "timeZone": "Europe/Vienna"
      }
    },
    "filters": []
  }
}
]

```

So what were you missing?

 ![ChadTurner_0-1685102274275.png](https://global.discourse-cdn.com/dynatrace/original/3X/9/e/9e5fa9cbb1cccffc403f50101defc08e3036a34d.png)

1 - Schema Version was missing

2 - Conflicting names and descriptions

3 - No Date with your time stamp for both start and end

4 - Missing the filters segment

5 - Bracket/Braces were not correct.

Please save this as a gold template for you to leverage with posting of MWs via the API

---

<div class="post-metadata">

**Author:** ![gabrielsantos](https://avatars.discourse-cdn.com/v4/letter/g/5fc32e/32.png) [@gabrielsantos](https://community.dynatrace.com/u/gabrielsantos)\
**Post date:** [October 26, 2023, 1:22pm UTC](https://community.dynatrace.com/t/creating-maintenance-window-using-setting-api/39897/9 "2023-10-26T13:22:27Z")

</div>

Hello,

I’ve tried to use this code to create a MW from Servicenow via REST and I am getting the following error:

\*\*\* Script: {“error”:{“code”:400,“message”:“Could not map JSON at ‘’ near line 1 column 1”}}

My code is the following:

```javascript
var rm = new sn_ws.RESTMessageV2(); 
var jsonObj = {
  "scope":"environment",
  "schemaId":"builtin:alerting.maintenance-window",
  "schemaVersion":"2.14.2",
  "value":{
     "enabled":true,
     "generalProperties":{
        "name":"TEST INTEGRATION",
        "description":"This a test for maintenance window via API",
        "maintenanceType":"PLANNED",
        "suppression":"DONT_DETECT_PROBLEMS",
        "disableSyntheticMonitorExecution":true
     },
     "schedule":{
        "scheduleType":"ONCE",
        "onceRecurrence":{
           "startTime":"2023-12-29T13:45:00",
           "endTime":"2023-12-30T14:45:00",
           "timeZone":"Europe/Vienna"
        }
     },
     "filters":[

     ]
  }
};
rm.setEndpoint('https://{environmentid}.live.dynatrace.com/api/v2/settings/objects'); 
rm.setHttpMethod('POST'); 
rm.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
rm.setRequestHeader('entities.write', 'application/json;charset=UTF-8');
rm.setRequestHeader('Accept', 'application/json; charset=utf-8');
rm.setRequestHeader('Authorization', 'Api-Token dt0c01.......');
rm.setRequestBody(JSON.stringify(jsonObj));

var response = rm.execute();
var responseBody = response.getBody(); 

gs.print(responseBody);

```

I dont understand why this is not being able to parse the JSON.

Thanks in advance.

Gabriel

---

<div class="post-metadata">

**Author:** ![victor\_balbuena](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/victor_balbuena/32/9773_2.png) [@victor\_balbuena](https://community.dynatrace.com/u/victor_balbuena)\
**Post date:** [October 26, 2023, 1:25pm UTC](https://community.dynatrace.com/t/creating-maintenance-window-using-setting-api/39897/10 "2023-10-26T13:25:01Z")

</div>

You need to add square brackets at the start and end of your JSON or the API gets confused, so just add&nbsp;**[** and&nbsp;**]** at the start and end of your JSON body respectively.

---

<div class="post-metadata">

**Author:** ![gabrielsantos](https://avatars.discourse-cdn.com/v4/letter/g/5fc32e/32.png) [@gabrielsantos](https://community.dynatrace.com/u/gabrielsantos)\
**Post date:** [October 26, 2023, 1:33pm UTC](https://community.dynatrace.com/t/creating-maintenance-window-using-setting-api/39897/11 "2023-10-26T13:33:17Z")

</div>

Amazing Victor, thank you very much!

---

<div class="post-metadata">

**Author:** ![wipkumv3](https://avatars.discourse-cdn.com/v4/letter/w/ccd318/32.png) [@wipkumv3](https://community.dynatrace.com/u/wipkumv3)\
**Post date:** [January 25, 2024, 9:03am UTC](https://community.dynatrace.com/t/creating-maintenance-window-using-setting-api/39897/12 "2024-01-25T09:03:17Z")

</div>

Hi,

Is there a way to fetch the entity ID against whom we need to create a maintenance window and use it in the same script, or how does it work to schedule MW for specific entity?

---

<div class="post-metadata">

**Author:** ![prakhardt](https://avatars.discourse-cdn.com/v4/letter/p/0ea827/32.png) [@prakhardt](https://community.dynatrace.com/u/prakhardt)\
**Post date:** [January 3, 2025, 9:19am UTC](https://community.dynatrace.com/t/creating-maintenance-window-using-setting-api/39897/13 "2025-01-03T09:19:12Z")

</div>

Hi Naveen

Please tell me how we have to deploy this json script so that we can automatically apply maintenance windows,or create an automated path for maintenance window.
