## TL;DR
A ScraperAPI 401 right after rotating keys means the pipeline is still sending the old key, or the new key has not propagated. Verify which key the failing requests actually carry, wait a few minutes for propagation, and confirm with a minimal request before rerunning the batch.

## The error
```text
HTTP 401 Unauthorized
{"error": "Unauthorized: invalid API key"}
Key rotation completed 2 minutes ago; requests still failing.
```

## Fix it
1. Log the first 8 characters of the key the failing requests actually send and compare with the new key in the dashboard. Mismatches mean the deploy did not pick up the rotation.
   Expected: Expected: the comparison shows whether the pipeline uses the old or the new key.
2. If the pipeline already sends the new key, wait 5 minutes and retry; ScraperAPI key propagation is not always instant.
   Expected: Expected: the retry succeeds once propagation completes.
3. Test with a minimal curl using the new key against a simple URL to isolate key validity from pipeline config.
   Expected: Expected: curl 200s, proving the key works and the pipeline config is the issue, or curl 401s, proving the key itself is bad.
4. After fixing, add a key-validation probe at pipeline startup and version your secrets so rotations are atomic.
   Expected: Expected: the next rotation either works immediately or fails fast at deploy.

## When to use
- Your ScraperAPI calls 401 immediately after a key rotation.
- You need to tell propagation delay apart from a config problem.
- You rotate scraper keys on a schedule.

## When not to use
- The 401 predates the rotation - then the old key was already dead.
- The error is 402 or 429 - those are billing and rate limits, not the key.
- Multiple services share the key - check that all of them were updated.

## Why it happens
Key rotations have two failure modes: the new secret never reached the running workers (stale config, cached env), or the provider needs minutes to activate the new key across its edge. Both look like an invalid key from the outside.

## Variant phrasings
### scraperapi 401 invalid api key
Same failure and fix; some searchers phrase the query this way.
### scraperapi unauthorized after key rotation
Same failure and fix; some searchers phrase the query this way.

## Edge cases
- Some clients cache DNS or connections that pin to an edge node with stale key state; a fresh process can succeed.
- Rotating the key twice quickly can leave the first new key invalid; always use the latest.

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_X1G1CnLTJSTvMkp70w1iDQ
