Newsletters and Broadcasts
Sign people up to a free package, confirm the address with a sign-in code, and send broadcasts to its members.
In Arky a newsletter is a free package. A sign-up is a free order for the package, which makes the visitor a member; each issue is a broadcast to the package’s members. Joining is the “yes”; unsubscribing stops broadcasts to the address and ends no membership.
The same model covers paid newsletters and members-only email: a broadcast goes to the members of chosen packages, or of every package in an offering, free or paid.
1. The Package
A new store has one package, keyed public, which is its shop. Create another for the newsletter: an
active package that never renews and has no items:
const updates = await sdk.eshop.package.create({
store_id: storeId,
id: crypto.randomUUID(),
key: "newsletter",
blocks: [],
renewal: { type: "never" },
stay: { type: "any_time" },
minimum: { type: "none" },
schedule: { type: "always" },
status: { type: "active" },
items: [],
});
A package with no automatic items is free, and it uses none of the plan’s packages limit. Its sign-up
order has no money, no billing address and no promotions, and it sends no order emails.
2. Price It In A Shop
The storefront signs up through a cart, so the newsletter must be a priced item of the package the cart
shops in: a package item, saved together with its one price, zero in that market. The store’s first
package is keyed public; any package the visitor may buy from works:
const newsletter = await sdk.eshop.package.getByKey({ store_id: storeId, key: "newsletter" });
const shop = await sdk.eshop.package.getByKey({ store_id: storeId, key: "public" });
const eu = await sdk.store.market.getByKey({ store_id: storeId, key: "eu" });
await sdk.eshop.packageItem.create({
store_id: storeId,
id: crypto.randomUUID(),
package_id: shop.id,
market_id: eu.id,
item: {
type: "package",
package_id: newsletter.id,
price: {
id: crypto.randomUUID(),
amount: 0,
compare_at: { type: "none" },
changes: [],
schedule: { type: "always" },
status: { type: "active" },
},
},
});
3. Sign People Up From The Site
Broadcasts reach a member only at an email they proved with the sign-in code, so nobody can sign up a stranger by typing their address. The sign-up form asks for the email, sends a code, and joins once the code is confirmed:
import { PackageJoinError, initialize } from "arky-sdk/storefront";
import { createOrderToken } from "arky-sdk/utils";
const arky = initialize("arky_pk_...", {
locale: "en",
market: "eu",
createVisitorIdentity: () => ({
customer_id: crypto.randomUUID(),
session_id: crypto.randomUUID(),
}),
createSessionId: () => crypto.randomUUID(),
createId: () => crypto.randomUUID(),
createOrderToken,
});
const { items: packages } = await arky.eshop.package.find();
const shop = packages.find((value) => value.key === "public");
if (!shop) throw new Error("Newsletter shop unavailable");
async function startSignUp(email: string) {
const sent = await arky.newsletter.requestCode({ contact: { email, language: "en" } });
return sent.type;
}
async function finishSignUp(email: string, code?: string) {
try {
const joined = await arky.newsletter.join({
email,
code,
key: "newsletter",
package_id: shop!.id,
language: "en",
});
return joined.type;
} catch (error) {
if (error instanceof PackageJoinError) return error.code;
throw error;
}
}
requestCodeemails the sign-in code to the contact you name, in its language, and answers{ type: "code_sent" }. When the browser is already signed in with that address it sends nothing and answers{ type: "verified" }: join without a code. Signed in with another address, it signs out first.joinconfirms the code you pass and checks that it proved this address, then signs the customer up with a free order. It reads the package and the prices of its item in the shop, makes its own cart, so the shopper’s cart is left alone, adds the package line at the package’s zero price, checks that the quote is free, and checks out with{ type: "free" }, the customer’s verified email and thelanguageyou pass, normally the page language.- It answers
{ type: "joined", order_id }, or{ type: "already_member", package_member_id }when the customer already belongs to the package, with no new order. Show both as subscribed. Joining again lifts the store’s unsubscribe for that address, so it is also how someone who unsubscribed comes back. - The cart, line and order ids and the order token come from your factories, and
joinkeeps them until a definite answer. A retry after a lost answer finishes the same sign-up without a second order; retry without the code, because the session is already signed in. joinnames the newsletter by itskeyor itsid, andpackage_idnames the shop it is priced in. The shop must list the newsletter for the visitor in the current market, and the newsletter must never renew, have no automatic items and be priced at zero there.- Asking for a code or confirming it can be refused with its own code:
CUSTOMER_SIGN_IN.CODE_RECENT(a new code too soon),CUSTOMER_SIGN_IN.CODE_INVALID(a wrong or expired code) orCUSTOMER_SIGN_IN.ATTEMPTS_EXHAUSTED(too many wrong codes); show your own text for each. - A refusal is a
PackageJoinError. Itscodeissign_in_required(the address isn’t confirmed),email_mismatch(the code proved another address),not_free, orsign_up_usedandsign_up_expired(the kept sign-up can’t finish: join again). - A customer who is already signed in with a verified email can join with
arky.eshop.package.joinFree({ key, package_id, language })(or{ id, package_id, language }), which answers the same way. - Someone the team assigned to the package gets no broadcasts until they sign up themselves; their sign-up order then reuses the same member.
- A storefront can also offer
arky.customer.unsubscribe()in the account, andresubscribe()to come back.
The arky.io footer’s newsletter form follows this flow; its source is the arky.io repository’s
src/lib/services/newsletter.ts.
4. Send A Broadcast
const { creation_templates } = await sdk.store.emails.get({ store_id: storeId });
const draft = await sdk.broadcast.create({
store_id: storeId,
id: crypto.randomUUID(),
key: "november-news",
audience: {
include: [{ type: "package", package_id: newsletter.id }],
exclude: [],
conditions: [],
},
email: {
email_template_id: creation_templates.broadcast,
sending_address_id: news.id,
from_name: "Northwind",
reply_to: { type: "address", email_address_id: news.id },
},
});
await sdk.broadcast.send({ store_id: storeId, id: draft.id, expected_updated_at: draft.updated_at });
Or schedule it with schedule({ send_at }), and take it back to draft with unschedule. As sending
goes on, Arky makes one email per person member of the audience who counts and joined with an
order, once per address, to the member’s verified contact: in its language when the broadcast’s
template has it, otherwise in the template’s default language. Scheduling, or sending a draft,
freezes the template, so editing it later changes no scheduled broadcast. The broadcast ends sent
with its recipient_count.
Arky’s own unsubscribe page is in English. To confirm in the store’s own words and languages, link
the broadcast template to a page on the store’s site with ?token={{unsubscribe_token}} instead of
{{unsubscribe_url}}. When the reader confirms, the page posts the token; it needs no session and
answers { result: "unsubscribed" }:
const token = new URL(location.href).searchParams.get("token");
if (token) await arky.broadcast.unsubscribe({ token });Every broadcast template prints {{unsubscribe_url}}, or {{unsubscribe_token}} in a link to the
store’s own unsubscribe page, in its HTML and its text. Arky also adds the one-click unsubscribe
headers Gmail and Yahoo require, which always name Arky’s address. Unsubscribing stops broadcasts to the address; it
never ends a membership, so an email link can’t cancel a paid package.
Replies to a broadcast go to its selection’s reply-to. Make that a receiving store address that a support flow’s entry names to get them as conversations; see Support.