Implementation:OpenHands OpenHands ResendKeycloakSync
| Knowledge Sources | |
|---|---|
| Domains | Data_Sync, Email |
| Last Updated | 2026-02-11 21:00 GMT |
Overview
Standalone sync script that exports Keycloak user records to Resend email audiences and sends HTML welcome emails to new users.
Description
The resend_keycloak module fetches users from Keycloak with pagination, deduplicates against existing Resend audience contacts, validates email formats using strict regex, adds new users to the Resend audience via API, and sends HTML welcome emails. It uses tenacity-based retry logic with exponential backoff, rate limiting (2 requests/second), and tracks sync statistics (added, skipped, errors). Supports dry-run mode for testing.
Usage
Run this script as a periodic background job to synchronize Keycloak users to the Resend email marketing platform and send welcome emails to newly registered users.
Code Reference
Source Location
- Repository: OpenHands
- File: enterprise/sync/resend_keycloak.py
- Lines: 1-436
Signature
def is_valid_email(email: str) -> bool: ...
def get_keycloak_users(offset: int = 0, limit: int = 100) -> List[Dict]: ...
def get_total_keycloak_users() -> int: ...
def get_resend_contacts(audience_id: str) -> Dict[str, Dict]: ...
async def add_contact_to_resend(
contact_data: Dict, audience_id: str
) -> bool: ...
async def send_welcome_email(email: str, user_data: Dict) -> bool: ...
async def sync_users_to_resend(dry_run: bool = False) -> Dict: ...
Import
# Standalone script, not typically imported
# Run directly: python enterprise/sync/resend_keycloak.py
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| KEYCLOAK_SERVER_URL | env var | Yes | Keycloak server URL |
| KEYCLOAK_REALM_NAME | env var | Yes | Keycloak realm name |
| RESEND_API_KEY | env var | Yes | Resend API key |
| RESEND_AUDIENCE_ID | env var | Yes | Resend audience identifier |
| dry_run | bool | No | Test mode (no actual API calls) |
Outputs
| Name | Type | Description |
|---|---|---|
| sync_users_to_resend() | Dict | Statistics dict with added, skipped, errors counts |
| is_valid_email() | bool | Email format validation result |
Usage Examples
# Run as standalone script
# python enterprise/sync/resend_keycloak.py
# Dry run mode
from enterprise.sync.resend_keycloak import sync_users_to_resend
stats = await sync_users_to_resend(dry_run=True)
print(f"Would add {stats['added']} users, skip {stats['skipped']}")