Internal error when creating an engagement

engagements

#1

I am trying to import a record of customer emails into a hubspot account as part of evaluating hubspot, but hitting an internal error with the API. I am trying to create an engagement record using this python code:

def format_email(email, name):
    if not name:
        return email
    else:
        return u'%s <%s>' % (name, email)


data = json.load(open(data_file))
(firstname, lastname) = name_from_data(data)
engagement = { 
    'engagement': {
        'active': False,
        'type': 'EMAIL',
        'timestamp': data['ts'] * 1000,
        'uid': data['msg']['headers']['Message-Id'],
    },  
    'metadata': {
        'from': {
            'email': data['msg']['from_email'],
        },  
        'to': [format_email(*to) for to in data['msg']['to']],
        'cc': [], 
        'bcc': [], 
        'subject': data['msg']['subject'],
        'html': data['msg']['html'],
        'text': data['msg']['text'],
    },  
}   
if firstname:
    engagement['metadata']['from']['firstName'] = firstname
if lastname:
    engagement['metadata']['from']['lastName'] = lastname

connection.send_post_request('/engagements/v1/engagements', engagement)

That always seems to result in an internal error in the hubspot API:

'{"status":"error","message":"internal error","correlationId":"b4b9e236-42a4-4b84-bd96-de84f602e108","requestId":"d21a5da8-1e76-40a8-acf5-c59550ad4b76"}

As far as I can see my data structure matches documented structure. Can anyone see why I get that error?


#2

Hi @wichert

Those errors often point to a formatting issue with the data in the request. Do you have an example of the actual body of the request? Is the data in engagement getting stringified before the request?


#3

Hi @dadams,

This is the exact request data being send:

'{"engagement": {"active": false, "timestamp": 1368214102000, "type": "EMAIL", "uid": "<999.20130510192820.aaaaaaaaaaaaaa.aaaaaaaa@mail115.us4.mandrillapp.com>"}, "metadata": {"from": {"email": "example.sender@mandrillapp.com"}, "cc": [], "text": "This is an example inbound message.\\n", "bcc": [], "to": ["example@mail.up.curvetips.net"], "html": "<p>This is an example inbound message.</p><img src=\\"http://mandrillapp.com/track/open.php?u=999&id=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&tags=_all,_sendexample.sender@mandrillapp.com\\" height=\\"1\\" width=\\"1\\">\\n", "subject": "This is an example webhook message"}}'

In a more readable form (before serializing as JSON):

{'engagement': {'active': False,
                'timestamp': 1368214102000,
                'type': 'EMAIL',
                'uid': u'<999.20130510192820.aaaaaaaaaaaaaa.aaaaaaaa@mail115.us4.mandrillapp.com>'},
 'metadata': {'bcc': [],
              'cc': [],
              'from': {'email': u'example.sender@mandrillapp.com'},
              'html': u'<p>This is an example inbound message.</p><img src="http://mandrillapp.com/track/open.php?u=999&id=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&tags=_all,_sendexample.sender@mandrillapp.com" height="1" width="1">\n',
              'subject': u'This is an example webhook message',
              'text': u'This is an example inbound message.\n',
              'to': [u'example@mail.up.curvetips.net']}}

#4

Hi @dadams,

can you see what I’m doing wrong?


#5

@wichert the "to" field in the metadata needs to be an array, not a list:

"to": {
  "email": "example@mail.up.curvetips.net"
},

This isn’t related to the error, but you’ll also want to make sure that you’re including associations with the data, otherwise you won’t actually see the engagement (the engagement will not automatically be associated with a record based on that to field).