I’m currently implementing a function within my company’s web dashboard to send all contacts that have verified their email address and logged in, over to Hubspot.
We’re using MEAN stack, and I’m making the call from Express to the Hubspot API using the API Key auth method.
We have a variety of custom fields that I’m looking to fill, and all of these are setup in Hubspot already. I’ve got the connection working with the example JSON displayed in the API docs, but when I try and use our custom fields, I’m getting a 400 Bad Request error.
Please see the code I’m using below:
function getMidnight(t) {
t = t.setHours(0,0,0,0);
return new Date(t).getTime();
}
var sendToCRM = function(req, res, form) {
console.log("Sending contact to Hubspot")
var user = form;
User.findOne({email: user.email}, function(erro,resul) {
if (resul) {
if (typeof resul.ContactId != "undefined") {
console.log("Already in CRM")
} else {
var path = "/contacts/v1/contact/";
var date = getMidnight(new Date());
var fdate = getMidnight(new Date(resul.firstLogin));
var email = resul.email;
/* THIS WORKS */
/* postBody = {
"properties": [
{
"property": "email",
"value": "testingapis@hubspot.com"
},
{
"property": "firstname",
"value": "Adrian"
},
{
"property": "lastname",
"value": "Mott"
},
{
"property": "website",
"value": "http://hubspot.com"
},
{
"property": "company",
"value": "HubSpot"
},
{
"property": "phone",
"value": "555-122-2323"
},
{
"property": "address",
"value": "25 First Street"
},
{
"property": "city",
"value": "Cambridge"
},
{
"property": "state",
"value": "MA"
},
{
"property": "zip",
"value": "02139"
}
]
};
*/
/* THIS DOESN'T WORK */
var postBody = { "properties": [
{
"property": "email",
"value": email
},
{
"property": "firstname",
"value": resul.fname
},
{
"property": "lastname",
"value": resul.lname
},
{
"property": "gb_first_login",
"value": fdate
},
{
"property": "gb_last_login",
"value": date
},
{
"property": "gb_login_counter",
"value": resul.loginCounter
},
{
"property": "verification_email_counter",
"value": resul.verEmailCounter || 1
},
{
"property": "gb_referral",
"value": resul.ref
},
{
"property": "ipaddress",
"value": (req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress)
}
]
};
api.CRMRequest(path, postBody, function(statusCode, result)
{
if(result.error != 201 && statusCode != 302 && statusCode != 200) {
console.log("ERROR: Problem in CRM Post")
console.log(result)
return false;
}else {
console.log(result);
var leadId = result['identity-profiles'].vid;
User.findOne({email: email}, function(err, user2) {
if (err) {
return false;
} else {
if (user2) {
if (user2.email == email) {
var obj = {};
obj.ContactId = leadId;
user2.update(obj, function(err2, resul2) {
if (err2) {
console.log("ERROR: Adding CRM ID for "+email);
return false;
}
return true;
})
} else {
return false;
}
}
}
});
return true;
}
});
}
} else {
console.log("ERROR: User " + user.email + " not in Database");
}
});
}
At this point I’m at a loss - both JSON objects run through JSONLint fine, and the only error I manage to get our of this is the 400 Bad Request, which makes me think that it’s a malformed postBody, but my logs show it being identically formed.
EDIT: Here is the output of my logging for a working call (API Key obscured for obvious reasons):
@pmanca Removing the IP address field was what fixed the issue for me a couple days ago. I don’t have the log file handy anymore, but the only change was me removing the:
@gamebenchjake I am thinking for the contact object the ip address is a read only property and can’t be updated. Unlike the forms API which allows you to do so.
Looks like the issue was caused by my trying to use the “ipaddress” field - I created a custom one and it worked perfectly. Strange that nothing in the docs mentions this issue.
The error status code was a 400, and the message was “Bad Request”, a pretty generic error which is what made debugging it a bit of a pain (ended up just starting with First name, Last name, and Email and adding in the fields until it broke.)
I also tried with a couple random IPs just in case there was any validation parsing to no avail.