APIs & Integrations

Gavin_J
Participant

C# RestSharp library Add Contact To List Example

Hi I'm new to HubSpot,

Could I have an  example of adding a contact to a list using RestSharp C#

 

        private void AddContactToList()
        {
            var listId = "226468";
            var hapikey = "demo";
            var client = new RestClient("https://api.hubapi.com/");
            var request = new RestRequest("contacts/v1/lists/{listId}/add", Method.POST);
            request.AddUrlSegment("listId", listId);
            request.AddQueryParameter("hapikey", hapikey);
            request.RequestFormat = DataFormat.Json;

            request.AddJsonBody(new
            {
                vids = new[]
                {
                    new{property="vids",value="3057124"},
                    new{property="vids",value="3057124"},
                    new{property="emails",value="testingapis@hubspot.com"},
                }
            });

            IRestResponse response = client.Execute(request);
        }
0 Upvotes
4 Replies 4
himanshurauthan
Thought Leader | Elite Partner
Thought Leader | Elite Partner

C# RestSharp library Add Contact To List Example

Hello @Gavin_J ,

 

Can you try this format once?

var listId= "226468";
var hapiKey = "{YOUR_HAPI_KEY_HERE}";

var client = new RestClient("https://api.hubapi.com/");
var request = new RestRequest("contacts/v1/lists/{listId}/add", Method.POST);
request.AddUrlSegment("listId", listId);
request.AddQueryParameter("hapikey", hapiKey);
request.RequestFormat = DataFormat.Json;

request.AddBody(new {
vids = new[] {
new { "3057124" },
}
});

IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

Console.ReadLine();

 

Thanks.

Digital Marketing & Inbound Expert In Growth Hacking Technology
0 Upvotes
Gavin_J
Participant

C# RestSharp library Add Contact To List Example

I noticed I had "POST" instead of "PUT", I'm not getting an error but I do not see the changes. Do you see anything that I am missing?

To add a client to a list:

        private void AddContactToListAddContactToList()
        {

            var listId = "1";
            var hapikey = "{YOUR_HAPI_KEY_HERE}";
            var client = new RestClient("https://api.hubapi.com/");
            var request = new RestRequest("contacts/v1/lists/{listId}/add", Method.PUT);
            request.AddUrlSegment("listId", listId);
            request.AddQueryParameter("hapikey", hapikey);
            request.RequestFormat = DataFormat.Json;

            request.AddJsonBody(new
            {

                vids = new[]{
                   new{property="vids",value="1301"}
                }
            });

            IRestResponse response = client.Execute(request);
            Debug.Write(response.Content);
        }

To Update a company:

 

    private void UpdateCompanyProperty(string companyId)
        {
      
            var hapikey = "{YOUR_HAPI_KEY_HERE}";
            var client = new RestClient("https://api.hubapi.com/");
            var request = new RestRequest("companies/v2/companies/{companyId}", Method.PUT);
            request.AddUrlSegment("companyId", companyId);
            request.AddQueryParameter("hapikey", hapikey);
            request.RequestFormat = DataFormat.Json;

            request.AddJsonBody(new
            {
                properties = new[]
                {
                    new{property="name",value="ZZTop"},
                    new{property="value",value="My description"}
                }
            });

            IRestResponse response = client.Execute(request);

            JObject jObject = JObject.Parse(response.Content);
            JToken jvid = jObject["portalId"];

            Debug.WriteLine(jvid);

        }

 

0 Upvotes
Gavin_J
Participant

C# RestSharp library Add Contact To List Example

I create a list and it looks like the ID is 1 is this the correct number?

I'm getting an error:

{"status":"error","message":"Invalid input JSON on line 1, column 10: Can not deserialize instance of java.lang.Long out of START_OBJECT token","correlationId":"82172630-ed10-46db-b87f-bf2804838995","requestId":"33d218a88fd99fe221001b85507f1462"}

 

            var listId = "1";
            var hapikey = "{YOUR_HAPI_KEY_HERE}";
            var client = new RestClient("https://api.hubapi.com/");
            var request = new RestRequest("contacts/v1/lists/{listId}/add", Method.POST);
            request.AddUrlSegment("listId", listId);
            request.AddQueryParameter("hapikey", hapikey);
            request.RequestFormat = DataFormat.Json;

            request.AddJsonBody(new
            {

                vids = new[]{
                   new{property="vids",value="1301"}
                }
            });

            IRestResponse response = client.Execute(request);
            Debug.Write(response.Content);
0 Upvotes
himanshurauthan
Thought Leader | Elite Partner
Thought Leader | Elite Partner

C# RestSharp library Add Contact To List Example

Hello @Gavin_J 

 

Can you please let me know the error/ response that you are receiving?

 

Thanks

Digital Marketing & Inbound Expert In Growth Hacking Technology
0 Upvotes