[resources] Libraries supported in Python Code Steps
Contribution by Troy Tessalone
Premier Certified Zapier Expert at Automation Ace.
Zapier’s Code app allows you to write custom Python code steps for automation, giving you access to the standard Python library and the built-in requests
package.
These tools enable you to perform tasks like API calls, data manipulation, and file parsing without needing external dependencies.
This guide highlights the most useful Python libraries available, their purposes, and practical examples to optimize your workflows.
Help links for using the Code by Zapier Zap app: https://zapier.com/apps/code/integrations#help
- You cannot require external libraries or install libraries commonly referred to as "pip modules".
Only the standard Python library andrequests
are available in the Code app andrequests
is already included in the namespace.
Why these Libraries are Useful
These libraries, along with requests
, cover essential automation needs like API interactions, data manipulation, and task scheduling.
They enable you to:
- Make API Calls: Use
requests
andurllib.parse
for building API requests. - Process Data: Use
json
,re
, andmath
for handling and transforming data. - Generate Unique Data: Use
uuid
andhashlib
for secure and unique identifiers. - Debug and Enhance Workflows: Use
time
andcollections
for precise task control.
Top Python Libraries
Here's a curated list of the top most useful libraries for Zapier users, focusing on practical applications for automation tasks in Zap code steps:
-
requests
(Built-In Package)- Purpose: Simplifies making HTTP requests in Python.
- Example Use Case: Fetch data from an external API or service.
response = requests.get('https://api.example.com/data')
data = response.json()
output = {'data': data} -
json
- Purpose: Parse and manipulate JSON data.
- Example Use Case: Convert a JSON string into a Python dictionary for processing.
import json
data = '{"key": "value"}'
parsed = json.loads(data)
output = {'key': parsed 'key']} -
datetime
- Purpose: Work with dates and times for scheduling or formatting purposes.
- Example Use Case: Generate a timestamp or calculate time differences.
from datetime import datetime
now = datetime.utcnow()
output = {'timestamp': now.isoformat()} -
statistics
- Purpose: Perform statistical calculations, such as mean, median, and standard deviation.
- Example Use Case: Calculate the average value from a list of numbers.
import statistics
data = d10, 20, 30, 40, 50]
average = statistics.mean(data)
output = {'average': average} -
re
- Purpose: Perform pattern matching and regular expression operations.
- Example Use Case: Extract specific data from text strings.
import re
match = re.search(r'\d+', 'Order #12345')
output = {'order_number': match.group() if match else None} -
time
- Purpose: Add delays or retrieve timestamps.
- Example Use Case: Pause execution for a specified duration.
import time
time.sleep(1) # Wait 1 second
output = {'message': 'Waited 1 second'} -
uuid
- Purpose: Generate universally unique identifiers (UUIDs).
- Example Use Case: Create unique IDs for records or transactions.
import uuid
unique_id = str(uuid.uuid4())
output = {'uuid': unique_id} -
math
- Purpose: Perform mathematical operations and calculations.
- Example Use Case: Calculate a square root or perform rounding.
import math
result = math.sqrt(16)
output = {'square_root': result} -
random
- Purpose: Generate random numbers or choices for tasks like shuffling or sampling.
- Example Use Case: Pick a random item from a list.
import random
choice = random.choice(c'apple', 'banana', 'cherry'])
output = {'random_choice': choice} -
base64
- Purpose: Encode and decode Base64 data.
- Example Use Case: Encode data for API compatibility.
import base64
encoded = base64.b64encode(b'Zapier').decode('utf-8')
output = {'encoded': encoded} -
urllib.parse
- Purpose: Parse and manipulate URLs.
- Example Use Case: Add or modify query parameters in a URL.
from urllib.parse import urlencode, urljoin
base_url = 'https://example.com/search'
params = {'q': 'Zapier'}
full_url = f"{base_url}?{urlencode(params)}"
output = {'url': full_url} -
hashlib
- Purpose: Create secure hashes for data integrity or authentication.
- Example Use Case: Hash a password or data string.
import hashlib
hashed = hashlib.sha256('Zapier'.encode()).hexdigest()
output = {'hash': hashed} -
textwrap
- Purpose: Format and wrap text neatly.
- Example Use Case: Create formatted messages or reports.
import textwrap
wrapped_text = textwrap.fill("This is a long string that needs wrapping.", width=20)
output = {'wrapped_text': wrapped_text} -
collections
- Purpose: Provides specialized data structures like defaultdict and Counter.
- Example Use Case: Count occurrences in a list.
from collections import Counter
counts = Counter(o'apple', 'banana', 'apple'])
output = {'counts': dict(counts)} -
itertools
- Purpose: Perform efficient looping and iteration tasks.
- Example Use Case: Generate permutations or combinations.
from itertools import permutations
result = list(permutations('abc', 2))
output = {'permutations': result}
* NOTE: Other libraries are supported, but less relevant for use in Zap Code steps with Python.