import Migma from 'migma';const migma = new Migma(process.env.MIGMA_API_KEY);// A project holds your brand info (colors, fonts, logos, tone of voice).// Import one from your website, or use an existing project ID from the dashboard.const brand = await migma.projects.importAndWait({ urls: ['https://yourbrand.com']});// Generate an email design with AI — returns the finished HTML directly.const email = await migma.emails.generateAndWait({ projectId: brand.data.projectId, prompt: 'Create a welcome email for new subscribers', languages: ['en']});if (email.data?.status === 'completed') { console.log('Subject:', email.data.result.subject); console.log('HTML:', email.data.result.html); // primary email HTML console.log('Email ID:', email.data.result.emails[0].emailId);}
Prefer async? Use migma.emails.generate() to kick off generation without waiting, then set up a webhook to receive an email.generation.completed event when the design is ready — no polling needed.
OpenClaw (formerly ClawdBot) is an open-source AI agent for WhatsApp, Telegram, and Discord. Pair it with Migma to generate and send professional emails from a single chat command.
import Migma from 'migma';const migma = new Migma(process.env.MIGMA_API_KEY);// 1. Set up an instant sending domain (no DNS needed — see /sending-domains/overview)await migma.domains.createManaged({ prefix: 'yourcompany' });// 2. Generate an on-brand email with AIconst { data: email } = await migma.emails.generateAndWait({ projectId: process.env.MIGMA_PROJECT_ID, // your brand project prompt: 'Create a summer sale email with 30% off everything',});if (!email?.result) throw new Error('Email generation failed');// 3. Send it — emailId works for single emails and series slotsconst selectedEmail = email.result.emails[0];if (!selectedEmail.emailId) throw new Error('No emailId returned');await migma.sending.send({ recipientType: 'email', recipientEmail: 'sarah@example.com', from: 'hello@yourcompany.migma.email', fromName: 'Your Company', subject: selectedEmail.subject, emailId: selectedEmail.emailId,});
conversationId still works for single-email conversations. If the email has multiple slots or is part of a series, use the slot emailId from result.emails[].
Full OpenClaw Tutorial
Step-by-step guide: API key, sending domain, project setup, and sending from WhatsApp/Telegram/Discord.
Email generation, brand import, and preview creation are asynchronous. The SDK provides *AndWait helpers that poll automatically until the operation completes:
const email = await migma.emails.generateAndWait( { projectId: 'proj_123', prompt: 'Create a product launch email' }, { interval: 2000, // poll every 2s (default) maxAttempts: 150, // give up after 150 polls (default) onPoll: (status, attempt) => { console.log(`Poll ${attempt}: ${status.status}`); } });
All three polling methods:
Method
Waits for
migma.emails.generateAndWait()
Email generation to complete
migma.projects.importAndWait()
Brand import to finish
migma.previews.createAndWait()
Device preview screenshots
You can cancel any polling operation with an AbortController:
const controller = new AbortController();setTimeout(() => controller.abort(), 60000); // 60s timeoutconst result = await migma.emails.generateAndWait(params, { signal: controller.signal});
Generate one email or a full series from the same API. Pass count when you want a specific number of emails; otherwise Migma infers the output from the prompt.
const { data: series } = await migma.emails.generateAndWait({ projectId: 'proj_123', prompt: 'Create a three-email welcome series for new trial users', count: 3});if (series?.status === 'completed') { for (const email of series.result.emails) { console.log(email.slot, email.emailId, email.subject); console.log(email.html); console.log(email.screenshotUrl); }}
result.html and result.subject are always the primary email for backwards compatibility. Use result.emails[] when you need the stable emailId, per-email HTML, screenshots, or series order.
By default, emails send through Migma’s built-in infrastructure. You can also send via Amazon SES, Resend, SendGrid, or Mailgun by connecting your provider and passing providerType in the send call.