Search code examples
google-apigmailgmail-api

Fetching email ID's of only the emails in the main Gmail Inbox


I'm using the Gmail API to fetch the email ID's like so:

https://www.googleapis.com/gmail/v1/users/{myEmailID}/messages

Once I have the email ID's, I then fetch the full emails like so:

https://www.googleapis.com/gmail/v1/users/me/messages/\(id)?format=full

However, what I'm noticing is that this will not only fetch the emails in my inbox, but also ones that I've archived. How do I configure this to only fetch the the emails that are in the current inbox?


Solution

  • When fetching all the emails with users.messages.list you can filter them so you will only get the emails that are on the INBOX.

    Using the parameter labelIds:

    enter image description here

    So a call would be like:

    https://gmail.googleapis.com/gmail/v1/users/me/messages?labelIds=INBOX&key=[YOUR_API_KEY]

    You can test the filters on the Try this API section of the API method