Subscribing users to newsletter from sign up form

Hey there, how would I go about signing users up for my Ghost newsletter upon checking a box on a sign up form on my website? I’ve been looking into this for an hour or so and can’t seem to find any documentation on it, any help is much appreciated.

Something like this:

<form data-members-form>
  ...
  <label>
    Newsletter Name
    <input data-members-newsletter type="checkbox" value="Newsletter Name" />
  </label>
  <label>
    Newsletter Two
    <input data-members-newsletter type="checkbox" value="Newsletter Two" />
  </label>
  
  <button type="submit">Subscribe</button>
</form>

See Ghost Theme Development: Building custom membership flows

Hey Cathy, wouldn’t that only work for my Ghost page, I want to be able to subscribe users to the newsletter on an external page.

Ah, I’d missed that requirement.

You’ll want to construct a post to the Ghost members endpoint /members/api/send-magic-link/ that includes the newsletter’s name. Then when the user submits the form, use a javascript event listener to make the POST below.

That post object looks something like this:

{
  "email": "me+teststringsoIcanmakemoreaccounts@gmail.com",
  "emailType": "subscribe",
  "labels": [],
  "autoRedirect": true,
  "urlHistory": [
    --bunch of stuff removed -- 
  ],
  "newsletters": [
      {"name": "Cathy's second newsletter" },{ "name": "My other newsletter" }
  ]
}