# `valstorm-plugin-sendgrid` — Hybrid SendGrid Transactional Email & Event Webhook Plugin (`FI-473`) Hybrid (**Python + Go**) SendGrid integration for **Open Valstorm**, mounted at `platform.sendgrid` (`VALSTORM_PLUGINS=sendgrid` or `*`). Provides transactional and bulk email delivery, `{{variable}}` template rendering, VFS file attachments, Email Activity Log search, and high-throughput Go worker processing for `/v3/mail/send` (`email:sendgrid`) and SendGrid Event Webhooks (`sendgrid:webhook_event` -> `sendgrid_email` `sgem_`). --- ## 🤖 Agent-Assisted Quick Setup (Let the Valstorm Agent Set It Up) You only need your **SendGrid API Key** (`SG....`) and verified sender email address. Your **Valstorm AI Agent** can validate your API key against SendGrid, save the encrypted credentials in `platform.credentials`, configure your SendGrid Event Webhook URL (`/v1/email-services/sendgrid/webhook/`) for open/click/delivery tracking, and verify live connectivity. ### 1. Quick Human Prerequisite (60 Seconds in SendGrid) 1. Log in to [app.sendgrid.com](https://app.sendgrid.com/). 2. Go to **Settings -> API Keys -> Create API Key** (grant *Mail Send* and *Email Activity* access) and copy the `SG....` key. 3. Ensure your sender domain or Single Sender Address is verified under **Settings -> Sender Authentication**. ### 2. Copy-Paste Prompt for Your Valstorm Agent Drop this prompt into Valstorm Chat: > *"Set up `valstorm-plugin-sendgrid` using my SendGrid API Key `` and default sender `` (Sender Name: ``). Follow `plugins/valstorm-plugin-sendgrid/README.md` to save the encrypted credentials via `platform.sendgrid.save_credentials(...)`, configure the SendGrid Event Webhook, and run `sendgrid_verify_connection`."* ### 3. Agent Execution Runbook (Instructions for the AI Agent) When a user asks you to set up `valstorm-plugin-sendgrid`, execute this sequence autonomously: 1. **Validate & Save Encrypted Credentials**: - Call `await platform.sendgrid.save_credentials(api_key=..., from_email=..., from_name=..., validate_live=True)` (or `POST /v1/email-services/sendgrid/credentials`). 2. **Configure Event Webhook (`processed`, `delivered`, `open`, `click`, `bounce`, `dropped`)**: - Call `await platform.sendgrid.configure_event_webhook(base_url="https://")` so SendGrid posts delivery and engagement events to `/v1/email-services/sendgrid/webhook/`. 3. **Verify Live Connection**: - Call `sendgrid_verify_connection` and report the verified sender identity and webhook status back to the user. --- ## 🛠️ End-to-End DIY Setup Guide (Manual) ### Step 1: Verify Your Domain or Single Sender in SendGrid 1. In [app.sendgrid.com](https://app.sendgrid.com/), navigate to **Settings -> Sender Authentication**. 2. Authenticate your sending domain (SPF/DKIM DNS records) or verify a Single Sender email address. ### Step 2: Create a SendGrid API Key 1. Go to **Settings -> API Keys -> Create API Key**. 2. Copy the `SG....` key (shown once). ### Step 3: Configure the SendGrid Event Webhook 1. Navigate to **Settings -> Mail Settings -> Event Settings -> Event Webhook**. 2. Set the **HTTP Post URL** to: `https://api.yourdomain.com/v1/email-services/sendgrid/webhook/` 3. Check **Processed**, **Delivered**, **Opened**, **Clicked**, **Bounced**, **Dropped**, **Spam Report**, and **Unsubscribed**, then click **Save**. ### Step 4: Save Encrypted Credentials in Open Valstorm ```python await platform.sendgrid.save_credentials( api_key="SG.xxxxxxxxxxxxxxxxxxxx.yyyyyyyyyyyyyyyyyyyy", from_email="notifications@yourdomain.com", from_name="Valstorm Notifications", ) ``` --- ## Running Tests (Python & Go) ```bash # Run Python unit & route tests cd apps/open-api .venv/bin/pytest tests/test_plugin_sendgrid.py -v # Run Go worker `/v3/mail/send` & Event Webhook race tests cd ../../plugins/valstorm-plugin-sendgrid/go go test -race -v ./... ```