Solved

how to use form element to post member to a List

  • 31 March 2022
  • 4 replies
  • 1646 views

Badge +2

I want use a html form element to submit the subscribers like this:

<form action="https://a.klaviyo.com/api/v2/list/VMd343/members?api_key=U12345" method="post" target="_blank">
<input type="email" name="EMAIL" placeholder="Email">
<button type="submit">Sign In</button>
</form>

But when I test to submit the form, I got an error:

{"detail":"CSRF Failed: <HttpResponseForbidden status_code=403, \"text/html\">"}

Does the development API not be supported in Frontend?

Could anyone give a hand? thanks a lot!

icon

Best answer by Dov 1 April 2022, 16:44

View original

4 replies

Userlevel 7
Badge +61

Hi @jeremyfix,

Thanks for sharing this with us.

You are correct, that endpoint has to be executed server side.

The base URL to use is https://manage.kmail-lists.com/ajax/subscriptions/subscribe which will add a user's email (and additional user properties) to any list in Klaviyo. 

This is different than the normal subscribe URL used in Klaviyo's own forms. Below is a break down of POST body properties to pass.

  • g (required) - LIST_ID
  • email (required) - User's email address
  • $fields (optional) - This tells the Klaviyo backend which keys (additional users properties) to expect in the POST request. All properties (other than email) need to be passed as a value under $fields and as it's own key.
  • $list_fields (optional) - This inherits form the $fields but would then would take a list of data and transpose it as a custom property in list format
  • other properties (optional) - For each property in $fields, you want to pass a key/value pair like below:
    • $first_name: John
    • $last_name: Smith
    • Custom Property 1: Custom Value 1

    • Custom Property 2: Custom Value 2
      var settings = {
      "async": true,
      "crossDomain": true,
      "url": "https://manage.kmail-lists.com/ajax/subscriptions/subscribe",
      "method": "POST",
      "headers": {
      "content-type": "application/x-www-form-urlencoded",
      "cache-control": "no-cache"
      },
      "data": {
      "g": "{{LIST_ID}}",
      "$fields": "$first_name,$last_name,Favorite Color,Favorite Book,$source",
      "email": "{{email}}",
      "$first_name": "{{first_name}}",
      "$last_name": "{{last_name}}",
      "Favorite Color": "{{favorite_color}}",
      "Favorite Book": "{{favorite_book}}",
      "$source": "Custom Form"
      }
      }
      $.ajax(settings).done(function (response) {
      console.log(response);
      });

      The snippet above uses {{ }} placeholder syntax, which will need to be altered for your platform. The important part is that product fields are dynamically rendered based on which product page you're viewing.

Response

200 Success: Request was successful
400 BAD REQUEST: This happens when the email already exists.
405 METHOD NOT ALLOWED: required fields missing from the POST request

Special Properties

$first_name, $last_name, and $source are special Klaviyo properties. Here is a list of all Special Klaviyo Properties for Users:

  • $id - your unique identifier for a person
  • $email - email address
  • $first_name - first name
  • $last_name - last name
  • $phone_number - phone number$
  • $title -title at their business or organization
  • $organization - business or organization they belong to
  • $city - city they live in
  • $region - region or state they live in
  • $country - country they live in
  • $zip - postal code where they live
  • $image - url to a photo of the person
  • $source - What form the user signed up with

I hope that helps.

Badge +2

@Dov Great help!

Finally I find the tutorial here https://help.klaviyo.com/hc/en-us/articles/115005080167-How-to-Redirect-Existing-Signup-Forms-to-Klaviyo

Thanks 😀

Hi @jeremyfix, the tutorial you shared above is no longer available, any chance you could give me a few details on where to implement the code? I’m trying to subscribe to my newsletter all visitors that sign up for an account on my shop. 

Thanks a lot! 😊

Userlevel 7
Badge +36

Hi @sophiedupau!

 

Here’s a Community thread where I discuss the contents of that original tutorial:

 

Best,

Brian

Reply