0

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.

https://api.hubapi.com/contacts/v1/contact/?hapikey=3fd8a881-2859-4c85-bec8-690bd989a889&contact=[object%20Object]&email=testingapis@hubspot.com

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);
                });
Peter3
  • 2,549
  • 4
  • 20
  • 40
  • have you heard of PostMan? its a pretty cool free app that lets you do a lot of API stuff, I suggest you try it... https://www.getpostman.com/ – omarjmh Apr 22 '16 at 23:52
  • Yea I actually did stumble across this earlier today but I can't seem to make any Post calls with it only get calls. I am not sure how to format the JSON properly. it does seem like a cool app though – Peter3 Apr 22 '16 at 23:53
  • I can tell your JSON object has issues, try this: see what postman returns from the get, you need to send that same format back to hubspot but with the proper values..., you're close man... – omarjmh Apr 22 '16 at 23:54
  • I was trying to follow the API documentation which is this { "properties": [ { "property": "email", "value": "testingapis@hubspot.com" }, { "property": "firstname", "value": "Adrian" }, { "property": "lastname", "value": "Mott" }........... – Peter3 Apr 22 '16 at 23:57

1 Answers1

0

Your JSON is incorrect, no need to "stringify" and then parse it.... just send an object like so:

I know you have seen this page: http://developers.hubspot.com/docs/methods/contacts/create_contact

try to copy exactly the object they have on that page, do not modify it other than taking off the properties you dont need to send...

{
            "properties": [
                {
                    "property": "email",
                    "value": "testingapis@hubspot.com"
                },
                {
                    "property": "phone",
                    "value": "555-122-2323"
                },
                {
                    "property": "address",
                    "value": "25 First Street"
                },
                {
                    "property": "city",
                    "value": "Cambridge"
                },
                {
                    "property": "state",
                    "value": "MA"
                },
                {
                    "property": "zip",
                    "value": "02139"
                }
            ]
        }
omarjmh
  • 13,632
  • 6
  • 34
  • 42
  • so using this gives me a syntax error. is having the contact= and email= even right? I am just guessing at that. var testContact = { "properties": [ { "property": "email", "value": "testingapis@hubspot.com" }, { "property": "firstname", "value": "Adrian" }, { "property": "lastname", "value": "Mott" }]}; var test = JSON.parse(testContact); – Peter3 Apr 23 '16 at 00:08
  • Yes the link you posted was what I had originally posted above. My issue is it doesn't share how to properly format email and contact. For the key it shows you in the example. For all i know the contact= and email= could be wrong. – Peter3 Apr 23 '16 at 00:21
  • I can't say I understand this but I just got this to work. $.ajax({ type: "POST", url: "https://api.hubapi.com/contacts/v1/contact/?hapikey=demo", processData: false, contentType: 'application/json', data: JSON.stringify(testContact), success: function(r) {} }); – Peter3 Apr 23 '16 at 00:29