Add Company to contact using api

companies

#1

Hello

I have created contact using contact api, and i created the company name using company api, now i want to add that company name into contact using api.

i was using this api “https://api.hubapi.com/companies/v2/companies/39238082/contacts/270146?hapikey=demo’”

But i couldn’t add the company name into contact, please save me thanks in advance…


#2

@developer You are using the demo hapikey. You need to switch that out for your own.


#3

Thanks for your reply…

I am not using “demo” api that one was sample link. now i am explain what i want

Using the below code i can able to create a new contact

            'properties' => array(
                array(
                    'property' => 'email',
                    'value' => 'apitest@hubspot.com'
                ),
                array(
                    'property' => 'firstname',
                    'value' => 'hubspot'
                ),
                array(
                    'property' => 'lastname',
                    'value' => 'user'
                ),
                array(
                    'property' => 'phone',
                    'value' => '555-1212'
                )
            )
        );			
			
			$post_json = json_encode($arr);
			$hapikey='286f936c-7963-43e6-8c59-c8a3f5f434d3';
			$endpoint = 'https://api.hubapi.com/contacts/v1/contact/?hapikey='.$hapikey;
			//$endpoint = 'https://api.hubapi.com/companies/v2/companies?hapikey='.$hapikey;
						
			$ch = @curl_init();
			@curl_setopt($ch, CURLOPT_URL, $endpoint);
			@curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			@curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
			@curl_setopt($ch, CURLOPT_POST, true);
			@curl_setopt($ch, CURLOPT_POSTFIELDS, $post_json);
			@curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			$response = @curl_exec($ch);
			$status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
			$curl_errors = curl_error($ch);
			@curl_close($ch);```

**After creating a new contact i can create a new company name using below code**

```$arr = array(
            'properties' => array(
                array(
                    'name' => 'name',
                    'value' => 'Testing Developer Company'
                ),
                array(
                    'name' => 'description',
                    'value' => 'A company description'
                )
            )
        );			
			
			$post_json = json_encode($arr);
			$hapikey='286f936c-7963-43e6-8c59-c8a3f5f434d3';
			$endpoint = 'https://api.hubapi.com/companies/v2/companies?hapikey='.$hapikey;
						
			$ch = @curl_init();
			@curl_setopt($ch, CURLOPT_URL, $endpoint);
			@curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			@curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
			@curl_setopt($ch, CURLOPT_POST, true);
			@curl_setopt($ch, CURLOPT_POSTFIELDS, $post_json);
			@curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			$response = @curl_exec($ch);
			$status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
			$curl_errors = curl_error($ch);
			@curl_close($ch);
			$comp_response = json_decode($response);
			echo "<pre>"; print_r($comp_response); echo "</pre>";

My concern is how to add the newly created company name into contact using api ? can i have a code to add the company name to contact ? please help me.
Thanks in advance.


#5

@developer you can add a contact to a company by using the Add a contact to a company endpoint in the Company API.