Prevent duplicate batch sends with idempotency keys:
ctx := context.Background()batchEmails := []*resend.SendEmailRequest{ { To: []string{"[email protected]"}, From: "[email protected]", Subject: "Order Confirmation", Text: "Your order has been confirmed.", },}options := &resend.BatchSendEmailOptions{ IdempotencyKey: "order-batch-12345",}// First request sends the emailssent, err := client.Batch.SendWithOptions(ctx, batchEmails, options)if err != nil { panic(err)}// Second request with same key returns the same response without sending duplicatessent2, err := client.Batch.SendWithOptions(ctx, batchEmails, options)// sent2 will have the same email IDs as sent
// Send welcome emails to new usersvar batchEmails []*resend.SendEmailRequestfor _, user := range newUsers { batchEmails = append(batchEmails, &resend.SendEmailRequest{ To: []string{user.Email}, From: "[email protected]", Subject: fmt.Sprintf("Welcome %s!", user.Name), Html: fmt.Sprintf("<h1>Welcome %s!</h1><p>Thanks for joining.</p>", user.Name), Tags: []resend.Tag{ {Name: "campaign", Value: "welcome"}, {Name: "user_id", Value: user.ID}, }, })}// Use permissive mode to ensure valid emails are sentoptions := &resend.BatchSendEmailOptions{ BatchValidation: resend.BatchValidationPermissive, IdempotencyKey: fmt.Sprintf("welcome-batch-%s", time.Now().Format("2006-01-02")),}sent, err := client.Batch.SendWithOptions(ctx, batchEmails, options)
Notification Broadcast
// Send notification to all subscribersvar batchEmails []*resend.SendEmailRequestfor _, subscriber := range subscribers { batchEmails = append(batchEmails, &resend.SendEmailRequest{ To: []string{subscriber.Email}, From: "[email protected]", Subject: "New Feature Released!", Html: "<p>We just released an exciting new feature...</p>", Tags: []resend.Tag{ {Name: "type", Value: "notification"}, {Name: "subscriber_id", Value: subscriber.ID}, }, })}// Use strict mode to ensure all or nothingsent, err := client.Batch.SendWithContext(ctx, batchEmails)
Use strict mode for critical transactional emails where consistency matters:
// Strict for order confirmations (all must succeed)BatchValidation: resend.BatchValidationStrict// Permissive for marketing campaigns (partial success OK)BatchValidation: resend.BatchValidationPermissive
2
Use Idempotency Keys
Always use idempotency keys for batch sends to prevent duplicates on retry:
In permissive mode, always check and log validation errors:
if len(sent.Errors) > 0 { // Log for retry or investigation for _, err := range sent.Errors { log.Printf("Email at index %d failed: %s", err.Index, err.Message) }}
5
Batch Size Optimization
Keep batches to 100-500 emails for optimal performance:
const batchSize = 250// Split large lists into batches of 250
6
Rate Limit Awareness
Add delays between batches if sending many batches in succession:
time.Sleep(200 * time.Millisecond) // Brief pause between batches