We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

Subscribe to news letter using API

Subscribe to news letter using API

techboas
Visitor
1 0 0

I'm building an app with shopify as headless CMS, I need to make a part where a visitor can subscribe to our news letter, two cases:

-When client has account, I can subscribe using storefront API to update 'acceptsMarketing' and set it to true so that marketing apps can use it to email our campaigns.

-When it's a visitor, I'm not sure how to do this, is there a way to subscribe even a visitor?

Reply 1 (1)

DaisyVo
Shopify Partner
4469 501 598

Hi @techboas 

It sounds like you're working on integrating a newsletter subscription feature into your Shopify-based headless setup. You’ve already got the approach sorted out for logged-in customers by updating the acceptsMarketing field via the Storefront API, but handling guest (visitor) subscriptions is where you're stuck. Let’s break it down.

Solution for Guest (Visitor) Subscriptions:

Since visitors don’t have accounts, they aren’t recognized by Shopify's customer model. This means you can’t update acceptsMarketing directly for them. However, there are two solid approaches to get around this:

1. Using a Marketing App's API (Recommended)

Many email marketing platforms like Klaviyo, Mailchimp, and Omnisend offer APIs that allow you to subscribe users without needing a Shopify account. This is the easiest and most scalable approach. Here’s how you can do it:

  • Capture the visitor’s email through your frontend.
  • Send this email to a marketing tool’s API (e.g., Klaviyo's /api/v2/list endpoint).
  • These tools will then handle the newsletter opt-in process for you.
Example with Klaviyo:

 

fetch('https://a.klaviyo.com/api/v2/list/YOUR_LIST_ID/members', {

    method: 'POST',

    headers: {

        'Content-Type': 'application/json'

    },

    body: JSON.stringify({

        api_key: 'YOUR_KLAVIYO_PRIVATE_KEY',

        profiles: [{ email: "visitor@example.com" }]

    })

});

 

This will add the visitor's email to your Klaviyo list.

2. Creating a Shopify Customer Record for Visitors

If you prefer keeping all data in Shopify, you can create a customer record for visitors (without requiring them to log in) and set acceptsMarketing to true.

Steps:
  1. When a visitor enters their email, call the Shopify Admin API to create a customer.
  2. Set acceptsMarketing to true in the request.
  3. Shopify will store this new customer, and marketing tools can then use it.
Example Request to Create a Customer via Admin API:

 

POST /admin/api/2024-01/customers.json

{

  "customer": {

    "first_name": "Newsletter",

    "last_name": "Subscriber",

    "email": "visitor@example.com",

    "accepts_marketing": true

  }

}

 

Limitations: This will add visitors to your customer list, which may not always be ideal unless you want to manage them as contacts in Shopify.

Which Method Should You Use?

  • If you're already using an email marketing app (Klaviyo, Mailchimp, etc.), integrating directly with their API is the best approach—it’s cleaner and avoids unnecessary customer creation in Shopify.
  • If you want all data inside Shopify, then creating a customer and setting acceptsMarketing to true works, but it might clutter your customer list with non-buyers.

If you need any other assistance, feel free to reply and I will try my best to help.
Best regards,
Daisy

Please let us know if our reply is helpful by giving it a Like or marking it as a Solution!

Avada SEO & Image Optimizer - The #1 SEO solution