I’m having trouble with my Zapier Python code step. The system keeps telling me I need to specify an output but I’m not sure where to put it in my script.
Zapier’s documentation shows this basic example:
return {'user_name': input_data['user_name']}
But my code is more complex since I’m making an API call. Here’s what I have:
import requests
import json
api_endpoint = "https://example-api.com/submit"
request_data = json.dumps({
"ContactInfo": "Contact Details",
"ClientID": None,
"CommunityID": "None",
"LastName": "input_data['surname']",
"MiddleName": "None",
"FirstName": "input_data['givenName']",
"Prefix": "Ms",
"LastName2": "SAMPLE",
"MiddleName2": "Test",
"FirstName2": "Test",
"Prefix2": "",
"Suffix2": "",
"Address": "Test Ave",
"CityName": "Springfield",
"PostalCode": "54321",
"State": "CA",
"HomePhone": "input_data['phoneNumber']",
"EmailAddress": "input_data['emailAddr']",
"Score": "Score",
"Source": "Source",
"Notes": "text",
"ExtID": "EXTID",
"CurrentStatus": "Active",
"ProspectID": "1",
"CustomerData": {
"AgreementName": "SampleAgreement",
"Customers": [
{}
]
}
})
request_headers = {
'Content-Type': 'application/json'
}
api_response = requests.request("POST", api_endpoint, headers=request_headers, data=request_data)
print(api_response.text)
How do I properly set up the return statement so Zapier gets the response data? Every time I try adding return anywhere I get new errors.
The main issue is that your script doesn’t handle the API response properly before returning it. After working with Zapier’s Python actions for several months, I’ve found that you need to check if the API call was successful first, then parse the response appropriately. Try adding error handling like this:
if api_response.status_code == 200:
try:
response_data = api_response.json()
return {'success': True, 'data': response_data}
except:
return {'success': True, 'raw_response': api_response.text}
else:
return {'success': False, 'error': api_response.text, 'status_code': api_response.status_code}
This approach has saved me countless debugging hours because you can see exactly what went wrong when the API call fails. Also fix those input_data references by removing the quotes around them.
Had this exact same issue when I first started using Zapier’s Python actions. The key thing you’re missing is that you need to actually return the API response data at the end of your script, not just print it. Replace your print(api_response.text)
line with something like return {'api_response': api_response.text, 'status_code': api_response.status_code}
. This way Zapier can access the response data in subsequent steps. Also noticed you have input_data values wrapped in quotes - those should be actual variables like input_data['surname']
without the quotes, otherwise you’re just sending literal strings. I made that mistake too and spent way too long debugging why my API calls weren’t working properly. The return statement must be the last line of your script and should contain all the data you want to pass to the next Zapier step.
you gotta store the response in json format first then return it. try adding response_json = api_response.json()
after your api call, then return {'data': response_json}
at the end. also fix those input_data strings - remove the quotes so it actually uses the variables instead of literal text