0

Create an email campaign

by
Published Apr 8, 2025
Script brevo Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Brevo = {
3
  apiKey: string;
4
};
5
/**
6
 * Create an email campaign
7
 *
8
 */
9
export async function main(
10
  auth: Brevo,
11
  body: {
12
    tag?: string;
13
    sender: { name?: string; email?: string; id?: number };
14
    name: string;
15
    htmlContent?: string;
16
    htmlUrl?: string;
17
    templateId?: number;
18
    scheduledAt?: string;
19
    subject?: string;
20
    previewText?: string;
21
    replyTo?: string;
22
    toField?: string;
23
    recipients?: {
24
      exclusionListIds?: number[];
25
      listIds?: number[];
26
      segmentIds?: number[];
27
    };
28
    attachmentUrl?: string;
29
    inlineImageActivation?: false | true;
30
    mirrorActive?: false | true;
31
    footer?: string;
32
    header?: string;
33
    utmCampaign?: string;
34
    params?: {};
35
    sendAtBestTime?: false | true;
36
    abTesting?: false | true;
37
    subjectA?: string;
38
    subjectB?: string;
39
    splitRule?: number;
40
    winnerCriteria?: "open" | "click";
41
    winnerDelay?: number;
42
    ipWarmupEnable?: false | true;
43
    initialQuota?: number;
44
    increaseRate?: number;
45
    unsubscriptionPageId?: string;
46
    updateFormId?: string;
47
    emailExpirationDate?: {
48
      duration?: number;
49
      unit?: "days" | "weeks" | "months";
50
    };
51
  },
52
) {
53
  const url = new URL(`https://api.brevo.com/v3/emailCampaigns`);
54

55
  const response = await fetch(url, {
56
    method: "POST",
57
    headers: {
58
      "Content-Type": "application/json",
59
      "api-key": auth.apiKey,
60
    },
61
    body: JSON.stringify(body),
62
  });
63
  if (!response.ok) {
64
    const text = await response.text();
65
    throw new Error(`${response.status} ${text}`);
66
  }
67
  return await response.json();
68
}
69