CMS Development

andrepln
Participant

Calling Hubspot API in Landing Page

Hello guys!

I'm having trouble to call companies search API throug my HS Landing Page.

It's returning 401.

 

When I execute it in Postman app, it returns OK!

But when I try to use Landing Page script, it not works. It tried also in JSFiddle but it returns same erros.

 

Chrome: RequestsChrome: Requests

 

Chrome: 1st RequestChrome: 1st Request

 

Chrome: 2nd RequestChrome: 2nd Request

 

 

$(document).ready(function() {
    var settings = {
        "url": "https://api.hubapi.com/crm/v3/objects/companies/search?hapikey=MY_HS_KEY",
        "method": "POST",
        "headers": {
            "Content-Type": "application/json",
        },
        "data": JSON.stringify({"limit":20,"properties":["name","website","instagram_da_empresa","phone","bairro_endereco_empresa","cidade_endereco_empresa","estado_endereco_empresa"],"filterGroups":[{"filters":[{"propertyName":"estado_endereco_empresa","operator":"EQ","value":"EX"},{"propertyName":"cidade_endereco_empresa","operator":"CONTAINS_TOKEN","value":"Abadiânia"},{"propertyName":"bairro_endereco_empresa","operator":"CONTAINS_TOKEN","value":"BAIRRO "}]}],"sorts":[{"propertyName":"hs_lastmodifieddate","direction":"DESCENDING"}]}),
    };

    $.ajax(settings).done(function (response) {
        console.log(response);
    });
});
0 Upvotes
3 Replies 3
KimM
Top Contributor

Calling Hubspot API in Landing Page

Did you end up finding a solution to this @andrepln ? If so could you provide a bit more info as to how you went about this? I'm trying to do a similar thing currently and coming up with issues. 

0 Upvotes
doneill
HubSpot Employee
HubSpot Employee

Calling Hubspot API in Landing Page

Hey @andrepln!

 

Thanks for reaching out!

 

Could you please provide the full error response you are receiving? Also, it looks like you are trying to use an AJAX request with a HubSpot API. As you can see here that is not supported with the HubSpot API. The workaround the documentation states is:
In order to use JavaScript/AJAX, you would need to make the request (excluding any authentication) to an external server that could then add the needed authentication and make requests to HubSpot's APIs server-side.

Could you try to implement that? If you have more details please send them on and we can take a look!

0 Upvotes
andrepln
Participant

Calling Hubspot API in Landing Page

Thanks for reply, @doneill !

You suggest that I create a 3rd app, hosted on my server, to receive request and call HS API?

0 Upvotes