Web APIs
Authentication Basics
Master the core patterns that protect API endpoints and verify user identity across millions of applications.
GitHub serves over 100 million developers through its APIs every day. Each API call carries credentials that determine whether you get data or a 401 Unauthorized error. Without authentication, APIs would be chaos—anyone could access anything, modify user data, or drain system resources. That fundamental security gate keeps the modern web functioning.Authentication answers one critical question: who is making this request? It sits at the entry point of every protected API endpoint, validating credentials before any business logic runs. Think of it as checking IDs at a concert venue—no valid ticket means no entry, regardless of what happens inside.
The challenge grows complex fast. APIs serve mobile apps, web browsers, server-to-server integrations, and automated scripts. Each client type needs different authentication approaches. A banking app requires stronger security than a weather API. Real-time chat needs fast credential checks while payment processing demands bulletproof validation.
The Authentication Problem Space
Every API request arrives as anonymous traffic until proven otherwise. Your server sees an IP address, some headers, and a request body. It has no inherent way to know whether this comes from a legitimate user, a malicious bot, or an automated script running wild.Traditional web applications handle this through sessions. Users log in once, the server creates a session ID, stores user data in memory or a database, and sends the session ID back as a cookie. Every subsequent request includes that cookie, letting the server look up user information.
APIs break this model completely. Mobile apps cannot rely on cookies. Server-to-server communication has no concept of browser sessions. Single-page applications make hundreds of API calls from JavaScript code that traditional session management cannot handle efficiently.
Modern API authentication solves this through stateless credentials. Instead of server-side sessions, each request carries everything needed to verify the user. The server validates credentials without looking up external state, processes the request, and forgets about the interaction.
Core Authentication Patterns
Authentication patterns evolved to handle different security requirements and client capabilities. Understanding the five core approaches helps you choose the right method for specific scenarios.The pattern works for internal tools, development environments, or APIs where HTTPS provides the real security layer. Many CI/CD systems and developer tools use Basic Auth because it requires no complex token management—just embed credentials directly in HTTP headers.
GET /api/users/profile HTTP/1.1
Host: api.apiforge.com
Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=
Content-Type: application/json
# The APIForge DevOps team uses Basic Auth for internal monitoring tools
# Base64 decode reveals: username:password
# Simple but requires HTTPS to prevent credential theft| Authentication Method | What it does | APIForge use case |
|---|---|---|
| Basic Authentication | Sends base64 encoded username:password in header | Internal monitoring dashboard API access |
| API Keys | Uses unique tokens to identify API consumers | Developer platform access and rate limiting |
| Bearer Tokens | Carries access tokens representing user sessions | Mobile app user authentication |
| OAuth 2.0 | Delegates authentication to external providers | Sign in with Google/GitHub integration |
| JWT (JSON Web Tokens) | Self-contained tokens with embedded user data | Microservices user context sharing |
Stripe pioneered this approach for payment APIs. Developers get publishable keys for frontend code and secret keys for server-side operations. The key structure immediately tells Stripe what environment and permissions apply. This pattern now dominates SaaS APIs because it separates user accounts from API access cleanly.
GET /api/projects HTTP/1.1
Host: api.apiforge.com
X-API-Key: af_live_sk_2Qq8x9PmN4vK8jW3R5tY7sC1mH6fD9eL
Content-Type: application/json
# APIForge Backend team issues API keys for external developer access
# Keys include environment prefix (af_live_) and permission scope (sk_)
# Server validates key and applies associated rate limits and permissionsBearer Token Authentication
Bearer tokens solve the core problem of user-specific API access. Instead of sending actual credentials with each request, users authenticate once to receive a token representing their session. The token carries enough information to verify identity and permissions without database lookups.The flow works like a concert wristband system. You show ID at the entrance, get a wristband, then use only the wristband for access throughout the event. API clients authenticate with username/password once, receive a bearer token, then include that token in subsequent requests until it expires.
Twitter's API exemplifies this pattern. Mobile apps authenticate users through OAuth, receive access tokens, then make API calls with those tokens. The tokens expire periodically, forcing re-authentication and limiting damage if credentials leak.
// APIForge dashboard login flow
const response = await fetch('/api/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
email: 'dev@startup.com',
password: 'secure_password'
})
});
const { access_token } = await response.json();
localStorage.setItem('token', access_token);Authentication Headers and Standards
HTTP headers carry authentication data in standardized formats that servers and clients universally understand. The Authorization header serves as the primary mechanism, with specific schemes defining how credentials are formatted and processed.The header follows a simple pattern: Authorization: <scheme> <credentials>. The scheme tells the server how to interpret the credentials. Basic means base64-encoded username:password. Bearer indicates a token-based system. Custom schemes handle proprietary authentication methods.
Multiple authentication schemes can coexist in the same API. GitHub's API accepts both personal access tokens and OAuth tokens through the Authorization header, plus legacy username/password through Basic auth. The server examines the scheme and routes to appropriate validation logic.
# Different authentication schemes for the same APIForge endpoint
# Bearer token (JWT)
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9
# Basic authentication
Authorization: Basic dGVzdEBhcGlmb3JnZS5jb206cGFzc3dvcmQ=
# API key (custom scheme)
Authorization: APIKey af_live_sk_2Qq8x9PmN4vK8jW3R5tYAuthentication vs Authorization
Authentication and authorization work together but solve different problems. Authentication asks "who are you?" while authorization asks "what can you do?" The distinction becomes critical when building APIs that serve users with different permission levels.Think of airport security: authentication happens when you show ID at check-in—proving you are who you claim to be. Authorization happens at the gate when they verify your boarding pass allows access to that specific flight. Many systems conflate these concerns, leading to security vulnerabilities and inflexible permission models.
APIs handle this separation through layered validation. Authentication middleware verifies credentials and identifies the user. Authorization middleware checks whether that user can perform the requested action on the specified resource. The separation enables fine-grained permissions and audit trails.
Security Considerations
API authentication faces unique security challenges that traditional web authentication never encounters. Mobile clients store credentials locally where users can extract them. Server-to-server communication crosses network boundaries where packets might be intercepted. Token-based systems need defense against replay attacks and credential stuffing.The fundamental principle: treat all network traffic as hostile. HTTPS provides transport security but cannot protect against application-layer attacks. Stolen tokens work perfectly until they expire. Weak authentication schemes enable account takeovers that compromise entire user bases.
Rate limiting becomes essential for authentication endpoints. Password-based login attempts can be brute-forced if servers process unlimited requests. Token validation endpoints can be overwhelmed by replay attacks. Smart attackers distribute attacks across multiple IP addresses, bypassing simple rate limiting.
The APIForge Security team implements defense in depth: HTTPS for transport security, bcrypt for password hashing, JWT tokens with short expiration, rate limiting on authentication endpoints, and monitoring for suspicious patterns. Each layer provides partial protection, but together they create robust security.
Implementation Patterns
Authentication implementation varies dramatically based on client types and security requirements. Single-page applications need different approaches than mobile apps, which differ from server-to-server integration. Understanding common patterns helps choose appropriate authentication strategies.Web applications typically use session-based authentication with API fallbacks. Users authenticate through traditional login forms that create server sessions. JavaScript code accesses APIs through session cookies or by exchanging session data for API tokens. This hybrid approach provides familiar UX while enabling API access.
Mobile applications require token-based authentication exclusively. Apps cannot rely on cookies or server sessions. Users authenticate once during app login, receive long-lived refresh tokens stored in secure device storage, and exchange refresh tokens for short-lived access tokens as needed. The pattern enables offline-capable apps with secure credential storage.
// APIForge mobile app authentication flow
class AuthManager {
async login(email, password) {
const response = await fetch('/api/auth/token', {
method: 'POST',
body: JSON.stringify({ email, password })
});
const { access_token, refresh_token } = await response.json();
await this.storeTokens(access_token, refresh_token);
return access_token;
}
}The choice between authentication patterns depends on your specific requirements: user experience needs, security requirements, client capabilities, and operational complexity. Most modern applications implement multiple patterns—OAuth for third-party integrations, JWT tokens for mobile apps, and API keys for server-to-server communication.
Quiz
1. The APIForge Backend team needs to authenticate mobile app requests that cannot use browser cookies. What makes an authentication method "stateless" for APIs?
2. Why is Basic Authentication considered insecure compared to Bearer tokens?
3. The APIForge Security team wants to implement proper access control for their developer platform. What is the key difference between authentication and authorization?