Solved

CORS policy: No 'Access-Control-Allow-Origin' from Shopify Store

  • 4 December 2022
  • 3 replies
  • 232 views

Badge

Hi, I am currently using Klaviyo javascript client to do OnSite Activity: 
I have tried to unsubscribe a profile from a list using below code structure:

const options = {
method: 'DELETE',
headers: {'content-type': 'application/json'},
body: JSON.stringify({
emails: email
})
};

fetch('https://a.klaviyo.com/api/v2/list/LIST_ID/subscribe?api_key=PRIVATE_API_KEY', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));

  Unfortunately, I am getting CORS error, even though the site is made request over HTTPS.

 

How can I get resolve from this? Or How can I unsubscribe from a klaviyo list using JavaScript client?
Any help would be appreciate

icon

Best answer by Brian Turcotte 29 December 2022, 20:12

View original

3 replies

Userlevel 5
Badge +26

Hi @mohammadliton91 and welcome to the Community!

 

I see that you’re using the v2 List&Segments endpoint to accomplish the unsubscribe. I am going to check with our engineering team to see if they can provide some insight to this error, but in the meantime, have you considered using our new v3 endpoints?

 

The ‘unsubscribe profile from list’ action can be completed using our updated Lists endpoint, with this format:

const options = {
method: 'DELETE',
headers: {
accept: 'application/json',
revision: '2022-10-17',
'content-type': 'application/json',
Authorization: 'Klaviyo-API-Key your-private-api-key'
}
};

fetch('https://a.klaviyo.com/api/lists/id/relationships/profiles/', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));

 

Also, here are some more Community threads dealing with similar topics, if you’re interested:

 

I will update the thread shortly, and thanks for using the Community!

 

- Brian

Badge

Hi Brian,
Thanks for your reply.

I’ve tried with your given updated endpoint but still getting the same error :’( 
 

 

Userlevel 5
Badge +26

Hi@mohammadliton91!

 

Sorry for my delay in getting back to this thread, but I’ve actually found the cause of this issue. The v2 lists & segments endpoint is server-side only, so you can only call client-side using the ajax endpoint like in this example subscribe request below:

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",
"email": "email@address.com",
//pass in additional fields (optional)
"$fields": "$source, $first_name, $last_name",
"$source": "Account Creation",
"$first_name": firstname,
"$last_name": lastname
}
};
$.ajax(settings).done(function(response) {
console.log(response);
})

 

Additionally, it turns out that this endpoint only accepts POST requests, so you actually cannot unsubscribe profiles from a list using this endpoint - explaining the error.

 

You can utilize a specific list's unsubscribe page for this purpose if you wish, by navigating to the List in your account and then clicking Settings to find the url for the unsubscribe page.

 

I hope this helps to clarify, and thanks for using the Community!

 

- Brian

Reply