This page explains how to call the Gmail API's
users.messages.list
method.
The method returns an array of Gmail Message
resources that
contain the message id
and threadId
. To retrieve full message details, use
the
users.messages.get
method.
Prerequisites
Python
A Google Cloud project with the Gmail API enabled. For steps, complete the Gmail API Python quickstart.
List messages
The users.messages.list
method supports several query parameters to filter the
messages:
maxResults
: Maximum number of messages to return (defaults to 100, max 500).pageToken
: Token to retrieve a specific page of results.q
: Query string to filter messages, such asfrom:someuser@example.com is:unread"
.labelIds
: Only return messages with labels that match all specified label IDs.includeSpamTrash
: Include messages fromSPAM
andTRASH
in the results.
Code sample
Python
The following code sample shows how to list messages for the authenticated Gmail user. The code handles pagination to retrieve all messages matching the query.
The users.messages.list
method returns a response body that contains the
following:
messages[]
: An array ofMessage
resources.nextPageToken
: For requests with multiple pages of results, a token that can be used with a subsequent calls to list more messages.resultSizeEstimate
: An estimated total number of results.
To fetch the full message content and metadata, use the message.id
field to
call the
users.messages.get
method.