Can n8n connect to gmail?
Yes, n8n can connect to Gmail using the Gmail node and Google OAuth2. This tutorial shows a clear Docker-based setup, how to create OAuth credentials in Google Cloud, and how to run n8n so the Gmail node can authenticate and send or read messages.
What You Need
- Docker and docker-compose installed on your machine.
- A Google Cloud project with OAuth 2.0 credentials.
- An HTTPS endpoint for n8n (ngrok is fine for testing).
- Basic familiarity with environment variables and the command line.
Setup: can n8n connect to gmail with OAuth2
This section lists the Google Cloud and n8n steps. Keep sentences short. Follow the numbered steps.
- 1. Create a Google Cloud project and enable the Gmail API.
- 2. Create OAuth 2.0 Client ID credentials (type: Web application).
- 3. Add the n8n OAuth redirect URI: for example,
https://YOUR_DOMAIN/rest/oauth2-credential/callback. - 4. Save the Client ID and Client Secret for n8n credentials.
How to Install n8n with Docker
Use docker-compose to run n8n. The compose file sets environment variables for OAuth and the public URL. Replace placeholders with your values.
version: '3'
services:
n8n:
image: n8nio/n8n:latest
ports:
- 5678:5678
environment:
- N8N_HOST=YOUR_DOMAIN
- N8N_PORT=5678
- N8N_PROTOCOL=https
- WEBHOOK_URL=https://YOUR_DOMAIN/
- VUE_APP_URL_BASE_API=https://YOUR_DOMAIN/
volumes:
- ./n8n:/home/node/.n8n
Start n8n with a single command.
docker-compose up -d
Step 1: Expose n8n for Google OAuth (ngrok example)
If you run locally, use ngrok to provide HTTPS. Run ngrok and note the HTTPS URL.
ngrok http 5678
# note the https://xxxxx.ngrok.io URL and use it as YOUR_DOMAIN
Step 2: Configure OAuth in n8n
Open n8n in your browser at the public URL. Create new credentials for Gmail using the Google Client ID and Client Secret.
- 1. In n8n, go to Credentials > New Credential > Google OAuth2.
- 2. Paste Client ID and Client Secret.
- 3. For the Redirect URI, use the exact callback URL you added in Google Cloud.
- 4. Use scopes like
https://www.googleapis.com/auth/gmail.sendorhttps://www.googleapis.com/auth/gmail.readonly.
Step 3: Authorize the Gmail Node
After saving credentials, use the Gmail node in a workflow. Click the credential button to run the OAuth flow in your browser. Complete Google consent to grant access.
# Example: simple send workflow trigger
# In n8n create a workflow with a Manual Trigger and a Gmail node configured to send
# This requires the Gmail credential created earlier.
Update
To update n8n, pull the latest image and restart the container. Update your client secret in Google if you regenerate it. Keep the redirect URI consistent with your public URL.
docker pull n8nio/n8n:latest
docker-compose down
docker-compose up -d
Security
Limit OAuth scopes to the minimum your workflow needs. Store Client ID and Client Secret in environment variables or encrypted secret storage. Use HTTPS in production. Rotate credentials periodically.
Done
You now have n8n running with Gmail access via OAuth2. Use the Gmail node to send, read, and manage messages. Test with a simple workflow before adding automation to production.
If you build custom nodes with Node.js, develop locally and test OAuth using the same redirect and credentials. Keep secrets out of source control.