I sell WordPress plugins through Stripe. This year I'm running my first Black Friday sale, so I needed one 20% coupon and 15 promotion codes, one per deal site. I asked an AI (Claude) to write a shell script that creates them with curl.
I ran it. The last line said:
All created as inactive.
"Inactive" meant the codes would sit switched off until the sale started. That was the plan.
Nothing had been created. Stripe had returned an error at the very first step, and the script printed its success message anyway.
There were two bugs. The first was a single character. The second matters more.
Bug 1: % in a value sent with curl -d
The coupon name was Black Friday 2026 - 20% off, sent like this:
curl https://api.stripe.com/v1/coupons -u "$SK:" -d "name=Black Friday 2026 - 20% off" ...
curl -d sends what you write as-is. It does not URL-encode. The server reads the body as application/x-www-form-urlencoded, where % means "the next two characters are a hex byte." % o isn't a valid escape.
I later set up a small server that echoes back exactly what it receives and compared -d with --data-urlencode (curl 8.5.0, parsed with Python's parse_qsl):
-
name=Black Friday 2026 - 20% offwith-d:% oarrives as a broken escape. A lenient parser keeps it, a strict one rejects it. -
name=C++ tipswith-d: arrives asC tips, because+means space. No error. -
name=Q&Awith-d: arrives as two fields,name=QandA=. No error. - All three with
--data-urlencode: arrive exactly as written.
The + and & cases are worse than %. They don't fail. They save the wrong value quietly.
--data-urlencode encodes only what comes after the first =, so names like applies_to[products][] are sent as-is.
Bug 2: the script never read the response
Stripe answered with an error. The script didn't look at it. It moved on to the next step, and when it reached the end, it printed "All created as inactive."
Looking back, that line meant exactly one thing: the script reached its last line. It said nothing about whether a coupon existed in Stripe. The thing that printed "created" was the same thing that never checked.
Bug 1 is fixed by renaming the coupon. Bug 2 would show up again in any script written the same way.
The fixed version
Two helpers. post() sends every value with --data-urlencode. check() reads the JSON, and if there's an error key, prints the message and the offending parameter, then exits non-zero.
post() {
local path="$1"; shift
local args=()
for kv in "$@"; do args+=(--data-urlencode "$kv"); done
curl -sS -X POST "https://api.stripe.com/v1/$path" -u "$SK:" "${args[@]}"
}
check() {
python3 -c '
import sys, json
raw = sys.stdin.read()
try:
d = json.loads(raw)
except Exception:
print("Unexpected response:", raw.strip()[:400] or "(empty)", file=sys.stderr); sys.exit(1)
if isinstance(d, dict) and "error" in d:
e = d["error"]
print("API error:", e.get("message", ""), file=sys.stderr)
if e.get("param"): print("Param:", e["param"], file=sys.stderr)
sys.exit(1)
json.dump(d, sys.stdout)
'
}
post coupons id=example-coupon percent_off=20 duration=once \
"name=Black Friday 2026 - 20 percent off" | check
I also renamed the coupon to 20 percent off, so there's no % at all.
With the fixed script, I created the coupon and all 15 codes, every one of them switched off until the sale.
What I check in AI-written scripts now
- When a script says "done," ask what it actually checked before saying it.
- Before running an AI-written script, look at what happens when a step fails. Does it stop?
- After it runs, look at the real thing in the real place. Here, that's the Stripe dashboard, not the terminal.
- With
curl, use--data-urlencodefor any value that might contain%,+,&, or=.
AI-written scripts are usually tidy on the happy path. What happens when something fails is the part you have to read yourself.
Disclaimer: The script in this post is a cleaned-up version of the one I used, with placeholder IDs and its messages translated from Japanese. English isn't my first language, so I use an AI assistant to help draft and edit the writing.
Top comments (0)