POST /functions/v1/read-gmail
Reads emails from a Gmail account using OAuth2 credentials. Searches for messages from a specific sender and optionally filters for unread messages.Request
Headers
Bearer token for authentication:
Bearer <your-supabase-jwt-token>Must be
application/jsonBody Parameters
The workflow element ID used to load Gmail OAuth credentials from the database.
The sender email address to filter messages by. Only messages from this sender will be returned.
Maximum number of messages to return. Defaults to 10.
If
true, only returns unread messages. If false, returns all messages from the sender. Defaults to true.Response
Array of email message objects
Gmail message ID
Gmail thread ID
Array of label IDs applied to the message (e.g.,
["UNREAD", "INBOX"])Short preview of the message content
Sender email address and name
Recipient email address
Email subject line
Date and time the email was sent
Full text body of the email (plain text version)
Gmail OAuth Flow
This endpoint uses OAuth2 credentials stored in theuser_gmail_credentials table:
- Retrieves Encrypted Credentials: Client ID, Client Secret, and Refresh Token
- Decrypts Credentials: Using XOR encryption with
ENCRYPTION_KEY - Obtains Access Token: Exchanges refresh token for access token with Google OAuth
- Searches Messages: Uses Gmail API to search for messages matching criteria
- Fetches Message Details: Retrieves full message content including headers and body
Gmail API Endpoints Used
- Token refresh:
https://oauth2.googleapis.com/token - Message search:
https://gmail.googleapis.com/gmail/v1/users/me/messages - Message details:
https://gmail.googleapis.com/gmail/v1/users/me/messages/{id}
Search Query
The function builds a Gmail search query:onlyUnread is false:
Examples
Request
Success Response
Empty Response
Error Response
Error Codes
| Status Code | Error Message | Description |
|---|---|---|
| 400 | Element ID is required | Missing elementId parameter |
| 400 | Sender email address is required | Missing fromEmail parameter |
| 400 | Invalid Gmail credentials | Credentials are incomplete or corrupted |
| 404 | No Gmail credentials found for this user | User hasn’t connected Gmail |
| 500 | Failed to retrieve Gmail credentials | Database error |
| 500 | Failed to read emails | Gmail API error or network issue |
Message Body Extraction
The function handles different email formats:- Multipart messages: Extracts the
text/plainpart - Simple messages: Extracts body directly from payload
- Base64 decoding: Decodes Gmail’s URL-safe base64 encoding
OAuth Token Refresh
Access tokens are obtained fresh on each request by exchanging the refresh token:Notes
- Credentials are retrieved from
user_gmail_credentialstable (centralized storage) - Agent-specific configuration in
agent_configsis optional - Messages are returned in the order provided by Gmail API
- Maximum of
maxResultsmessages will be processed - Only
text/plainmessage parts are extracted; HTML parts are ignored - Labels like
UNREAD,INBOX,SENTare included in response