Get Your API Key
First, you’ll need to create a Migma.ai account and generate an API key:
Navigate to Settings
Go to Settings → API Integration → API Keys tab
Create API Key
Click Create API Key , select permissions, and save your key securely
Store your API key securely! It won’t be shown again after creation. Treat it like a password.
Import Your Brand
Before generating emails, you need to import your brand information:
curl -X POST https://api.migma.ai/v1/projects/import \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"urls": ["https://yourbrand.com"],
"name": "My Brand"
}'
const response = await fetch ( 'https://api.migma.ai/v1/projects/import' , {
method: 'POST' ,
headers: {
'Authorization' : 'Bearer YOUR_API_KEY' ,
'Content-Type' : 'application/json'
},
body: JSON . stringify ({
urls: [ 'https://yourbrand.com' ],
name: 'My Brand'
})
});
const data = await response . json ();
console . log ( 'Project ID:' , data . data . projectId );
import requests
response = requests.post(
'https://api.migma.ai/v1/projects/import' ,
headers = {
'Authorization' : 'Bearer YOUR_API_KEY' ,
'Content-Type' : 'application/json'
},
json = {
'urls' : [ 'https://yourbrand.com' ],
'name' : 'My Brand'
}
)
data = response.json()
print ( f "Project ID: { data[ 'data' ][ 'projectId' ] } " )
Brand import is asynchronous. Save the projectId and use it in subsequent requests. You can check the import status by polling the project endpoint or using webhooks.
Generate Your First Email
Once your brand is imported, generate an email:
curl -X POST https://api.migma.ai/v1/projects/emails/generate \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"projectId": "your_project_id",
"prompt": "Create a welcome email for new subscribers",
"language": "en",
"purpose": "welcome"
}'
const response = await fetch (
'https://api.migma.ai/v1/projects/emails/generate' ,
{
method: 'POST' ,
headers: {
'Authorization' : 'Bearer YOUR_API_KEY' ,
'Content-Type' : 'application/json'
},
body: JSON . stringify ({
projectId: projectId ,
prompt: 'Create a welcome email for new subscribers' ,
language: 'en' ,
purpose: 'welcome'
})
}
);
const data = await response . json ();
console . log ( 'Conversation ID:' , data . data . conversationId );
console . log ( 'Status:' , data . data . status ); // 'pending'
response = requests.post(
'https://api.migma.ai/v1/projects/emails/generate' ,
headers = {
'Authorization' : 'Bearer YOUR_API_KEY' ,
'Content-Type' : 'application/json'
},
json = {
'projectId' : project_id,
'prompt' : 'Create a welcome email for new subscribers' ,
'language' : 'en' ,
'purpose' : 'welcome'
}
)
data = response.json()
print ( f "Conversation ID: { data[ 'data' ][ 'conversationId' ] } " )
print ( f "Status: { data[ 'data' ][ 'status' ] } " ) # 'pending'
Poll for Status
Email generation is asynchronous. Poll the status endpoint:
async function waitForGeneration ( conversationId ) {
while ( true ) {
const response = await fetch (
`https://api.migma.ai/v1/projects/emails/ ${ conversationId } /status` ,
{
headers: {
'Authorization' : 'Bearer YOUR_API_KEY'
}
}
);
const data = await response . json ();
const status = data . data . status ;
if ( status === 'completed' ) {
console . log ( 'Email generated successfully!' );
console . log ( 'HTML:' , data . data . html );
console . log ( 'React:' , data . data . react );
return data . data ;
} else if ( status === 'failed' ) {
throw new Error ( `Generation failed: ${ data . data . error } ` );
}
// Wait 2 seconds before next poll
await new Promise ( resolve => setTimeout ( resolve , 2000 ));
}
}
Better approach : Use webhooks to get notified when generation completes instead of polling!
Get the Generated Email
Retrieve the generated email HTML and React code:
curl https://api.migma.ai/v1/projects/emails/{conversationId}/status \
-H "Authorization: Bearer YOUR_API_KEY"
const response = await fetch (
`https://api.migma.ai/v1/projects/emails/ ${ conversationId } /status` ,
{
headers: {
'Authorization' : 'Bearer YOUR_API_KEY'
}
}
);
const data = await response . json ();
if ( data . data . status === 'completed' ) {
const emailHtml = data . data . html ;
const emailReact = data . data . react ;
console . log ( 'Generated HTML:' , emailHtml );
console . log ( 'React Email code:' , emailReact );
}
Complete Example
Here’s a complete workflow from import to email generation:
const MIGMA_API_KEY = 'your_api_key_here' ;
const BASE_URL = 'https://api.migma.ai' ;
async function generateEmail () {
try {
// 1. Import brand
console . log ( 'Importing brand...' );
const importRes = await fetch ( ` ${ BASE_URL } /v1/projects/import` , {
method: 'POST' ,
headers: {
'Authorization' : `Bearer ${ MIGMA_API_KEY } ` ,
'Content-Type' : 'application/json'
},
body: JSON . stringify ({
urls: [ 'https://yourbrand.com' ],
name: 'My Brand'
})
});
const importData = await importRes . json ();
const projectId = importData . data . projectId ;
console . log ( 'Project created:' , projectId );
// 2. Wait for import to complete (simplified - use webhooks in production)
await new Promise ( resolve => setTimeout ( resolve , 10000 ));
// 3. Generate email
console . log ( 'Generating email...' );
const genRes = await fetch (
` ${ BASE_URL } /v1/projects/emails/generate` ,
{
method: 'POST' ,
headers: {
'Authorization' : `Bearer ${ MIGMA_API_KEY } ` ,
'Content-Type' : 'application/json'
},
body: JSON . stringify ({
projectId: projectId ,
prompt: 'Create a welcome email for new subscribers' ,
language: 'en' ,
purpose: 'welcome'
})
}
);
const genData = await genRes . json ();
const conversationId = genData . data . conversationId ;
console . log ( 'Generation started:' , conversationId );
// 4. Poll for completion
let completed = false ;
let emailData = null ;
while ( ! completed ) {
await new Promise ( resolve => setTimeout ( resolve , 2000 ));
const statusRes = await fetch (
` ${ BASE_URL } /v1/projects/emails/ ${ conversationId } /status` ,
{
headers: { 'Authorization' : `Bearer ${ MIGMA_API_KEY } ` }
}
);
const statusData = await statusRes . json ();
if ( statusData . data . status === 'completed' ) {
completed = true ;
emailData = statusData . data ;
} else if ( statusData . data . status === 'failed' ) {
throw new Error ( 'Generation failed: ' + statusData . data . error );
}
}
console . log ( 'Email generated successfully!' );
return {
html: emailData . html ,
react: emailData . react
};
} catch ( error ) {
console . error ( 'Error:' , error );
throw error ;
}
}
// Run it
generateEmail (). then ( result => {
console . log ( 'HTML:' , result . html . substring ( 0 , 200 ) + '...' );
console . log ( 'React:' , result . react . substring ( 0 , 200 ) + '...' );
});
Next Steps
Common Issues
Check that your API key is correct and included in the Authorization header as Bearer YOUR_API_KEY
Brand imports can take 30-60 seconds. Use webhooks to get notified when complete instead of polling.
Check the error message in the response. Common issues:
Invalid projectId (brand not imported yet)
Prompt is too vague or too long
Rate limit exceeded
The generated HTML is optimized for email clients. Test it in an actual email client or use a service like Litmus.