LearnThatStack Ace your next interview
API Design · question
Question 33 of 92

How do you implement API key management?

intermediate Pro
← All API Design questions

The full answer covers the core mechanics, when this pattern wins, and the trade-offs interviewers most often probe. It includes a code sample. Read it once at your own pace, then try to recall the structure from memory before the interview. Pro also includes this question's interview lens - the likely follow-up probes and what you can say in the room.

Full answer + code samples + AI explanations

Unlock to read the complete answer for this premium question.

Guided practice for API Design One question at a time. Answer out loud, get graded, see what you missed.
Related concept

Authentication: proving who is calling

Authentication asks who you are and authorization asks what you may do. Authentication methods differ mainly in where the identity state lives.

For this question · chapter 4 of 4

4/4 A key names an application
api keysA key names an applicationshown once, hashed, scoped, rotatedbilling-workerswitched to k2key lts_k1_9f2ekey lts_k2_c41bAPI server, api_keysk1 retired, k2 livekeysha256scopeuses in 1 hk14c7a1dinvoices:read10k2b83e09invoices:read01
api keysA key names an applicationbilling-workerswitched to k2key lts_k1_9f2ekey lts_k2_c41bAPI server, api_keysk1 retired, k2 livekeysha256scopeuses in 1 hk14c7a1dinvoices:read10k2b83e09invoices:read01

A key names an application

  1. OAuth acts for a user. An API key is for an application instead, with no user involved. The app stores the key. The server stores only the key's sha256 hash, 4c7a1d, beside the key's scope and a use count of 0.
  2. Someone copies the api_keys table out, but every row in that copy is a hash, not a key. A request that presents 4c7a1d, one of those hashes, as its key gets a 401 from the server. A leaked key table gives no access. That protection is the reason we store only the hash.
  3. The server returns 200 for a GET to /invoices when key k1 is in the X-Api-Key header. The server counts that call, so k1's use count becomes 1. The server refuses a POST to /invoices with the same key and returns 403, because k1's scope is invoices:read, which only allows reading. Keys go in a header, not in the URL, because proxies and logs keep URLs.
  4. Key rotation needs an overlap, so you create a second key, k2, and show it once. Both keys work for a while. The app switches to k2, so the number of k1 uses in the last hour drops to 0. Retire k1 then, because that count proves nothing uses k1 anymore.
Chapter 4 · step 1 of 4

OAuth acts for a user. An API key is for an application instead, with no user involved. The app stores the key. The server stores only the key's sha256 hash, 4c7a1d, beside the key's scope and a use count of 0.

1 of 4 Use ← → or swipe See the concept: all 4 chapters in it

© LearnThatStack - diagrams may not be republished without permission.

Want a quick review of the fundamentals? See the API Design cheatsheet.

← Back to all API Design questions
Pro · $10/mo

74 of 92 API Design answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime