Skip to content
Automation

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.

Intermediate10 min read

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

Prompt
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

scripts/sync-leads.js
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.

.github/workflows/sync-leads.yml
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.

Ready to build the next one?

Browse the full tutorial library or grab a ready-made prompt for your next step.