I’m having trouble making a POST request to the Prosperworks API using JavaScript fetch inside Zapier Code. The same request works perfectly when I use curl, but I can’t get it working with fetch. Here’s my current code:
fetch('https://api.prosperworks.com/developer_api/v1/contacts/search_by_email', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-PW-AccessToken': 'YOUR_API_KEY',
'X-PW-Application': 'developer_api',
'X-PW-UserEmail': 'USER_EMAIL'
},
body: JSON.stringify({'email_address': inputData.email})
}).then(function(response) {
var contact_id = response.id;
return response.json();
}).then(function(data) {
callback(null, {id: 5678, rawHTML: data});
}).catch(function(err) {
callback("request failed");
});
Can anyone help me figure out what’s wrong with this fetch implementation? I’ve been stuck on this for hours and would really appreciate any guidance.
u might be trying to access response.id b4 parsing the json - that def won’t work. and check ur api key too. i had similar issues cuz my token was expired. adding some console.log statements can help u see the actual response from the api.
I hit this same issue with the Prosperworks API in Zapier. You’re trying to access response.id before parsing the JSON - that won’t work. The fetch response doesn’t have the API data directly, you need to parse it first. Also check response.ok before parsing JSON since the API might return error responses that aren’t valid JSON. Double-check your API credentials too - expired auth can cause silent failures in Zapier Code.
I encountered a similar problem with using fetch in Zapier Code. The main issue is accessing response.id before converting the fetch response to JSON. You need to ensure you call response.json() first to obtain the actual API data. Here’s an updated version of your promise chain: fetch(‘https://api.prosperworks.com/developer_api/v1/contacts/search_by_email’, { method: ‘POST’, headers: { ‘Content-Type’: ‘application/json’, ‘X-PW-AccessToken’: ‘YOUR_API_KEY’, ‘X-PW-Application’: ‘developer_api’, ‘X-PW-UserEmail’: ‘USER_EMAIL’ }, body: JSON.stringify({‘email_address’: inputData.email}) }).then(function(response) { return response.json(); }).then(function(data) { var contact_id = data.id; callback(null, {id: contact_id, rawHTML: data}); }).catch(function(err) { callback(err); }); Additionally, it’s good practice to check response.ok before parsing the JSON to handle any API errors effectively.