
Almost every affiliate script that dies, dies quietly. A field is renamed, the parse step returns an empty value, the report posts with a column of zeroes, and nobody looks until someone asks why a channel stopped converting. API changes automation depends on three habits more than any library: pin what you depend on, read what you were given, and make the run complain when its output does not look like the output. This piece is about writing syncs that degrade instead of disappearing.
The surrounding workflow matters as much as the code. If your scripts are still run by hand when someone remembers, start with one manual workflow, automated properly, then come back to hardening the calls.
The brittle patterns that break on the first API change
Three patterns account for most of the damage, and all three feel reasonable when you write them.
Hardcoded field names in the parsing logic. row.commission_amount appears in twelve places and nowhere else in your head. When the provider renames it to commissionValue, nothing throws. The dictionary lookup returns nothing, the value becomes an empty string, and the script writes a zero. Every downstream total is now wrong and the run reports success.
No version pinning. Your script works on the machine where it was written. Move it to a new machine, rebuild the container, or let a dependency install its latest minor version, and a library that used to parse a date leniently now throws, or a client that used to follow redirects no longer does. The API did not change; your copy of the world did.
Silent failures. The script runs, logs nothing useful, and exits zero. You discover the problem when the report is wrong or the sync never happened, which is to say you discover it downstream, from someone else, at the worst time. A run that fails loudly is a nuisance. A run that fails politely is a business problem.
The common thread is that all three remove information. A hardcoded name refuses to notice a change, an unpinned dependency hides the environment, and silence hides the outcome.
Where API changes automation breaks silently
Brittleness is not evenly distributed. Changes that break parsing loudly, by throwing an error on a missing key, are the good ones: you find out immediately. The dangerous changes are additive or structural.
- A renamed key. The old key disappears, the access returns nothing, and an empty value flows through arithmetic as zero.
- A type change. A value that was a string becomes a number, or a number becomes a string with a currency suffix, and comparisons start sorting alphabetically.
- A nested restructure. A flat field becomes an object, so what used to be a value is now a dictionary and every equality check fails without an exception.
- A new pagination shape. The response still has data on page one, so the script appears to work while quietly collecting a fraction of the rows.
- A shifted window. The date range the API honours changes to something exclusive, so the script double-counts or misses a day at the boundary.
None of those throw. All of them produce a number that looks like a number. That is why the guards have to be about shape and volume, not just about errors.
Pinning versions and parsing defensively
Start by pinning. Every dependency your script loads should have an exact version recorded, including the transitive ones your lockfile already tracks. Commit the lockfile, install from it, and treat a dependency upgrade as a change with a test attached rather than something that happens during a deploy.
Then make the parse step suspicious. The pattern that holds up:
- Read the API version explicitly. If the endpoint takes a version in the path or a header, put it in configuration where a human can see it, and keep the ability to call two versions during a migration. A version you cannot see is a version you cannot choose.
- Declare the fields you need. Keep the list of required field names in one place, outside the loop that reads them. When the provider changes a name, you edit one list instead of hunting twelve call sites.
- Validate before you use. Check every required field on every row before any arithmetic. Count the rows that fail validation, and treat a nonzero count as a run-level problem, not a per-row note.
- Log unexpected fields. When the response contains a field you did not declare, log it and carry on. New fields usually arrive before they matter, and the log gives you warning before the old field goes away.
- Log missing fields with the row. When a required field is absent, log the error with the row of data attached, redacted of anything sensitive. Without the row you cannot tell a schema change from one malformed record.
- Fail the run, not the row. Write the bad rows to a quarantine table or file, keep the run alive for the healthy rows if a partial load is safe, and exit nonzero so the scheduler knows.
Two more pieces earn their keep: an idempotent write, so a retry after a partial failure does not duplicate rows, and a dry-run mode that performs every step except the write. The dry run is how you test a response shape without touching production data.
A final check that tells you the run was wrong
Every script needs a closing step that compares what it expected with what it got. If the run found nothing, or too little, send an alert. A message saying "expected 1000 rows, got 50" is immediate feedback, and it converts a silent failure into a same-day ticket rather than a discovery next week.
| Guard | What it catches | What you see without it |
|---|---|---|
| Pinned dependencies | Environment drift between machines | Works locally, fails on the server after a rebuild |
| Required-field list | Renamed or removed keys | Report full of zeroes that looks like a bad week |
| Row-count floor | Partial pagination, filtered responses | A correct-looking report built from a fraction of the data |
| Unexpected-field log | Additive changes that precede a rename | Two weeks of warning thrown away, then a break |
| Run heartbeat | A job that never started at all | Nobody notices until the numbers are missing |
Set the floor from history rather than taste: take the smallest row count you have seen on a normal day and alert below it. Store the counts per run so you can answer "when did this start" without guessing. The heartbeat is the cheapest of the five and catches the failure that no internal check can see, which is a job that never ran.
The manual fallback you document before you need it
Every automated sync needs a written answer to one question: what does this script do, and could a person do it by hand for a day? If the answer is no, the automation is a single point of failure with no plan attached.
Write the fallback while the script is working. The document should say which report or endpoint the data comes from, which fields are needed, where the output goes, and which step is the one that cannot be skipped. Then the day an API breaks, the decision is already made: run it manually for one day, fix the code without the pressure of a missing report, and close the gap. That is what turns a bad API update into an inconvenience rather than a business emergency.
Keep the fallback next to the code, not in a chat thread, and check it when the script changes. A fallback that describes the version of the script from last year is worse than none, because someone will follow it.
When it does break, resist rewriting the whole sync. The cheapest recovery is usually to restore the field list, log the raw response for one run, and confirm the shape against the log. The most expensive is to debug from memory while a report is overdue. The same logging discipline that keeps the reports honest also makes them cheaper to build, which is the argument in automating affiliate reports.
FAQ
How often should I check for API changes?
Continuously, and passively. You do not want a calendar reminder, you want a log that notices. Every run should record the field names it received, the row count and the API version it called, so a diff between yesterday and today is enough to see a change the moment it happens. Then read the provider's changelog or deprecation notices on a fixed schedule, because the log tells you what changed after it changed.
Should a missing field crash the script or log and continue?
Log, then fail the run. Crashing on the first bad row throws away everything the run learned, including which fields were present. Continuing without failing produces a report that looks complete. The middle path is to quarantine the affected rows with the raw data attached, complete the safe part of the load, and exit nonzero so the scheduler raises the alarm and someone reads the log the same day.
What is the minimum viable guard for a small script?
A row-count floor and an alert. Before any schema validation, before version pinning, add the final step that compares rows fetched to rows expected and sends you a message when the number is too low. It catches pagination changes, authentication failures that return an empty list, and jobs that never ran. Two lines of code and an email address cover the failure modes that cost the most time to notice.
Next week: add the closing check to one script, with the floor taken from its own history, and write the paragraph that describes the manual fallback. Both are small, and together they mean the next API change costs you an afternoon rather than a reporting cycle.