APIs & Integrations

anasaldib
Participant

Get Contacts from Specific Custom Attribute Value Query via HubSpot API

SOLVE

Hi

We are trying to get contacts list with specific custom attribute value via HubSpot API, please guide us. 

 

Please I need a clear example in whatever language code (PHP, C#, Java) anything.

 

We stuck!!!!!!

 

 

 

http://aljewar.masaragency.com/Post.asp

 

 

<script language="javascript">
function SendPost(){
  var xhttp = new XMLHttpRequest();
  xhttp.onload = function() {
    if (this.readyState == 4 && this.status == 200) {
      var data = JSON.parse(this.response);
      alert(this.responseText);
    }
  };
  xhttp.open("POST", "https://api.hubapi.com/contacts/v1/lists?hapikey=xxxxxxxxx",true);
  xhttp.setRequestHeader("Content-Type", "application/json");
  var trinput = JSON.stringify({"name":"NEW LIST"});
  xhttp.send(trinput);
}
</script>
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.hubapi.com/contacts/v1/lists?hapikey=9fb77290-e1af-42cd-8b20-ce7f32ff28cf. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
0 Upvotes
1 Accepted solution
jackcoldrick
Solution
HubSpot Employee
HubSpot Employee

Get Contacts from Specific Custom Attribute Value Query via HubSpot API

SOLVE

Hi @anasaldib,

 

Please do not share your HubSpot API publicly in these forums. This should never be exposed as it can be used to push and pull data from your portal. I've since removed it from your original post.

 

The code you are share will not work because our APIs (with a few exceptions) do not support CORS requests. Therefore you need to implement a proxy to handle these requests and authenticate on the server side.

 

You mention you are trying to "Get a list with specific custom attribute values". Can you confirm if you want to "CREATE" a list of contacts or "GET" contacts in a specific list? The code you share is using the "Create a new contact list" endpoint. If that is the case then the following PHP code would work:

 

$apikey = "XXXX";
$data = array("name" => "HubSpot Email Addresses", "dynamic" => true, "filters" => array(array(array("operator" => "CONTAINS", "value" => "@hubspot", "property" => "email", "type" => "string"))));
$data_string = json_encode($data);      

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.hubapi.com/contacts/v1/lists?hapikey=$apikey",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS =>$data_string,
  CURLOPT_HTTPHEADER => array(
    "Content-Type: application/json"
  ),
));

$response = curl_exec($curl);
curl_close($curl);
echo $response;

 

The output of the above would be a new list created with the name "HubSpot Email Addresses" with a filter to show all contacts who have an email that CONTAINS "@hubspot".

 

Thanks,

Jack

 

 

Jack Coldrick
Solutions Engineer @ HubSpot
Add me on LinkedIn

View solution in original post

4 Replies 4
jackcoldrick
Solution
HubSpot Employee
HubSpot Employee

Get Contacts from Specific Custom Attribute Value Query via HubSpot API

SOLVE

Hi @anasaldib,

 

Please do not share your HubSpot API publicly in these forums. This should never be exposed as it can be used to push and pull data from your portal. I've since removed it from your original post.

 

The code you are share will not work because our APIs (with a few exceptions) do not support CORS requests. Therefore you need to implement a proxy to handle these requests and authenticate on the server side.

 

You mention you are trying to "Get a list with specific custom attribute values". Can you confirm if you want to "CREATE" a list of contacts or "GET" contacts in a specific list? The code you share is using the "Create a new contact list" endpoint. If that is the case then the following PHP code would work:

 

$apikey = "XXXX";
$data = array("name" => "HubSpot Email Addresses", "dynamic" => true, "filters" => array(array(array("operator" => "CONTAINS", "value" => "@hubspot", "property" => "email", "type" => "string"))));
$data_string = json_encode($data);      

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.hubapi.com/contacts/v1/lists?hapikey=$apikey",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS =>$data_string,
  CURLOPT_HTTPHEADER => array(
    "Content-Type: application/json"
  ),
));

$response = curl_exec($curl);
curl_close($curl);
echo $response;

 

The output of the above would be a new list created with the name "HubSpot Email Addresses" with a filter to show all contacts who have an email that CONTAINS "@hubspot".

 

Thanks,

Jack

 

 

Jack Coldrick
Solutions Engineer @ HubSpot
Add me on LinkedIn
anasaldib
Participant

Get Contacts from Specific Custom Attribute Value Query via HubSpot API

SOLVE

By using the above code we have successfully created a list using the standard property "Email" in the filter array as below:
"filters" => array(array(array("operator" => "EQ", "value" => "monem.soliman@gmail.com", "property" => "Email", "type" => "string")))

 

But we got the error message "The remote server returned an error: (400) Bad Request", when we were using the custom property "Integrated" in filter array as below:
"filters" => array(array(array("operator" => "EQ", "value" => "No", "property" => "Integrated", "type" => "string")))

0 Upvotes
anasaldib
Participant

Get Contacts from Specific Custom Attribute Value Query via HubSpot API

SOLVE

Any solution please

0 Upvotes
jackcoldrick
HubSpot Employee
HubSpot Employee

Get Contacts from Specific Custom Attribute Value Query via HubSpot API

SOLVE

Hi @anasaldib,

 

Can you please ensure the internal name for the "Integrated" property is defined correctly. The property is case sensitive so if you pass through "Integrated" but the internal name is "integrated" it will return a 400 bad request.

 

I was successfully able to create a list in my own portal by using the below:

 

 

POST:https://api.hubapi.com/contacts/v1/lists?hapikey={{apikey}}
{ "name": "Jacks Test List", "dynamic": true, "portalId": {{portalId}}, "filters": [ [ { "operator": "EQ", "value": "test@hubspot.com", "property": "email", "type": "string" }, { "operator": "EQ", "value": "No", "property": "integrated", "type": "string" } ] ] }

 

Jack Coldrick
Solutions Engineer @ HubSpot
Add me on LinkedIn
0 Upvotes