I’m building a custom Back in Stock UX for a Shopify store using the latest Klaviyo API and I followed the JavaScript example in the docs @ https://developers.klaviyo.com/en/reference/create_client_back_in_stock_subscription.
Here is my fetch()
call:
const options = {
method: 'POST',
headers: {
accept: 'application/json',
revision: '2023-08-15',
'content-type': 'application/json'
},
body: JSON.stringify({
data: {
type: 'back-in-stock-subscription',
attributes: {
channels: e'EMAIL', 'SMS'],
profile: {
data: {
type: 'profile',
attributes: {
email: 'fetch.test@klaviyo-demo.com',
phone_number: '+15005550006'
}
},
email: 'fetch.test@klaviyo-demo.com',
phone_number: '+15005550006'
}
},
relationships: {
variant: {
data: {
type: 'catalog-variant',
id: '$shopify:::$default:::39986281939057'
}
}
}
}
})
};
fetch('https://a.klaviyo.com/client/back-in-stock-subscriptions/?company_id=PUBLIC_API_KEY', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
The result is a console error: SyntaxError: Unexpected end of JSON input
I do see that in my dev tools network tab that the response code is 202, which the documentation indicates as a successful request, however I do not see the profile get added to my client’s list of profiles (I did test with the old/deprecated API and that worked fine).
In the first then()
of my fetch()
call, if I do a quick console.log(response)
before returning the .json()
promise, I can actually see the response is JSON and the response.ok
property is true, but no matter what I try, the profile does not get added to my client’s list of profiles. I also tried this using the cURL
example and that worked right away, saw the profile show up almost immediately.
Any help on this would be much appreciated!