Changing from POST to PATCH

We have an API running for leads from a provider but if the contact is already in the system it trips over and does nothing.

The live chat advised that i need to update the API from POST to PATCH so that it updates a contact if its in the system - however they were not able to tell me how to do this.

I didnt create the API it was done by a previous employee and im just trying to rectifdy an issue as we are losing leads.

Is anyone able to help me?

Hi there @HDilloway

Thanks for reaching out — I can see why this is urgent if leads are getting dropped when a contact already exists.

I’d like to invite some community members who are subject matter experts in HubSpot APIs and contact upserts to join this conversation.
@BrandonWoodruff @evaldas @kosalaindrasiri — hope you’re all having a great week! Can you share the best-practice way to change a create-only flow (POST) into an update-or-create pattern (PATCH/upsert) for contacts, and what endpoints/payload structure they should use to avoid duplicates and failed lead creation?

Any guidance would be greatly appreciated.
Best,
Victor

Hello,

I would suggest trying the batch upsert: Accounts Dashboard | HubSpot

The batch allows you to still do one at a time, and it can handle creating or updating based on the email:

const options = {
method: ‘POST’,
headers: {Authorization: ‘Bearer <token>’, ‘Content-Type’: ‘application/json’},
body: JSON.stringify({
inputs: [
{
id: ‘<string>’,
properties: {},
idProperty: ‘<string>’,
objectWriteTraceId: ‘<string>’
}
]
})
};

fetch(‘https://api.hubapi.com/crm/v3/objects/contacts/batch/upsert’, options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));

{
“completedAt”: “2023-11-07T05:31:56Z”,
“results”: [
{
“archived”: true,
“createdAt”: “2023-11-07T05:31:56Z”,
“id”: “<string>”,
“new”: true,
“properties”: {},
“updatedAt”: “2023-11-07T05:31:56Z”,
“archivedAt”: “2023-11-07T05:31:56Z”,
“objectWriteTraceId”: “<string>”,
“propertiesWithHistory”: {},
“url”: “<string>”
}
],
“startedAt”: “2023-11-07T05:31:56Z”,
“status”: “CANCELED”,
“errors”: [
{
“category”: “<string>”,
“context”: {},
“errors”: [
{
“message”: “<string>”,
“code”: “<string>”,
“context”: “{missingScopes=[scope1, scope2]}”,
“in”: “<string>”,
“subCategory”: “<string>”
}
],
“links”: {},
“message”: “<string>”,
“status”: “<string>”,
“id”: “<string>”,
“subCategory”: {}
}
],
“links”: {},
“numErrors”: 10,
“requestedAt”: “2023-11-07T05:31:56Z”
}

Let me know if this helps, or if you have any other questions!


:check_mark: Was I able to help answer your question? Help the community by marking it as a solution.

Brandon Woodruff
Senior Software Developer @ PearagonStill have questions? Reach out at brandon@pearagon.com

Thanks @BrandonWoodruff - this might be a stupid response but where do i put that?
im very new to dealing with this sort of thing
Im looking in the Legacy Apps what we have created for this lead provider but not sure where to go from there