Best answer

What is an example of "Code by Zapier" being used as a trigger step?

  • 15 December 2021
  • 2 replies
  • 311 views

Userlevel 1

Essentially, I’d like to know the nature of how triggers with code steps work. If I turn on a zap with code step as trigger, is the python code going to run once? Is there a time limit on this trigger code step if I made it a polling script? If not, what other uses are there for having code step as a trigger?

icon

Best answer by David Sha 15 December 2021, 08:26

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

2 replies

Userlevel 7
Badge +14

Hi @David Sha 

Check out the available help articles for the Code app: https://zapier.com/apps/code/help

 

NOTE:

If Code by Zapier is the Zap's trigger and you return an empty array [], we will not trigger any actions downstream.
It's as if you said "never mind" in code.
This does not apply when Code by Zapier is used as an action, only when it is the trigger.

 

Limitations with Code steps

  • The environment in which your Code steps run (AWS Lambda) has an I/O limit of 6 MB. The total size of the code and the data processed by the Code step cannot exceed that. If you're hitting this error, try to limit the amount of data returned from your function. For instance, don't return an entire JSON structure, just the keys you need.
  • Free users can run scripts of up to 1 second and 128 MB of RAM. Paid users can run scripts of up to 10 seconds and 256 MB of RAM. Your Zap will hit an error if it exceeds these limits.
  • You cannot require external libraries, or install or import libraries commonly referred to as "npm modules". Only the standard node.js library and the fetch package are available in the Code by Zapier app. fetch is already included in the namespace.
Userlevel 1

I have done some exploration and just tested out the code step as a trigger. It seems that the code is triggered every minute which is exactly what I was looking for in a polling request. I also struggled to find any code on this forum as a trigger and I do remember someone asking for an example so here is a version of mine. It polls a website (every minute ± 5 secs according to my testing with Zapier), see if there is any new data after the last request, and only continues if data is found (which I use the “Filter” step to accomplish after the code step).

from requests import request
import json
from datetime import datetime, timedelta

store = StoreClient("dee5252a-6135-4f80-8393-7e6e122803fc")

# constants
TESTING = True
CLIENT_ID = store.get("poll-lup-client-id")
CLIENT_SECRET = store.get("poll-lup-client-secret")
EVENT_ID = store.get("poll-lup-event-id")

# obtain bearer token
url = "https://security.lup.com.au/connect/token"
data = {
"grant_type": "client_credentials",
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
"scope": "oapi"
}
response = request("POST", url, auth=(CLIENT_ID, CLIENT_SECRET), data=data)
token = response.json()["access_token"]

# make the poll request
now = datetime.now().isoformat()
if TESTING:
long_time_ago = datetime.now() - timedelta(hours=6000)
last_request_at = long_time_ago.isoformat()
else:
last_request_at = store.get("poll-lup-last-request-time")
store.set("poll-lup-last-request-time", now)

url = f"https://oapi.lup.com.au/api/v1/events/{EVENT_ID}/visitors"
headers = {
"Authorization": f"Bearer {token}",
"If-Modified-Since": last_request_at,
"Content-Type": "application/json"
}
response = request("GET", url, headers=headers)

if response.text:
return response.json()
else:
# nothing in response
print(response.text)

An example output in json when a request is made by moi is:

{
"visitors": [
{
"id": "08cebd32-25a3-4744-a8df-7137d2574281",
"givenName": "John",
"familyName": "Doe",
"email": "test@email.com",
"postCode": "4000",
"phoneNumber": "0412345678",
"productCode": "A&O - Expo Pass",
"atEvent": false,
"modifiedUTC": "2021-12-14T09:58:44.3628325+00:00"
},
{
"id": "97a2a52a-a675-4211-ae68-c933384f7442",
"givenName": "Jeff",
"familyName": "Smith",
"email": "email@gmail.com",
"postCode": "4000",
"phoneNumber": "0444444444",
"productCode": "E&R - Expo Pass",
"atEvent": false,
"modifiedUTC": "2021-12-14T09:58:45.1283019+00:00"
}
],
"totalCount": 2,
"totalPages": 1,
"nextPage": "",
"previousPage": ""
}



In the filter step, I would use `total_count` in the json and if it is greater than zero, I would continue the zap. If it is less than zero, it will be filtered out.

If you’re worried about task usage, if the zap filters after the second step, then no tasks will be used and you still make a request without using tasks!