I’m trying to figure out how to set a contact owner automatically when creating new contacts through the Hubspot API. I know this can be done with Workflows, but I want to do it directly through the API.
Here’s my current code that creates contacts, but it doesn’t set the owner:
$contactInfo = [
'properties' => [
['property' => 'given_name', 'value' => $person->first_name],
['property' => 'surname', 'value' => $person->last_name],
]
];
$jsonData = json_encode($contactInfo);
$apiResponse = $hubspotClient->send('POST', '/contacts/v1/contact/email/'.$person->email+'/profile',
['query' => ['apikey' => $hubspot_apikey, 'body' => $jsonData]);
How can I modify this to include setting the contact owner? Any help would be appreciated!
I’ve actually tackled this issue before in one of my projects. While the previous answer is correct about using the ‘hubspot_owner_id’ property, there’s a bit more to consider for a robust solution.
First, you might want to fetch the owner ID dynamically rather than hardcoding it. This allows for more flexibility, especially if you’re assigning different owners based on certain criteria.
Here’s a snippet that demonstrates this:
$ownerEmail = '[email protected]';
$owner = $hubspotClient->owners()->getByEmail($ownerEmail);
$ownerId = $owner->getId();
$contactInfo = [
'properties' => [
['property' => 'given_name', 'value' => $person->first_name],
['property' => 'surname', 'value' => $person->last_name],
['property' => 'hubspot_owner_id', 'value' => $ownerId]
]
];
This approach ensures you’re always using a valid, up-to-date owner ID. It’s also worth noting that if the owner assignment fails for any reason, HubSpot will typically default to the API key owner. So, make sure your error handling accounts for this possibility.
I’ve implemented this in a recent project and found that using the Contacts API v3 provides more flexibility and better performance. Here’s an updated approach:
$properties = [
'firstname' => $person->first_name,
'lastname' => $person->last_name,
'email' => $person->email,
'hubspot_owner_id' => $ownerId
];
$contactInput = new SimplePublicObjectInput(['properties' => $properties]);
try {
$apiResponse = $hubspotClient->crm()->contacts()->basicApi()->create($contactInput);
$newContact = $apiResponse->getId();
} catch (ApiException $e) {
// Handle error
}
This method is more streamlined and aligns with HubSpot’s current best practices. Remember to properly handle potential API exceptions for a robust implementation.
hey, i’ve dealt with this before. you gotta add the ‘hubspot_owner_id’ property to ur contactInfo array. like this:
$contactInfo = [
'properties' => [
['property' => 'given_name', 'value' => $person->first_name],
['property' => 'surname', 'value' => $person->last_name],
['property' => 'hubspot_owner_id', 'value' => $ownerId]
]
];
just make sure u have the $ownerId variable set to the right owner’s ID first. good luck!
To set the contact owner when creating a new contact via the HubSpot API, you’ll need to include the ‘hubspot_owner_id’ property in your contact creation request. Here’s how you can modify your existing code:
$contactInfo = [
'properties' => [
['property' => 'given_name', 'value' => $person->first_name],
['property' => 'surname', 'value' => $person->last_name],
['property' => 'hubspot_owner_id', 'value' => 'OWNER_ID_HERE']
]
];
Replace ‘OWNER_ID_HERE’ with the actual HubSpot user ID of the desired owner. You can find this ID in the HubSpot UI or by using the Owners API endpoint. Remember to ensure the owner ID is valid, or the API call may fail. This approach should automatically assign the specified owner to the newly created contact without relying on Workflows.