Prompt
I'm building an API product, how do I set up auth and permissions?
Latest observation
A good API auth/permissions setup usually has two layers:
- Authentication: who is calling?
- Authorization: what are they allowed to do?
Here’s a practical way to design it.
1) Pick the right auth model for each client type
Machine-to-machine APIs
Use API keys or, better, OAuth2 Client Credentials.
-
API keys
- Simple to issue and use
- Easy for early products
- Weaker security if a key leaks, because the key often directly identifies access level
-
OAuth2 Client Credentials
- Better for production and larger ecosystems
- Supports token expiration, scopes, rotation, and revocation
- Recommended if you expect enterprise customers or multiple services
User-facing apps
Use OAuth2 Authorization Code + PKCE.
- Best for web/mobile apps where a user logs in
- Lets you represent both the app and the user
- Works well with consent and delegated access
Internal service-to-service
Use mTLS, JWTs, or an internal identity platform.
- If you have microservices, don’t rely on shared static secrets forever
- Prefer short-lived credentials and service identities
2) Use short-lived tokens
Whether you use OAuth2 or signed JWTs, prefer:
- Access tokens: short-lived, e.g. 5–15 minutes
- Refresh tokens: longer-lived, stored securely, only for user flows
Why:
- Limits blast radius if leaked
- Easier to revoke by waiting for expiry
- Encourages safer key rotation
3) Define permissions with scopes and roles
Scopes
Scopes are best for API-level permissions.
Examples:
read:orderswrite:ordersread:customersadmin:billing
Use scopes when you want:
- Fine-grained access control
- Clear documentation for developers
- Consent screens in user-delegated flows
Roles
Roles are useful for your own product admin model.
Examples:
owneradminmemberbilling
Roles are easier for humans, but less precise than scopes. A common pattern is:
- Roles in your admin UI / tenant management
- Scopes in the token used by the API
4) Tie permissions to resources and tenants
If your product is multi-tenant, don’t just ask “is this token valid?” Also ask:
- Which organization/account/tenant does this token belong to?
- Does this user or client have access to this specific resource?
Example checks:
- Token is valid
- Token has
read:invoices - Token’s tenant ID matches the invoice’s tenant ID
- Token owner has access to that project/customer/workspace
This prevents accidental cross-tenant access.
5) Decide where auth is enforced
Best practice:
- Enforce authentication at the gateway / edge
- Enforce authorization again in the application/service layer
Why both?
- Edge layer is great for standard checks: token validity, rate limits, IP allowlists
- App layer is where you verify business rules: ownership, tenant boundaries, object-level permissions
Never rely only on frontend checks.
6) Keep secrets secure
For API keys / client secrets:
- Store hashed secrets if possible
- Show the secret only once at creation
- Support rotation
- Allow multiple active keys during migration
- Let users revoke keys instantly
For tokens:
- Don’t log full tokens
- Redact in observability tooling
- Use HTTPS everywhere
- Consider signing tokens, not encrypting unless necessary
7) Design for revocation and rotation
You need a plan for:
- Lost credentials
- Compromised credentials
- Offboarding customers
- Emergency revocation
Good practices:
- Key/token expiration
- Manual revocation
- Key versioning
- Overlap periods for rotation
- Audit logs for creation/use/revocation
8) Add rate limits and abuse controls
Auth is not enough. Also add:
- Per-key/per-user rate limits
- Burst control
- Quotas by plan
- IP/device anomaly detection
- Optional allowlists for enterprise customers
9) Give developers a clear auth UX
Your docs should answer:
- How do I authenticate?
- What headers do I send?
- How do I get a token?
- What scopes do I need?
- How do I rotate credentials?
- How do I revoke access?
- What error codes should I expect?
Example error responses:
401 Unauthorized= missing/invalid auth403 Forbidden= valid auth, insufficient permissions- Include a machine-readable error code like
insufficient_scope
10) A solid default architecture
If you’re starting a serious API product, this is a strong default:
For customer integrations
- OAuth2 Client Credentials
- Short-lived JWT access tokens
- Scopes for permissions
- Tenant/org ID in token claims
- Refresh/rotation/revocation support
For user-facing apps
- OAuth2 Authorization Code + PKCE
- Scopes
- User + tenant claims
- MFA optional for sensitive actions
For internal services
- Service identities
- mTLS or workload identity
- Short-lived signed tokens
- Least privilege
11) Common mistakes to avoid
- Using one global API key for everything
- Putting all permissions into the frontend
- Making tokens long-lived and non-expiring
- Using roles only, with no resource-level checks
- Not supporting key rotation
- Not distinguishing
401vs403 - Not separating user identity from tenant access
- Logging secrets
12) If you want the simplest good version
Start with:
- API keys for machine clients
- OAuth2 Authorization Code + PKCE for user clients
- Scopes for permissions
- Tenant-based access control
- Short-lived tokens
- Revocation + rotation
- Rate limiting
Then evolve to:
- OAuth2 client credentials
- Fine-grained RBAC/ABAC
- mTLS for internal traffic
- Advanced audit and anomaly detection
If you want, I can also give you:
- a recommended auth architecture diagram,
- a database schema for users/roles/scopes/api keys, or
- example middleware code for Express/FastAPI/Go/Spring.