A practical field guide from Automation Ace.
How to Use AI to Generate JavaScript or Python for a Zapier Code Step
Zapier Code steps give you the ability to write custom logic — transforming data, calling APIs, building dynamic values — but writing the code from scratch requires JavaScript or Python knowledge that not every Zapier user has. AI assistants like ChatGPT and Claude can write working Code step code in seconds when prompted correctly. The key is giving the AI enough context about Zapier's environment and your specific data to produce code that actually works without modification. This guide covers how to prompt AI for Code step code and what details to include.
What AI Needs to Write Good Zapier Code
An AI assistant generating code for a Zapier Code step needs four things to produce useful output:
- The language: JavaScript or Python — specify which you want
- The input data fields: What fields are available via
inputData(JS) orinput_data(Python), and what values they contain - What the code should do: The transformation, calculation, or API call you need
- What the output should be: The field names and format of the
outputobject the next Zap step will use
Skipping any of these produces generic code that needs significant editing. Including all four produces code you can paste and run.
Example Prompt: JavaScript Date Calculation
A poorly framed request:
"Write Zapier code to add 30 days to a date."
A well-framed request:
"Write a Zapier JavaScript Code step that takes a date frominputData.start_date(formatted as YYYY-MM-DD, e.g. '2025-03-15') and outputs two fields:due_dateas a date 30 days later in YYYY-MM-DD format, anddue_date_formattedas a human-readable string like 'April 14, 2025'. The output object should use string values."
The second prompt tells the AI exactly what the input looks like, what format it's in, what the outputs should be named, and what format they should use. The result will be paste-ready.
Example Prompt: Python Data Transformation
"Write a Zapier Python Code step. The input data has:input_data['full_name'](a string like 'John Smith'),input_data['phone'](a string like '555-867-5309'), andinput_data['tags'](a comma-separated string like 'vip, enterprise, Q1'). The code should: split the full name into first and last name, strip all non-digit characters from the phone number, split the tags into a list and trim whitespace from each, and return a dict with keys:first_name,last_name,phone_digits,tag_list(as a JSON string of the array), andtag_count(as a string of the count)."
Telling AI About Zapier's Environment
Zapier Code steps run in a specific environment — not a standard Node.js or Python runtime. Including these environment details prevents AI from writing code that uses unavailable features:
For JavaScript Code steps, add to your prompt:
"This runs in Zapier's JavaScript Code step environment. Available globals:fetch(async),URLSearchParams,btoa,atob. Thezobject is available forz.dehydrateFile(). Node.js built-ins likecrypto,querystring,url,buffer, andutilare available.async/awaitis supported.require()supports: lodash, moment, moment-timezone, xml2js, cheerio. Do NOT usefs,child_process,express, or browser APIs. Output must be assigned to the variableoutputas a plain object with string or number values."
For Python Code steps:
"This runs in Zapier's Python Code step environment. Available libraries:requests,datetime,calendar,json,re,hashlib,hmac,uuid,base64,math. Input data is ininput_datadict. The function must return a dict. All values in the return dict should be strings or numbers."
Example Prompt: API Call with fetch
"Write a Zapier JavaScript Code step that makes an authenticated GET request tohttps://api.example.com/contactswith query parametersinputData.email) andstatus=active. Set the Authorization header toBearer {inputData.api_token}. If the response is not OK, throw an error with the status code and response body. Parse the JSON response and output:contact_id(theidfield from the first result, or empty string if no results),contact_name(thenamefield),foundas 'true' or 'false', andresult_countas a string. Use async/await and URLSearchParams."
How to Verify AI-Generated Code
Before relying on AI-generated Code step code in production:
- Read it: Scan for anything that references unavailable APIs (
fetchwithout await,require()for unsupported packages, file system calls) - Test with real data: Paste into a Zapier Code step with real input data values and run a test. Check that output fields match what you specified.
- Check edge cases: What happens if the input is empty, null, or in an unexpected format? Add a follow-up prompt: "What if
inputData.emailis an empty string? Update the code to handle that gracefully." - Ask for comments: See asking AI to document Zapier Code steps with comments for how to get the AI to annotate the code it generates.
Iterating on the Output
If the first response isn't quite right, follow up with specific corrections rather than reprompting from scratch:
- "The output for
tag_countshould be a string, not a number. Update that." - "Add error handling: if the API returns a 404, output
found: 'false'and empty strings for the other fields instead of throwing." - "Rewrite this to use Python instead of JavaScript."
AI chat interfaces maintain context in the conversation — each correction refines the previous output rather than starting over.
The prompt pattern that produces the best Code step code every time: state the language, describe the input data fields and their example values, describe what the code should do step by step, and specify the exact output field names and value formats. AI assistants are good at writing code — they just need enough context to write the right code for Zapier's specific environment and your specific data.
For more on the JavaScript environment available in Code steps, see JavaScript libraries in Zapier Code steps. For Python, see Python libraries in Zapier Code steps. For asking AI to debug code that isn't working, see using AI to debug Zapier Code steps. For help building a Code step for a complex integration, talk to Automation Ace.
Disclaimer: This article may include links to apps, products, or services. Some links may be affiliate links, which means Automation Ace may earn a commission at no extra cost to you.