App Store Server API returns 401 Unauthenticated
A 401 from the App Store Server API means Apple rejected your JWT, not your request. The header needs alg ES256 and kid set to your In-App Purchase key ID. The payload needs iss (your issuer ID), aud appstoreconnect-v1, bid (the app's exact bundle ID), iat set to now, and exp no more than 60 minutes later. Decode the token and compare each field.
Symptom
- Every request returns
401 Unauthenticated, often with an empty body. - It worked once, then started failing after an hour.
- It works for one app and not another in the same account.
Why it happens
Apple validates every request's bearer token. Any wrong field gives the same 401:
| Cause | How to tell |
|---|---|
| Wrong key type: an App Store Connect API key instead of an In-App Purchase key | kid belongs to a key listed under App Store Connect API, not In-App Purchase |
bid missing or not the app's bundle ID |
Decoded payload has no bid, or a different bundle ID |
aud isn't appstoreconnect-v1 |
Decoded payload shows another value |
exp more than 60 minutes after iat, or the token expired |
It fails after an hour, or every time with a long lifetime |
| Server clock wrong | iat is in the future or far in the past |
Signed with the wrong .p8, or not ES256 |
Header alg isn't ES256, or the key doesn't match kid |
| Key revoked | The key shows as revoked in App Store Connect |
Confirm it
- Decode the token you're sending (it's three base64url parts):
echo "$JWT" | cut -d. -f1 | base64 -d 2>/dev/null; echo
echo "$JWT" | cut -d. -f2 | base64 -d 2>/dev/null; echo
- Check the header:
alg: ES256,kid: <In-App Purchase key ID>,typ: JWT. - Check the payload:
iss(Issuer ID from Users and Access → Integrations),iat,exp,aud: appstoreconnect-v1andbid: <bundle ID>. - Compare
iatwith the current Unix time (date +%s). - In App Store Connect, open Users and Access → Integrations → In-App Purchase and check the key ID exists and isn't revoked.
Fix
- Create or use an In-App Purchase key, and sign with its
.p8using ES256. - Put the right values in the token:
kidin the header;iss,iat,exp,aud: appstoreconnect-v1andbidin the payload. - Keep the lifetime at 60 minutes or less, and make a new token before the old one expires.
- Sync the server clock with NTP.
- If the key was revoked or leaked, generate a new one and update every server that uses it.
Stop it coming back
- Build tokens in one function with a unit test that decodes the result and checks each claim.
- Log the decoded header and payload (never the key) when a 401 happens.
If you use BillDog
BillDog builds and signs these tokens for you from the In-App Purchase key you save in Project Settings → Store → Apple App Store (step 5.2 in the setup guide), so you don't write JWT code. If verification fails there, BillDog's guide lists the same checks:
- The In-App Purchase key is saved, not only the App Store Connect API key.
- The
.p8contents are complete, including the BEGIN and END lines. - The Issuer ID, Key ID, Bundle ID and Apple ID match exactly.
- The key isn't revoked.
Related
Sources
Did this fix it?
Frequently asked questions
Can I use my App Store Connect API key for the App Store Server API?
Use the In-App Purchase key for the App Store Server API. It's a different key, created under Users and Access → Integrations → In-App Purchase, and its token needs the bid claim with your app's bundle ID.
How long can the token live?
Apple rejects tokens whose exp is more than 60 minutes after iat. Generate a new token at least hourly, and keep your server's clock in sync.
Does a 401 mean I hit the wrong environment?
Not usually. A transaction looked up in the wrong environment comes back as not found, not 401. A 401 is about the token itself.