I am trying to set up a Post call in Javascript to create a contact in my hubspot account. I am new to Rest API and how this all works. This is what the documentation says about doing so.
Required Parameters How to use Description HubSpot API Key ----- hapikey=X ---- Used in the request URL The HubSpot API key for the portal that you're making the call for.
Contact JSON -----Used in the request body----- This is JSON that represents a contact that you're creating. This should be of the format seen below in the code sample given.
Email Address----- Used in the request body----- Please note that Email Address is a required part of the JSON that you POST to HubSpot when creating a new contact. Optional Parameters How to use Description
None None No optional parameters for this method.
Example URL to POST to: https://api.hubapi.com/contacts/v1/contact/?hapikey=demo
I get the example and I can make get calls using the hapikey=demo. What I don't understand is the Used in the Request Body. What does that fully mean and how do I implement it. I can't find any documentation on to actually load the JSON onto the post call.
Here is what I have
var testContact = '{"properties":['+
'{"property":"email","value":"testing@hubspot.com"},'+
'{"property":"firstname","value":"Peter"},'+
'{"property":"lastname","value":"xxxxx"}]}';
var test = JSON.parse(testContact);
//alert(test.properties[1].value);
//console.log(test.properties[1].firstname);
$.post("https://api.hubapi.com/contacts/v1/contact/?hapikey=demo&contact=" + testContact + "&email=" + test.properties[0].value,function(result){
console.log(result);
});
I keep getting a bad request from the server so it must be connecting and I am setting up the info wrong.
UPDATE
So i un "stringified it" and what is interesting when i try and pass that i just get an object and not the whole string.
var testContact = {
"properties": [
{
"property": "email",
"value": "testingapis@hubspot.com"
},
{
"property": "firstname",
"value": "Adrian"
},
{
"property": "lastname",
"value": "Mott"
}]};
$.post("https://api.hubapi.com/contacts/v1/contact/?hapikey=3fd8a881-2859-4c85-bec8-690bd989a889&contact=" + testContact + "&email=" + testContact.properties[0].value,function(result){
console.log(result);
});