keep
keep copied to clipboard
feat(mailgun): Add EU region support and UI configuration options
close https://github.com/keephq/keep/issues/5416
๐ฏ Overview
This PR enhances the Mailgun provider for self-hosted Keep instances with:
- EU Region Support - Users can now use Mailgun EU accounts
- UI Region Selection - Dropdown to choose US/EU region per provider
- API Request Format Fix - Corrects route creation bug
- Hybrid Configuration - Supports both UI and environment variable configuration
๐ Problems Solved
Issue 1: EU Region Not Supported
The Mailgun API endpoint was hardcoded to https://api.mailgun.net (US region only). Users with EU Mailgun accounts received 401/400 errors when installing the provider.
Issue 2: Incorrect API Request Format
The setup_webhook() method was passing both files and data parameters to requests.post(), causing "400 Bad Request" errors from Mailgun API.
Issue 3: No UI Control for Region
Users had to modify code or use environment variables. No per-provider region configuration was possible.
โจ Solution
1. UI Dropdown for Region Selection
Added api_region field to MailgunProviderAuthConfig with dropdown:
- Type: Select field with options ["US", "EU"]
- Optional: Can be left empty to use environment variable
- User-friendly: Clear hints and descriptions
2. Hybrid Configuration Priority
region = (
self.authentication_config.api_region # 1. Check UI field first
or MailgunProvider.MAILGUN_REGION # 2. Fall back to env var
or "US" # 3. Default to US
).upper()
Benefits:
- Per-provider region configuration (UI)
- Global default via
MAILGUN_REGIONenvironment variable - Backward compatible (existing deployments unaffected)
- Flexible for multi-region deployments
3. API Endpoint Selection
Dynamically selects the correct API endpoint:
-
US Region:
https://api.mailgun.net -
EU Region:
https://api.eu.mailgun.net
4. API Request Format Fix
Before (Broken):
response = requests.post(
url,
files=payload, # โ Wrong parameter
auth=("api", key),
data=payload, # โ Missing action array
)
After (Fixed):
response = requests.post(
url,
auth=("api", key),
data={
**payload,
"action": [ # โ
Correct format
f"forward('{keep_api_url}&api_key={api_key}')",
"stop()",
],
},
)
๐งช Testing
- โ Tested with US region Mailgun account
- โ Tested with EU region Mailgun account
- โ Route creation works correctly in both regions
- โ UI dropdown displays and saves correctly
- โ Environment variable fallback works
- โ Backward compatible (defaults to US)
- โ No linting errors
๐ Changes
Files Modified:
-
keep/providers/mailgun_provider/mailgun_provider.py- Added
api_regionfield with dropdown (lines 49-59) - Added
MAILGUN_REGIONclass variable (line 74) - Dynamic API endpoint selection (lines 222-229)
- Fixed request format in
setup_webhook()(lines 231-259)
- Added
๐ Backward Compatibility
- โ Existing deployments unaffected (defaults to US region)
- โ
Environment variable
MAILGUN_REGIONstill works - โ No breaking changes to configuration
- โ Works with both old and new Mailgun API key formats