Search code examples
emailgmailgmail-apifraud-prevention

Get User Set Filters using Gmail API?


I have an application that rewards users for placing orders with certain merchants and we are planning on connecting to our user's inbox (with their consent of course!) to verify and confirm that an order was placed on certain merchant sites. The idea is that if the order is placed and then cancelled, we'd be able to detect the order cancellation email sent by the merchant and then revoke any rewards linked to that order.

One potential flaw with this approach is that it is possible for a user to set filters that automatically reject and delete incoming emails that match those filters. A user can then set filters on his account to automatically reject and delete any cancellation emails sent by the merchant.

Is it possible to obtain whether a user has any filters set (and what those filters are) using Gmail API?


Solution

  • How to get the logged in user's GMail filters:

    Use the users.settings.filters list endpoint.

    Here is an example response from that endpoint.

    {
      "filter": [
        {
          "id": "0000000",
          "criteria": {
            "from": "YOUREMAIL",
            "to": "me",
            "subject": "ORDER",
            "query": "ORDER"
          },
          "action": {
            "addLabelIds": [
              "Label_94938493840"
            ],
            "removeLabelIds": [
              "INBOX",
              "SPAM"
            ]
          }
        },
        ...
        ...
      ]
    }
    
    

    Experiment by going to the URL I posted and make a request with me as the userId to see your own filters.