API Keys, Sessions, and Service Credentials Explained

Compare API keys, browser sessions, and service credentials, then choose storage, rotation, and transport practices that fit each API client.

published: reading time: 7 min read author: GeekWorkBench
Quick Summary

API keys, browser sessions, and service credentials identify different kinds of callers and need different storage, permissions, and rotation practices. This guide compares those credential types, explains how to validate identity before authorizing an action, and covers safe rotation, revocation, telemetry, and session protections. Use its examples and checklists to choose a credential for each client and limit the impact if a secret leaks.

API Keys, Sessions, and Service Credentials Explained

Introduction

A reporting integration might send a project API key with each request:

GET /v1/reports HTTP/1.1
X-API-Key: key_example_123

That key can identify the integration or enforce a quota, but it does not decide which report the caller may access. A browser session represents a signed-in user, while a service credential represents a workload. Validate the credential, then authorize the requested action and resource. OAuth and OpenID Connect explains user and delegated identity flows.

Choose credentials for the client

  • Browser application: Use a session cookie or a suitable short-lived identity token. For cookies, set HttpOnly, Secure, and an appropriate SameSite policy; protect state-changing requests against CSRF.
  • Service workload: Prefer workload identity or short-lived service tokens when the platform supports them. Give each workload its own identity and narrowly scoped permissions.
  • Partner or project integration: An API key can identify the integration and support quotas. Scope it, store it as a secret, and avoid treating it as a human user’s identity or authorization policy.

Credential validation and authorization flow

flowchart TD
  A[Client presents credential] --> B[Validate credential, expiry, and status]
  B --> C[Resolve caller identity and scope]
  C --> D[Authorize requested action and resource]
  D -->|Allowed| E[Process request]
  D -->|Denied| F[Return safe error]
  E --> G[Record outcome without secrets]

Implementation practice

Validate credentials at the API boundary before handlers use the caller identity. Check authorization separately for the requested action and object; never trust identity or permission claims supplied in ordinary request data. Keep secrets out of examples and logs, and add tests for expired, revoked, and insufficiently scoped credentials as well as the allowed path.

Rotating credentials without service interruption

For a planned rotation, issue a replacement credential while the current one is still valid, then give the caller a clear migration window. Track credentials by a non-secret identifier so telemetry can show which key is still in use without logging its value. Once callers have moved, revoke the old credential and confirm that rejected uses are understood.

Keep the overlap bounded and assign an owner and deadline. If the replacement fails, make rollback explicit: pause revocation, restore the previous credential only if it remains safe, and resolve the cause before extending the overlap. Suspected compromise is different from routine rotation; revoke or disable the exposed credential promptly and use the incident process rather than waiting for ordinary migration.

Production failure scenarios and mitigations

  • A rotated or expired credential causes a caller outage. Monitor rejection rates by non-secret credential ID and use a bounded overlap for planned rotation.
  • Several workloads share one API key. Issue separate credentials so a leak can be traced and contained without disabling unrelated callers.
  • A secret appears in logs or an error report. Redact values at ingress and in application telemetry, restrict log access, and rotate the exposed credential through the incident process.
  • A revoked session remains usable. Invalidate the session server-side at logout or privilege changes, and test that stale session identifiers are rejected.

Trade-off Analysis

Credential type Benefit Cost or risk
Static API keys Simple for project integrations and quotas Long-lived bearer secrets need storage, scope, and rotation
Short-lived workload tokens Limits the useful window after a leak Identity issuance and renewal must stay available
Browser session cookies HttpOnly cookies keep credentials out of JavaScript Automatic sending adds CSRF defenses and session lifecycle work

Observability checklist

  • Track authentication success and rejection by credential type and non-secret identifier.
  • Monitor expiration, revocation, rotation overlap, and unusual use from new clients or locations.
  • Correlate failures with request IDs while redacting secret values from logs and traces.
  • Alert on unexpected rejection spikes and review dashboards after credential changes.

Security and Compliance Notes

Use TLS and least privilege. Validate credentials at the server boundary, then authorize the requested action and resource; apply the same checks to alternate routes and batch operations. Keep credentials and sensitive payloads out of URLs, examples, analytics, and error messages. Record who or which workload issued, rotated, or revoked a credential, along with its scope and timestamp, but never store the secret value in audit records. Set access and retention rules for those records to match service policy. For browser sessions, rotate the session identifier after login or privilege changes and invalidate it at logout. Give each integration its own API key so a suspected leak can be traced and contained without disrupting unrelated callers.

Quick Recap Checklist

  • Use user sessions for user-facing authentication and service credentials for workloads.
  • Give each credential only the access its caller needs.
  • Store secrets outside source code and redact them from logs.
  • Set up rotation, revocation, and monitoring for unusual use.

Interview Questions

1. How does an API key differ from a user access token?
An API key often identifies a project or integration and may support quotas. A user access token carries delegated authority for a particular resource server and scope; neither credential removes the need for resource-level authorization.
2. Why might a server store a verifier instead of the original API key?
A one-way verifier lets the server check a presented key without retaining a usable copy of the secret. A leak of the verifier store is then less likely to expose credentials that can be replayed directly.
3. Why give each workload its own service credential?
Separate credentials limit access to each workload's needs and make usage attributable. If one is compromised, it can be revoked without disrupting unrelated services.
4. How can a planned credential rotation avoid downtime?
Issue a replacement before revoking the old credential, move callers during a bounded overlap, and monitor old-key use by credential identifier. Revoke the old value after migration is confirmed.
5. What should a team do differently when a credential is suspected to be compromised?
Contain the exposure by revoking or disabling the credential promptly, assess affected access, and issue a replacement through the incident process. Do not wait for the ordinary rotation window if the secret is actively at risk.
6. Why do cookie-based browser sessions need CSRF defenses?
Browsers attach eligible cookies automatically, including when a request is initiated from another site. Protect state-changing operations with appropriate defenses such as SameSite cookie policy, CSRF tokens, and origin checks.

Further Reading

Conclusion

Use sessions for people, workload credentials for services, and scoped API keys for integrations that need them. Keep each credential attributable, monitor its use, and make rotation and revocation part of its lifecycle so one caller’s secret does not become everyone else’s risk.

Category

Related Posts

CSRF, CORS, and SSRF: Defending Web Request Boundaries

Learn how CSRF, CORS, and SSRF differ, then apply cookie, origin, allowlist, and egress controls to protect browser and server request boundaries.

#web-security #csrf #cors

API Authentication vs. Authorization: Identity and Access

Understand API authentication and authorization, how they differ in request handling, and how to avoid common identity and access-control mistakes.

#api-security #authentication #authorization

API Input Validation, Secrets, and Sensitive Data

Validate API inputs at trust boundaries, handle secrets safely, and limit sensitive data exposure in logs, storage, and error responses.

#api-security #input-validation #secrets