Skip to Content
Appwright
Examples

Newsletter Signup

Capture newsletter signups from a storefront form as metaobject entries

A worker that accepts newsletter signups from a storefront form and records each one as a metaobject entry — no third-party form app required.

Examples are deliberately generic — they’re here to show the moving parts, not to be pasted in verbatim. Comments mark the spots you’ll need to adapt, including the metaobject type and field keys, which must match a definition you create in your Shopify admin.

Setup

  • No secrets required — this worker only uses the built-in Admin API client.
  • A metaobject definition to hold signups (for example a newsletter_signup type with email, source_page, country, and signed_up_at fields). Workers can create metaobject entries but not definitions — create the definition under Content → Metaobjects in your Shopify admin first (see Shopify Scopes).
  • A storefront form that POSTs JSON to https://<your-shop>/apps/bound/<handle>.

The worker

// Storefront endpoint: POST /apps/bound/<handle>
// Expects a JSON body from your signup form, e.g. { "email": "...", "sourcePage": "..." }

let signup;
try {
  signup = await request.json();
} catch {
  return new Response("Expected a JSON body", { status: 400 });
}

// Validate whatever your form promises to send. Treat everything as
// untrusted input — this endpoint is public on your storefront.
const email = signup.email;
if (typeof email !== "string" || !email.includes("@")) {
  return new Response("A valid email is required", { status: 400 });
}

// Record the signup as a metaobject entry, with whatever captured context
// is useful — Cloudflare geolocation headers are a typical extra source.
// this.ctx.props.admin is pre-authenticated — no tokens here.
const result = await this.ctx.props.admin.query(
  `#graphql
  mutation SaveSignup($metaobject: MetaobjectCreateInput!) {
    metaobjectCreate(metaobject: $metaobject) {
      metaobject { id }
      userErrors { field message }
    }
  }`,
  {
    metaobject: {
      type: "newsletter_signup", // must match your metaobject definition
      fields: [
        { key: "email", value: email },
        { key: "source_page", value: signup.sourcePage ?? "" },
        { key: "country", value: request.headers.get("cf-ipcountry") ?? "" },
        { key: "signed_up_at", value: new Date().toISOString() },
      ],
    },
  },
);

// GraphQL errors inside a 200 response don't throw — check userErrors so a
// bad write doesn't look like success.
const userErrors = result.data?.metaobjectCreate?.userErrors ?? [];
if (userErrors.length > 0) {
  return new Response(`Signup failed: ${userErrors[0].message}`, {
    status: 500,
  });
}

return new Response("thanks");

Notes

  • Metaobjects don’t enforce uniqueness — a repeat signup creates a second entry. If that matters, query metaobjects by email first and skip the write when one exists, or dedupe downstream where you consume the list.
  • Actually sending email (a welcome message, or syncing the signup to your ESP) can run in this.ctx.waitUntil() so the form responds instantly — put the ESP’s API key in a secret enabled on this worker.
  • Processing shopper-submitted data makes you responsible for it — disclose it in your store’s privacy policy.