Test API key rotation without downtime

Keys leak, people leave, and compliance asks for rotation every 90 days. If your API issues keys to customers, rotation has to work without breaking anyone: the customer creates a new key, deploys it, and only then revokes the old one.

That sequence has four states, and each is worth a test.

Keep keys out of the collection

The keys are secrets, and the test creates and revokes real ones. Keep them in environment variables, set by scripts during the run, and never typed into requests:

HTTP
GET {{baseUrl}}/v1/account
Authorization: Bearer {{apiKey}}

The folder sets apiKey before each step, so the same request is sent with different keys. Requests read values from the environment through {{apiKey}}; values a script sets are passed to the requests after it in the same run and are not saved.

The folder

Output
📂 Key rotation
  POST   Create key A            (admin token)
  GET    Call with key A
  POST   Create key B            (admin token)
  GET    Call with key A (both exist)
  GET    Call with key B (both exist)
  DELETE Revoke key A            (admin token)
  GET    Call with key A (revoked)
  GET    Call with key B (after revoke)
  DELETE Revoke key B            (clean up)

Create key A uses an admin or account-owner token from a secret environment variable, and stores what comes back:

JavaScript
const key = pm.response.json();
pm.environment.set('keyAId', key.id);
pm.environment.set('keyA', key.secret);

Most APIs show a key's secret only once, at creation. Check that, too: the GET /keys/{{keyAId}} response should not include secret.

Each "Call with…" request sets the key it uses in a pre-request script:

JavaScript
pm.environment.set('apiKey', pm.environment.get('keyA'));

The checks

JavaScript
// "Call with key A (both exist)" and "Call with key B (both exist)"
pm.test('key works during the overlap', () => pm.response.to.have.status(200));
JavaScript
// "Call with key A (revoked)"
pm.test('revoked key is refused', () => pm.response.to.have.status(401));
pm.test('client is told why', () => {
  pm.expect(pm.response.text().toLowerCase()).to.match(/revoked|invalid|expired/);
});
pm.test('no data in the error', () => {
  pm.expect(pm.response.json()).to.not.have.property('account');
});
JavaScript
// "Call with key B (after revoke)"
pm.test('new key unaffected by revoking the old one', () => pm.response.to.have.status(200));

The last check catches a revoke that's implemented as "revoke all keys of this account".

Caching

Gateways often cache key lookups for a minute or more. That means a revoked key can keep working for a while. Decide how long is acceptable, and test it honestly: either check right away and expect 401, or put a delay before the revoked call that matches the cache lifetime and document it. A test that passes only because the run happens to be slow will fail on a fast day.

Other cases

  • Revoke twice: the second call is 404 or 204, not 500.
  • Wrong account: revoking another account's key id with your admin token must be 404. This is the wrong-user test for keys.
  • Scopes: a key created with read-only scope gets 403 on a write, and the error names the missing scope.
  • Expiry: if keys can have an expiry date, create one that expires in a minute, and test it after.

Rotating your own keys

The same idea applies to the keys your collection uses to call other APIs. Keep them in a secret manager and fetch them at run time, so rotating one doesn't mean editing collections. See AWS Secrets Manager, Azure Key Vault or Google Cloud Secret Manager.