Python Syntax Error in Zapier Code Step When Sending Data to Analytics API

I’m working on a Zapier automation that takes email tracking data and sends it to an analytics platform using their API. I’m using the Python code action in Zapier but I keep running into a syntax error.

The error message I get is:

We hit an error creating a run python. Your code had an error! Traceback (most recent call last): SyntaxError: invalid syntax (usercode.py, line 9)

Here’s my Python code that’s causing the issue:

api_endpoint = 'https://api.analytics-service.com/v1/events/'
data_payload =
{
  'user_id': input_data['user_email'],
  'event_name': 'Email Interaction',
  'attributes': {
    'list_identifier': input_data['list_id'],
    'interaction_timestamp': input_data['timestamp'],
    'campaign_id': input_data['campaign_ref'],
    'user_city': input_data['location_city'],
    'user_state': input_data['location_state'],
    'user_nation': input_data['location_country'],
    'latitude': input_data['geo_lat'],
    'longitude': input_data['geo_lng'],
    'nation_code': input_data['country_iso']
  },
  'metadata': {
    'ip_address': input_data['user_ip']
  }
}

request_headers = {
    'content-type': 'application/json',
    'Authorization': 'Basic MYENCODEDAPIKEY'
}

api_response = requests.post(api_endpoint, data=json.dumps(data_payload), headers=request_headers)
api_response.raise_for_status()
return api_response.json()

Can anyone spot what might be wrong with the syntax? Also open to suggestions for better ways to handle this kind of data transfer.

move the opening bracket right after equals on line 2 - that’s the issue. python can’t handle line breaks there. also, don’t forget to import requests and json at the top, or it’ll fail even after syntax fix.

The syntax error’s on line 2 - you’ve got data_payload = followed by a line break. Python needs the assignment finished on the same line or properly continued. Either put the opening brace right after the equals sign like data_payload = { or use a backslash to continue the line. I’ve hit this same problem with Zapier’s Python environment before. It’s way pickier about formatting than local dev. Also, you’re missing your imports at the top - don’t see import requests and import json anywhere. That’ll throw runtime errors even after you fix the syntax. Zapier’s code steps need explicit imports for pretty much everything except basic Python stuff.

That line break after data_payload = on line 2 is your problem. Python needs the dictionary to start right away or you need proper line continuation. Just move the opening brace to the same line: data_payload = {. I’ve hit this same formatting issue with Zapier’s code actions - their Python interpreter is way pickier than what you’re used to locally. Once you fix the syntax, add some error handling around that API call. Network requests fail all the time in Zapier’s environment. Wrap the requests.post in a try-except block to catch connection timeouts or rate limiting. Also check that your input_data keys actually exist - missing keys throw KeyError exceptions that are a pain to debug in Zapier’s interface.