List api problem


#1

Hello, I'm using list api to insert contact into static list but although hubspot send me http 200 as response, my contact isn't entered into my list.
Here is my code:

private bool _addContactToList(int idlist, string email) {

  var endpoint = $"https://api.hubapi.com/contacts/v1/lists/{idlist}/add?hapikey=myapikey";

  var json = JsonConvert.SerializeObject(new { email = new List<string>() { email} });

  HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(endpoint);
  request.Method = "POST";
  request.ContentType = "application/json";

  using (var streamWriter = new StreamWriter(request.GetRequestStream())) {
    streamWriter.Write(json);
    streamWriter.Flush();
    streamWriter.Close();
  }

  HttpWebResponse response = (HttpWebResponse)request.GetResponse();

  string target = "";

  StreamReader streamReader = new StreamReader(response.GetResponseStream(), true);
  try
  {
    target = streamReader.ReadToEnd();
  }
  finally
  {
    streamReader.Close();
  }

  if (response.StatusCode == HttpStatusCode.OK) return true;

  return false;
  
}

My request body is: {"emails":["myemail@gmail.com"]}
And hubspot response body is this: {"updated":[],"discarded":[],"invalidVids":[],"invalidEmails":[]}
I also tried to encode my json into ASCII and send it as byte array but nothing happened


#2

Hi @guidifra,

Can you send me a link to the list in HubSpot you're trying to add contacts to?


#3

Yes @Derek_Gervais :
https://app.hubspot.com/lists/3393112/list/203/
https://app.hubspot.com/lists/3393112/list/204/
https://app.hubspot.com/lists/3393112/list/206/
https://app.hubspot.com/lists/3393112/list/207/


#4

Hi @guidifra,

Very strange; I created myself as a test contact and was able to successfully add myself to one of those lists (the first one listed above). Do the emails you're including in the POST body exist as contacts? Is it possible that the request body is somehow getting malformed? The response you mentioned is what I would expect to see if we received a request with out any vids or emails.


#5

Hi @Derek_Gervais,
Yes, the email I use is one of an existing contact because first of all I create a new contact with form api successfully and then I use the same email to put it into my list.


#6

Hi @guidifra,

How much time is there between when the form submission is sent to HubSpot and when you make the request to add the contact to the list? I'm wondering if perhaps there's a race condition where the contacts backend hasn't had enough time to finish creating the contact when it receives your request to add the contact to the list?


#7

Hi @Derek_Gervais, the time between form submission and request to add contact to the list is less then 1secs.. I'd try to run list add function after few minutes