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?
What is an example of "Code by Zapier" being used as a trigger step?
Best answer by David ShaBest answer by David Sha
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!
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.