
API Key in Your App? Fix It — Supabase + Claude
Your API key is in the app bundle. Anyone can extract it.
You shipped your AI feature the fast way: the Anthropic key lives in a Swift constant, the app calls the API directly. It works — until someone runs strings on your binary, posts your key, and your bill becomes their plaything. Apple's reviewers increasingly catch this too (it's a rejection pattern).
This skill builds the layer that should have been there from day one: a Supabase backend where your key lives server-side and every request is verified, limited, and accounted for — built from a real Claude-powered iOS subscription app (¥480/month) running in production. Every module here is live code, not tutorial code.
The stack you get
iPhone (SwiftUI)
→ StoreKit 2: subscription verified
→ Supabase Auth: JWT in every request
Edge Function
→ JWT verified · tier checked · rate limited (3 layers)
→ prompt injection scanned · cache checked
Claude API
→ response cached, usage logged, cost tracked per user
Why every layer matters: no JWT check = strangers spend your API budget. No tier check = free users get premium AI. No rate limit = one user bankrupts you overnight. No injection defense = users puppet your AI. No cache = you pay 10× for repeated questions.
What's inside (10 modules, all copy-paste runnable)
- Secure Edge Function — the complete authenticated, tiered, rate-limited handler
- Prompt injection defense — pattern + heuristic scanning before anything reaches Claude
- Context window management — auto-compress long conversations with the cheapest model
- 3-layer rate limiting — per-user daily, per-user burst, global burst
- Prompt version control — change your AI's behavior with a SQL insert, no app update, with A/B testing built in
- Response caching — SHA-256 keyed, context-aware (40–60% cost reduction on FAQ-style traffic)
- Cost monitoring — per-user usage logs, daily cost dashboard, spend alerts
- Streaming — server SSE passthrough + the Swift client that consumes it
- Complete database schema — with row-level security locked down properly
- Deployment checklist — what to verify before real users arrive
Plus a Swift integration layer: AIService class with async/await, streaming, typed errors, and rate-limit handling that shows your upgrade prompt at exactly the right moment.
It debugs with you
Deployed and something's broken? Paste the error. The skill goes straight to diagnosis — no architecture lecture first. The troubleshooting playbook covers the four failures everyone hits (401s, CORS, secrets not loading, cold starts), and for everything else: supabase functions logs --tail, paste the output, debug together.
v4.0 — The Production Hardening Update
This version closes real security gaps: RLS lockdown on server-only tables (previously your system prompts and cached responses were readable through the auto-generated REST API), context-aware caching with SHA-256 keys, wired-in burst limiting, Claude API error handling, current pricing in the cost tracker, and the streaming Edge Function that completes the Swift client.
Who this is for
iOS developers adding Claude to a Supabase app. Indie hackers shipping AI subscription apps who want the backend done right the first time. Anyone whose API key is currently sitting in an app bundle (fix it today — really).
Part of the 3-skill iOS AI stack: App Store Rejection Buster (get approved) → SwiftUI StoreKit 2 (subscriptions) → this (the AI backend).


