Skip to content

Commit

Permalink
Add new subscribers to the mailerlite
Browse files Browse the repository at this point in the history
Add new subscribers to the mailerlite instead of mailchimp
  • Loading branch information
leomet07 committed Nov 6, 2023
1 parent c663d40 commit 0619468
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ yarn
MONGODB_URI=mongodb+srv://...
```

Optionally, add a api token for MailerLite in `MAILERLITE_API_TOKEN`

4. Run the development server:

```bash
Expand Down
17 changes: 7 additions & 10 deletions pages/api/subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,30 @@ export default async function handler(
});
}

const API_KEY = process.env.MAILCHIMP_API_KEY;
const API_SERVER = process.env.MAILCHIMP_API_SERVER;
const AUDIENCE_ID = process.env.MAILCHIMP_AUDIENCE_ID;
const API_KEY = process.env.MAILERLITE_API_TOKEN;

const url = `https://${API_SERVER}.api.mailchimp.com/3.0/lists/${AUDIENCE_ID}/members`;
const url = `https://connect.mailerlite.com/api/subscribers`;
const body = {
email_address: email,
status: "subscribed",
email,
};

try {
const r = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `api_key ${API_KEY}`,
Authorization: `Bearer ${API_KEY}`,
},
body: JSON.stringify(body),
});

const data: any = await r.json();
if (data.status == "subscribed") {
return res.status(201).json({ message: "success", response: data });
if (data?.data?.status == "active") {
return res.status(201).json({ message: "success" });
} else {
return res.status(500).json({
message: "failure",
error: { title: data.title },
error: { title: data.message },
});
}
} catch (error: any) {
Expand Down

0 comments on commit 0619468

Please sign in to comment.