Help

Use Python code in Zaps

tutorial
Last updated:

Code steps are a way to extend Zapier to run small snippets of Python or JavaScript as part of your Zaps. This tutorial is for Python Code steps, but you can read about JavaScript code steps here.

Code steps can be used as both triggers and actions.

Heads Up

Python is an advanced programming language. If you're not familiar with using Python, it's recommended to ask a developer for help. Zapier does not offer email support for code steps. If you run into trouble, we recommend asking questions on StackOverflow and tagging them with "Zapier".

For tips and inspiration, check out Python code examples.

The Python environment

The environment is vanilla Python 3.7.2 (2.7.10 for Zaps made before 2019-01-24). Your script is sandboxed and can only run for a limited amount of time and within a limited amount of memory—the exact limits are based on plan level. If you exceed those limits, your script will be stopped.

Input data for Code steps

In your code step, you have access to the input_data dictionary variable, where all values will be strings. Since the amount of data that might feed into your script might be large or highly dynamic you'll need to define an input_data mapping via our GUI by providing a key and value.

Here we're defining body, name and subject fields on the input_data object based on our trigger data.

Note

Mapping input_data is not available in triggers.

Output data from code steps

Code steps return a single output variable, which is a dictionary or list of dictionaries that will be the "return value" of this code. You can explicitly return early if you like. This must be JSON serializable.

Setting the output to a list of dictionaries will run the subsequent steps multiple times, once for each item in the list.

If your Code step is the Zap's trigger and an empty list is returned, nothing happens. You can think of it like a polling trigger that did not get any results in the HTTP response. This functionality is exclusive to triggers—returning an empty list in a code step action does not have the same effect.

Note

Note: you may see old code examples using input which we changed to input_data to reduce confusion. Both work but we prefer the less confusing input_data since input was a built in function for Python.

Utilities in Code steps

There are a few utilities you have access to:

  • requests: An easy-to-use HTTP client. Read the documentation here.
  • StoreClient: An easy to use database or cache for stashing keys and values to maintain state between runs. Learn more here.
  • print: Super helpful if you want to debug your function. You'll need to test your zap to see the values (the logs are returned to you in a runtime_meta added automatically to your output).

Testing and debugging code steps

Running your zap via the dashboard is the canonical way to confirm the behavior you expect. Tour Task History will have all relevant details around the ran code's input_data, output and logs. The test step in the editor can be used for a tighter feedback loop.

Limitations with Code steps

  • Free users are limited to 1 second and 128mb of RAM. Paid users get well over double that at 10 seconds and 256mb of RAM. Your Zap will hit an error if you exceed these limits.
  • Unfortunately, you cannot require external libraries or install libraries commonly referred to as "pip modules". Only the standard Python library and requests is available in the Code app and requests is already included in the namespace.

How Zapier handles floats and integers

If you have a python code step that returns a value like this…

return {'result': 1.0}

…it may appear as 1, rather than 1.0, in your Task History and test results. This is an artifact of the Zapier UI. The Zapier web UI does not distinguish between int and float values; they are all just numbers. In your Python code step you can use int(aNumber) or float(aNumber) to make sure to convert the value to the data type you require.


Was this doc helpful?

Need More Help?

Zapier Support

Contact our world class support team and we’ll be happy to help you get up and running!
Contact Us

Hire an Expert

We have a directory of professionals across the globe who are ready to help.
Find a Zapier Expert