How to Automate a Workflow with AI Coding Tools
Turn a manual, repetitive task into a scheduled script with proper error handling, secrets management, and monitoring.
What's actually worth automating
Good automation candidates are repetitive, well-defined, and low-risk to get wrong occasionally — syncing data between two systems, generating a recurring report, sending a reminder. This tutorial builds one end to end: a script that syncs leads from a CRM API into a local file on a schedule.
Step 1: Define the trigger and the steps in plain language before writing code
- Trigger: runs automatically once an hour
- Step 1: fetch new leads from the CRM's API
- Step 2: write them to a JSON file (or push to a database/sheet in a real setup)
- Step 3: log how many were synced, and fail loudly if the fetch fails
Step 2: Ask the AI to write the script
Write a Node.js script at scripts/sync-leads.js that fetches leads from
https://api.crm.example.com/leads using a bearer token from
process.env.CRM_API_KEY, and writes the result to leads.json. Log the
number of leads synced. Exit with a non-zero code and a clear error
message if the request fails.Step 3: Review the generated script for real error handling
import fs from "node:fs/promises"
async function syncLeads() {
const res = await fetch("https://api.crm.example.com/leads", {
headers: { Authorization: `Bearer ${process.env.CRM_API_KEY}` },
})
if (!res.ok) {
throw new Error(`CRM API returned ${res.status}: ${await res.text()}`)
}
const leads = await res.json()
await fs.writeFile("leads.json", JSON.stringify(leads, null, 2))
console.log(`Synced ${leads.length} leads at ${new Date().toISOString()}`)
}
syncLeads().catch((err) => {
console.error("Lead sync failed:", err.message)
process.exit(1)
})Step 4: Schedule it — don't rely on remembering to run it manually
GitHub Actions can run a script on a cron schedule for free on most plans, without needing a separate server. This is often the simplest place to start for a small automation.
name: Sync Leads
on:
schedule:
- cron: "0 * * * *"
workflow_dispatch: {}
jobs:
sync:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm install
- run: node scripts/sync-leads.js
env:
CRM_API_KEY: ${{ secrets.CRM_API_KEY }}Step 5: Keep secrets out of the repository
The workflow above reads `CRM_API_KEY` from GitHub's encrypted repository secrets, not from a committed file. Add it under Settings → Secrets and variables → Actions in your GitHub repository — never hardcode it into the script, even temporarily while testing.
Step 6: Add monitoring, even something simple
- Check the Actions tab periodically for failed runs, or wire up a failure notification
- Log a timestamp and count on every successful run so you can spot a silent failure (a run that succeeds but syncs zero leads)
- Set an alert if the job hasn't run successfully in longer than its schedule would allow
When not to automate
Skip automation for anything with irreversible consequences until you've run it manually enough times to trust the logic completely — a scheduled script that occasionally does the wrong thing at 3 a.m. with nobody watching is worse than a manual step that's merely inconvenient.
Prompts to pair with this
Automate a repetitive multi-step workflow across tools
Maps a manual, repetitive multi-step workflow into an automated one, with explicit failure handling and human checkpoints at the steps that need them.
Copy promptpythonWrite a robust Python automation script with error handling
Produces a Python automation script that handles real-world edge cases — bad input, network failures, partial completion — instead of a fragile happy-path-only script.
Copy promptRelated tutorials
How to Build a Simple AI Agent
Build a working agent loop — tool definitions, a system prompt, execution, and the guardrails that keep it from doing something destructive.
AdvancedHow to Connect a Real API to an AI-Generated App
Move an API call from an insecure, client-side prototype into a proper server-side route handler with error handling.
IntermediateGit Basics for AI-Assisted Coding
Git isn't optional once an AI tool is editing your code for you — it's the only reliable way to review, commit and roll back changes.
BeginnerReady to build the next one?
Browse the full tutorial library or grab a ready-made prompt for your next step.