Skip to content

Commit

Permalink
migrate from mailerlite to beehiiv
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrkulpinski committed Aug 6, 2024
1 parent c661cfa commit 512d937
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
5 changes: 3 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ VITE_PLAUSIBLE_HOST="https://plausible.io"
# AdSense
VITE_ADSENSE_PUBLISHER_ID=""

# MailerLite
MAILERLITE_API_TOKEN=""
# Beehiiv
BEEHIIV_API_KEY=""
BEEHIIV_PUBLICATION_ID=""

# GitHub Token
GITHUB_TOKEN=""
Expand Down
20 changes: 13 additions & 7 deletions app/routes/api.subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ const subscriberSchema = z.object({
.string()
.email("Invalid email address, please use a correct format.")
.refine(isRealEmail, "Invalid email address, please use a real one."),
groups: z.array(z.string()).optional(),
referring_site: z
.string()
.optional()
.default(process.env.VITE_SITE_URL ?? ""),
utm_source: z.string().optional(),
utm_medium: z.string().optional(),
utm_campaign: z.string().optional(),
reactivate_existing: z.boolean().optional().default(true),
send_welcome_email: z.boolean().optional().default(true),
})

export type ActionState =
Expand All @@ -26,17 +34,15 @@ export type ActionState =
export async function action({ request }: ActionFunctionArgs): Promise<TypedResponse<ActionState>> {
const data = await request.formData()
const parsed = await subscriberSchema.safeParseAsync(Object.fromEntries(data.entries()))
const url = `https://api.beehiiv.com/v2/publications/${process.env.BEEHIIV_PUBLICATION_ID}/subscriptions`
const authorization = `Bearer ${process.env.BEEHIIV_API_KEY}`

if (!parsed.success) {
return json({ type: "error", error: parsed.error.format() })
}

await got
.post("https://connect.mailerlite.com/api/subscribers", {
json: parsed.data,
headers: { authorization: `Bearer ${process.env.MAILERLITE_API_TOKEN}` },
})
.json()
// Subscribe to the publication
await got.post(url, { json: parsed.data, headers: { authorization } }).json()

// Return a success response
return json({ type: "success", message: "Thank you for subscribing!" })
Expand Down

0 comments on commit 512d937

Please sign in to comment.