# Error 400 via API post with python to add tag to entity.  "Constraints violated."

**URL:** <https://community.dynatrace.com/t/error-400-via-api-post-with-python-to-add-tag-to-entity-constraints-violated/50396>\
**Category:** Open Q&A\
**Tags:** python, dynatrace-api\
**Created:** [February 21, 2025, 7:11pm UTC](https://community.dynatrace.com/t/error-400-via-api-post-with-python-to-add-tag-to-entity-constraints-violated/50396 "2025-02-21T19:11:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tree\_branch](https://avatars.discourse-cdn.com/v4/letter/t/e274bd/32.png) [@tree\_branch](https://community.dynatrace.com/u/tree_branch)\
**Post date:** [February 21, 2025, 7:11pm UTC](https://community.dynatrace.com/t/error-400-via-api-post-with-python-to-add-tag-to-entity-constraints-violated/50396/1 "2025-02-21T19:11:12Z")

</div>

Hi,

When trying to add a tag via the API with python, I’m receiving this error:

{“error”:{“code”:400,“message”:“Constraints violated.”,“constraintViolations”:[{“path”:“tags”,“message”:“must not be null”,“parameterLocation”:“PAYLOAD\_BODY”,“location”:null}]}}

This is the code I’m using (note url\_fstring = the request url with the entity id)

tag\_body\_data = {“context”: “CONTEXTLESS”,“key”: “Tag\_to\_be\_added”}  
tag\_body\_response = requests.post(url\_fstring, headers={“Content-Type”: “application/json”,‘Authorization’: "Api-Token " + api\_token}, data=json.dumps(tag\_body\_data), verify=False)

How do I make sense of that error response, or does anyone have a python code sample of assigning a simple tag to a entityID?

Thanks!

---

<div class="post-metadata">

**Author:** ![Julius\_Loman](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/julius_loman/32/51_2.png) [@Julius\_Loman](https://community.dynatrace.com/u/Julius_Loman)\
**Post date:** [February 21, 2025, 8:52pm UTC](https://community.dynatrace.com/t/error-400-via-api-post-with-python-to-add-tag-to-entity-constraints-violated/50396/2 "2025-02-21T20:52:04Z")

</div>

Your payload is not correct. It expects the tags structure which is missing in your payload. Please follow the API docs&nbsp;[https://docs.dynatrace.com/docs/discover-dynatrace/references/dynatrace-api/environment-api/custom-tags/post-tags](https://docs.dynatrace.com/docs/discover-dynatrace/references/dynatrace-api/environment-api/custom-tags/post-tags) .

---

<div class="post-metadata">

**Author:** ![tree\_branch](https://avatars.discourse-cdn.com/v4/letter/t/e274bd/32.png) [@tree\_branch](https://community.dynatrace.com/u/tree_branch)\
**Post date:** [February 24, 2025, 4:23pm UTC](https://community.dynatrace.com/t/error-400-via-api-post-with-python-to-add-tag-to-entity-constraints-violated/50396/3 "2025-02-24T16:23:16Z")

</div>

After changing the tag\_body\_data variable to below, it worked.&nbsp; Thanks!

tag\_body\_data = {  
&nbsp;&nbsp;&nbsp; “tags”: [  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; “key”: “Tag\_to\_be\_added”  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; },

]  
}
