Adding a mailbox filter to Gmail

Viewed 44

I am trying to add a simple mailbox filter to Gmail, and getting error 400. The response text says that "Filter doesn't have any criteria" but of course I believe I do. Here is the payload data:

{
"filter": [{
"id": "ABC12345-2",
"criteria": {
"from": "donald trump"
},
"action": {
"addLabelIds":  ["TRASH"]
}
}]
}

This is the URL that I am posting to:

https://gmail.googleapis.com/gmail/v1/users/{userId}/settings/filters

There is no problem with authentication. I have tried it with and without the "id" field. Any ideas about what I am doing wrong?

1 Answers

I'm not sure about your actual script. So I'm not sure about the requirement of {"filter": []}. But when I saw the official document, it seems that the sample request body for the endpoint of POST https://gmail.googleapis.com/gmail/v1/users/{userId}/settings/filters is as follows, when your request body is used.

Sample request body:

{
  "id": "###",
  "criteria": {"from": "###"},
  "action": {"addLabelIds": ["TRASH"]}
}
  • id: string, The server assigned ID of the filter.
  • criteria: object (Criteria), Matching criteria for the filter.
  • action: object (Action), Action that the filter performs.
  • In this case, even when id is not used, no error occurs.
  • You can also test above at "Try this API".

References:

Related