feat(apple): GET /api/apple/songs — batch ISRC lookup
Implements recoupable/chat#1959. Merge after recoupable/docs#298 — that PR is the contract this is built against.
First Apple Music integration in api. Complements the /api/spotify/* family rather than mirroring it: Spotify reaches ISRCs only through a fuzzy isrc: search query, while Apple matches the identifier exactly and returns release-level rights metadata Spotify does not expose.
The two correctness decisions worth reviewing
1. Results are built from meta.filters.isrc, not data[].attributes.isrc.
Apple echoes every requested ISRC back in meta.filters.isrc, mapped to the songs it matched — including an empty array for the ones it did not. That echo is the only place a requested-but-unmatched ISRC appears at all. The manual sweep script this replaces matched on data[].attributes.isrc instead, which silently drops any song whose returned ISRC differs from the one requested. getAppleSongsByIsrc.ts reads the meta map and carries a comment saying why.
2. A malformed ISRC is a 400, not a passthrough.
Apple answers filter[isrc]=NOTANISRC with 200 and {"data":[]} — byte-identical to a genuine takedown. Without our own format check, a typo would be reported to a customer as their recording having gone dark. That is the exact claim this endpoint exists to make, so the validator rejects anything failing /^[A-Z]{2}[A-Z0-9]{3}\d{7}$/ and names the offending value in the error.
Other behavior, all confirmed against the live API
- Chunks at 25. Apple hard-caps
filter[isrc]; a 26th value is a400 Invalid Parameter Value, not a truncation. Chunking lives in the lib and never surfaces to the caller. include=albumssoupc,record_label, andcopyright(the ℗ line) arrive in the same round trip.songsis an array. One ISRC legitimately maps to several Apple song ids when the same recording appears on multiple releases; a test ISRC returned 6.- Storefront validated locally against the 167 ids from
GET /v1/storefronts, so an unknown one is a local 400 rather than a wasted round trip. - Auth only, no
checkAccountArtistAccess— matching the precedent invalidateGetSongsRequest.ts, whose comment records that ISRC-keyed song metadata is DSP-public. - No credits. Unlike the Songstats-backed
/api/research/*endpoints, Apple charges nothing per call and responses are Akamai-cached, so this follows the free/api/spotify/*family. - Upstream errors are logged, never returned. A missing credential throws a message naming the env var; the client gets a generic 500. There is a test asserting
APPLE_MUSIC_PRIVATE_KEYcannot appear in a response body.
Developer token
Apple authenticates with a self-signed ES256 JWT rather than a client-credentials exchange, so this does not follow the Spotify generateAccessToken pattern. Two details that each produce a bare 401 with no error body when wrong, both covered by tests: the signature must be raw R‖S (IEEE P1363), not Node's default DER, and the key id goes in the JWT header while the team id is the issuer. Cached in module scope for 55 minutes against a 60-minute expiry.
Deployment prerequisite
APPLE_MUSIC_PRIVATE_KEY, APPLE_MUSIC_KEY_ID, and APPLE_MUSIC_TEAM_ID must be set in Vercel for preview and production before this works. APPLE_MUSIC_PRIVATE_KEY is the PEM body inlined, not a path — there is no filesystem to read a .p8 from. Added to .env.example.
Flagging for the record: the private key is currently a single local copy with no documented owner or rotation path. Worth fixing separately.
Tests
30 new tests across 5 files, each written RED before implementation. Full suite: 827 files / 4568 tests passing, tsc --noEmit and eslint clean.
Preview verification against every documented status code to follow in a comment once the deployment is up and the env vars are set.
🤖 Generated with Claude Code
Summary by cubic
Adds GET /api/apple/songs for batch ISRC lookup against Apple Music. It returns one row per requested ISRC so unmatched codes come back as found: false instead of disappearing, and rejects malformed ISRCs so typos don’t masquerade as takedowns.
- Builds results from meta.filters.isrc and derives found from the meta hit count (not the resolved songs), so a hit that fails to resolve is no longer reported as found: false.
- Rejects malformed ISRCs with 400; Apple otherwise returns 200 + empty data identical to a real takedown.
- Chunks requests at Apple’s 25-value cap and preserves the requested order; uses include=albums and extend=composerName,audioVariants to return album and rights fields in one call.
- Drops the limit param; Apple ignores limit on identifier filters and returns every match.
- Validates storefront locally (defaults to us); auth required, no per-artist scoping.
- Generates an Apple developer token via ES256 JWT with raw R‖S (IEEE P1363); caches within the 60-minute window; accepts a private key with escaped newlines or wrapping quotes; upstream errors are logged but never returned.
Rollout
- Set APPLE_MUSIC_PRIVATE_KEY, APPLE_MUSIC_KEY_ID, and APPLE_MUSIC_TEAM_ID in Vercel. APPLE_MUSIC_PRIVATE_KEY may be the PEM body inlined with real newlines or literal “\n” and optional wrapping quotes; both are accepted.
Summary by CodeRabbit
- New Features
- Added Apple Music song lookups by ISRC.
- Supports multiple storefronts, with the United States as the default.
- Returns song, album, artwork, preview, and match-status details.
- Preserves requested ISRC order and identifies recordings not found.
- Added request validation, authentication, and CORS preflight support.
- Provides clear responses for invalid requests and service errors.