With Zapier Functions, you have one main Python file that is run whenever your triggers have new output. You can also create multiple Python code files to separate and organize your functions.
Working with the main.py file
The main.py
file is added automatically when you create a new function. When your functions are deployed, this is the file that runs whenever your trigger events occur. You can reference trigger fields in it, import common Python packages, and reference other code files you create. You cannot delete this file.
Add actions to your code
You can add actions from any public Zapier app to your code. To insert a new action into your code:
- In any line of your code file, start typing
zapier.action.
to display a list of available apps in a dropdown menu. You can continue typing to find the one you want. To select the app, press the Enter or Tab key. - Enter a period to see the dropdown menu of available actions.
- Select the action and press the Enter or Tab key. The
account_id
field will be added to the code. - Move the cursor to the
account_id
field and press the CTRL + Space keys to display a list of existing connections. - Start typing the connection's name to filter the list. You can also add a new connection.
- Once the connection is done, the code will populate the parameters for that action, and you can insert variables or references to trigger outputs.
- You can use CTRL + Space with most parameters to see if Zapier has auto-complete options to help you set up your action.
- You can change the CTRL + Space hotkey in the code editor settings.
Reference trigger outputs
While you edit a code file, you can reference the output from any of your trigger files. To reference a trigger output field:
- On the right sidebar, click the Selected trigger dropdown menu.
- Select the trigger output you want to reference in your code.
- In the code editor, start typing
zapier.trigger_output.
to display a dropdown menu with available fields. - Move through the list and press the Enter or Tab key to select a field.
Use Tab to autocomplete the suggestions that appear while you type in the code editor.
Create and use other code files
You can create separate code files to organize Python functions and then reference them in your main.py
file.
To add a new code file:
- On the left sidebar, click the Explorer icon .
- In the CODE FILES section, click the plus sign +.
- In the text field that appears, enter the file name and press Enter.
- Import the new file into your
main.py
file so it can be run.
You create a file called "new_file.py" with a function called hello
. You can import the whole file by adding the following to your main.py
file:
Import new_file
Then, call the hello function by typing:
new_file.hello()
Or, you can import only the hello
function from your file by adding the following:
from new_file import hello
Then you can use the function anywhere on your code by typing:
hello()
Use Python packages
You can also use most common Python packages with Zapier Functions. Learn how to install and import packages.