how to get a contact by email field

The scenario is like this. I need to update or create a contact through the api, but I need to determine whether the contact exists through the email field, so I don’t know whether to update the contact or create the contact, so I need the corresponding api

api site

I think you can use an email search.

Something like this:

/crm/v3/objects/contacts/search

{
 filterGroups: [
 {
 filters: [
 {
 operator: 'EQ',
 propertyName: 'email',
 values: 'email@email.com',
 },
 ],
 },
 ],
 limit: 1,
 }

And in the body you get something like this:

{
 "total": 1,
 "results": [
 {
 "id": "512",
 "properties": {
 "company": "Biglytics",
 "createdate": "2019-10-30T03:30:17.883Z",
 "email": "bcooper@biglytics.net",
 "firstname": "Bryan",
 "lastmodifieddate": "2019-12-07T16:50:06.678Z",
 "lastname": "Cooper",
 "phone": "(877) 929-0687",
 "website": "biglytics.net"
 },
 "createdAt": "2019-10-30T03:30:17.883Z",
 "updatedAt": "2019-12-07T16:50:06.678Z",
 "archived": false
 }
 ],
 "paging": {
 "next": {
 "after": "NTI1Cg%3D%3D",
 "link": "?after=NTI1Cg%3D%3D"
 }
 }
}

Using Id, you can update your record, if no records are found, you can create a new

Search by ID and specify email to be the custom ID. NodeJS example:

try {
 return await hubspotClient.crm.contacts.basicApi.getById(
 email,
 ['email'],
 undefined,
 undefined,
 false,
 'email'
 )
} catch(e) {
 // Will throw a 404 when no user so catch and return undefined (or ensure not 404 and throw)
 return undefined
}

}

I recommend that over the search API as there is a delay in the search engine population so if you try to search, create, search again you may find that the contact has yet to show up in the search resulting in a create error.