APIs & Integrations

soniarottenberg
Member

API error get companies with their properties

Hello, 

 

I'm currently trying to get all companies using the API. 

Also, I want to get 2 specifics properties. 

I'm facing 2 issues at the moment.

1) I'm trying to limit the number of response without success

2) I get a "unexpected token" error

SyntaxError: Unexpected token < in JSON at position 0

 

I tried as much to stick with the documentation.  

Here's the piece of code I use : 

var request = require("request")
const returnedCompanies = [];
const API_KEY = 'my-api-key'
const count = 5;

function getCompanies(offset) {
    if (typeof offset == 'undefined') {
        offsetParam = null;
    } else {
        offsetParam = `offset=${offset}`;
    }
    const hapikeyParam = `hapikey=${API_KEY}`
    const paramsString = `?count=${count}&${hapikeyParam}&${offsetParam}&properties=name&properties=deja_equipe_ou_non&limit=2`;

    const finalUrl = `https://api.hubapi.com/companies/v2/companies/paged${paramsString}`
    console.log(finalUrl)
    request(finalUrl, (error, response, body) => {
        if (error) {
            console.log('error', error)
            throw new Error
        }
        const parsedBody = JSON.parse(body)
        parsedBody.companies.forEach(company => {
            returnedCompanies.push(company);
        });
        if (parsedBody['has-more']) {
            getCompanies(parsedBody['offset'])
        } else {
            //print out all companies
            console.log(returnedCompanies)
        }
    })
};

getCompanies()

Any help would be appreciated ! 

 

Best regards 

0 Upvotes
5 Replies 5
WendyGoh
HubSpot Employee
HubSpot Employee

API error get companies with their properties

Hey @soniarottenberg,

 

When looking at your code, nothing major stood out. I would expect it to work just fine and in order for me to further dig into this, could you share with me the HubSpot portal that you're looking to use the get all companies endpoint?

0 Upvotes
soniarottenberg
Member

API error get companies with their properties

Hello @WendyGoh 

Sorry for the late reply. 

By portal, you me my Hubspot account ? 

If so it's 4352625. 

 

Thank you for your help ! 

0 Upvotes
WendyGoh
HubSpot Employee
HubSpot Employee

API error get companies with their properties

Hey @soniarottenberg,

 

Thanks for sharing the details across. 

 

I tried using the same request url on your portal and I was able to received a 200 response. Could you try using Postman to see if the following request url works?

 

https://api.hubspot.com/companies/v2/companies/paged?hapikey={{hapikey}}&count=5&offsetParam=null&properties=name&properties=deja_equipe_ou_non&limit=2

Additionally, could you ensure that you're only running this piece of code so that we can narrow down the issue?

0 Upvotes
soniarottenberg
Member

API error get companies with their properties

@WendyGoh I just tried on Postman and I works fine (see screenshot below). 

On the other hand I tried again my code and I takes forever. 

HubSpot_Community_-_Re__API_error_get_companies_with_their_properties_-_HubSpot_Community.png

0 Upvotes
WendyGoh
HubSpot Employee
HubSpot Employee

API error get companies with their properties

Hey @soniarottenberg,

 

One note: This is a public forum, so you should refrain from posting any sensitive data, particularly authorization keys (e.g. API keys, OAuth tokens, etc.). I've removed your hapikey from the image, but I would strongly recommend deactivating it and generating a new one since it was visible on the forums for a while.

 

By taking forever do you mean that after running the code, the endpoint isn't returning back a response for a long period of time? Even with both the parameter limit=2 and count=5? In this case, are you still seeing the error - SyntaxError: Unexpected token < in JSON at position 0?

0 Upvotes