Python: How do I create an Email with associations in one call?

According to the API documentation on Emails (Accounts Dashboard | HubSpot), it is possible to create an email with properties AND associations in 1 call.
I’m using the official python client (latest version as of 21 Apr 2023), and using the `hs.crm.objects.emails.basic_api.create(input)` call to create my emails.
The `create` method accepts one parameter, of type `hubspot.crm.objects.emails.SimplePublicObjectInput`, but this object only accepts `properties` as a parameter, no `associations`.
As a result, I’m having to make 1 call to create the email, then 1 call to create an association to the ticket.
It takes a lot of time, and a lot of API calls.
I would much rather use the email batch API, and create all my emails at a go, along with their associations.
Could you please let me know how to do that with the official python client?
Thanks

Hi @wollivier,

Thank you for reaching out to the Community!

I wanted to tag in a couple of subject matter experts to see if they have any advice:

Hi @ChrisoKlepke, @taran42, do you have any tips for @wollivier? Thank you!!

Cheers
Mia, Community Team

Thank you for tagging me @MiaSrebrnjak .

Hey @wollivier,

it took me a minute, but I was able to test this successfully. The documentation apparently is a bit old in that regard. What you want to use is SimplePublicObjectWithAssociations and use the kwarg simple_public_object_input on the create method. Here is what that would more or less look like:

properties = {
 "hs_timestamp": "2019-10-30T03:30:17.883Z",
 "hs_email_direction": "EMAIL",
 "hs_email_status": "SENT",
 "hs_email_subject": "Let's talk",
 "hs_email_text": "Thanks for taking your interest let's find a time to connect",
}

associations = [
 {
 "to": {"id": "3301"},
 "types": [{"associationCategory": "HUBSPOT_DEFINED", "associationTypeId": 10}],
 }
]
foo = SimplePublicObjectWithAssociations(
 properties=properties, associations=associations
)

try:
 api_response = client.crm.objects.emails.basic_api.create(
 simple_public_object_input=foo
 )
 pprint(api_response)
except ApiException as e:
 print("Exception when calling basic_api->create: %s\n" % e)

I’ve tested this under version 7.5.0. Hope this helps, mate.

If you found this post helpful, consider helping others in the community to find answers faster by marking this as a solution. I’d really appreciate it.

Cheers,

Chriso

Thanks Chriso-mwx!
I can confirm it works flawlessly, and as a result I was able to halve the number of requests.
As a matter of fact, this also applies to the `batch_api` as well, so I was able to leverage creating emails and notes in batches, already associated to the correct tickets!
Good stuff!

Thank you for the wonderful feedback, mate! Glad I was able to help you out.

Have fun developing,

Chriso