Courses 0%
17
Fundamentals of REST api · Chapter 17 of 42

API-Authentication

Akhil
Akhil Sharma
15 min

🔐 17. API Authentication: Proving You Are Who You Say You Are

The Nightclub Scenario: You want to enter an exclusive club. How do they know you're allowed in?

Three Common Methods:


🎫 1. Basic Authentication: The Simple ID Check

Nightclub analogy: "Show me your driver's license every time you want to enter"

How it works:

Step 1: Client sends username + password Username: "john" Password: "secret123"

Step 2: Combine them: "john:secret123"

Step 3: Encode in Base64: "am9objpzZWNyZXQxMjM="

Step 4: Send with EVERY request: GET /api/posts Authorization: Basic am9objpzZWNyZXQxMjM=

Visual representation:

img1

Pros:

✅ Simple to implement

✅ Widely supported

Cons:

❌ Must send credentials with EVERY request

❌ No way to "log out"

❌ If someone intercepts credentials, they have full access

❌ MUST use HTTPS (otherwise passwords visible!)

Real-world use: Rarely used today except for very simple internal tools.

Mental model: Like showing your ID to security every single time you walk through a door - even if they just saw you 5 seconds ago!


🎟️ 2. Session-Based Authentication: The Wristband System

Nightclub analogy: "Here's a wristband - show this instead of your ID each time"

How it works:

Step 1: Login POST /login Body: { "username": "john", "password": "secret123" }

Server verifies and creates session Session ID: "sess_abc123xyz" Stores in database: { sess_abc123xyz: { user_id: 42 } }

Step 2: Server sends back session cookie 200 OK Set-Cookie: session_id=sess_abc123xyz; HttpOnly; Secure

Step 3: Browser automatically sends cookie with future requests GET /api/posts Cookie: session_id=sess_abc123xyz

Server looks up session in database Finds user_id: 42 Returns: 200 OK with data

Step 4: Logout POST /logout Server deletes session from database

Visual representation:

img2

Pros:

✅ Credentials sent only once

✅ Can easily invalidate sessions (logout works!)

✅ Server has full control

Cons:

❌ Server must store session data (memory/database)

❌ Doesn't scale well across multiple servers

❌ Vulnerable to CSRF attacks (need protection)

Real-world use: Traditional web applications, many banking websites.

Mental model: Like a gym membership - you verify your identity once, get a card, then just swipe the card each visit. The gym can deactivate your card anytime.


🎫 3. Token-Based Authentication: The VIP Pass

Nightclub analogy: "Here's a self-contained VIP pass with your info embedded in it"

How it works (JWT - JSON Web Token):

Step 1: Login

bash
json

Step 2: Server creates JWT token

Header:

json

Payload:

json

Server signs it with secret key

Result: "eyJhbGc.eyJ1c2VyX.SflKxwRJ"

Step 3: Client stores token (localStorage/memory)

bash

Body:

json

Step 4: Client sends token with each request

bash

Authorization: Bearer eyJhbGc.eyJ1c2VyX.SflKxwRJ

JWT Structure:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9 ← Header (algorithm) . eyJ1c2VyX2lkIjo0MiwidXNlcm5hbWUiOiJqb2huIn0 ← Payload (data) . SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c ← Signature (verification)

Visual representation:

img3

Pros:

✅ Stateless - no server storage needed!

✅ Scales horizontally (works across many servers)

✅ Can include custom data in payload

✅ Works great for mobile apps and SPAs

✅ Can set expiration times

Cons: ❌ Can't easily invalidate before expiration

❌ Token size larger than session ID

❌ Need to protect from XSS attacks

Real-world use: Modern SPAs, mobile apps, microservices, most modern APIs.

Mental model: Like a driver's license - all your info is embedded IN the card itself. Anyone with the right tools can verify it's real without calling a central database.


🎯 Authentication Comparison Chart

FeatureBasic AuthSession AuthToken Auth
Sends credentialsEvery requestOnly at loginOnly at login
Server storageNoneYes (sessions)None
Scalability⭐⭐⭐⭐⭐⭐
Security⭐ (if HTTPS: ⭐⭐)⭐⭐⭐⭐⭐⭐
Mobile-friendly
Logout capability✅ Easy⚠️ Complex
Modern usageRareCommonVery Common

Request/Response Lifecycle: The Complete Journey

The Restaurant Order: Step by Step

Let's trace a complete API call from start to finish!

Scenario: You want to create a new blog post.

img4

Total time: Usually 50-500 milliseconds!

🎮 Interactive Challenge: Design Your API

Scenario: You're building a social media API. Design the endpoints!

Requirements:

  1. Users should view their feed
  2. Users should post updates
  3. Users should like posts
  4. Users should delete their own posts
  5. Users should view someone else's profile

Think about:

  • What are the HTTP methods?
  • What are the URLs?
  • What is the authentication procedure?
  • What status codes for success/failure?

Suggested Solution:

  1. View Feed GET /api/feed Auth: Bearer token Success: 200 OK Error: 401 Unauthorized

  2. Create Post POST /api/posts Auth: Bearer token Body: { "content": "Hello world" } Success: 201 Created Error: 400 Bad Request, 401 Unauthorized

  3. Like a Post POST /api/posts/42/likes Auth: Bearer token Success: 201 Created Error: 404 Not Found, 401 Unauthorized

  4. Delete Own Post DELETE /api/posts/42 Auth: Bearer token Success: 204 No Content Error: 403 Forbidden (not your post), 404 Not Found

  5. View Profile GET /api/users/john Auth: Bearer token (optional for public profiles) Success: 200 OK Error: 404 Not Found


💡 Common API Design Pitfalls (And How to Avoid Them)

❌ Pitfall 1: Using GET to Modify Data

Bad:

bash

Good:

bash

Why it's wrong: GET should be safe and idempotent. Imagine bookmarking that URL!

❌ Pitfall 2: Returning 200 for Errors

Bad:

bash
json

Good:

bash
json

Why it's wrong: Status codes exist for a reason! HTTP has a rich vocabulary.

❌ Pitfall 3: Inconsistent Naming

Bad:

bash

Why it's wrong: Consistency helps developers predict your API.

❌ Pitfall 4: Not Using HTTPS

Bad:

bash

Good:

bash

Why it's wrong: You're sending data in plain text! Always use HTTPS.

❌ Pitfall 5: Returning Too Much Data

Bad:

bash

Why it's wrong: Expose only what's needed. Privacy and performance matter!


🎯 Quick Self-Test

Without scrolling back, can you answer:

  1. What's the difference between PUT and PATCH?
  2. Name the status code for "resource created successfully"
  3. Which authentication method doesn't require server storage?
  4. What does the 'S' in HTTPS stand for?
  5. If you get a 403 error, what should you do?

Answers:

  1. PUT replaces entire resource, PATCH updates specific fields
  2. 201 Created
  3. Token-based (JWT)
  4. Secure
  5. Nothing - you're authenticated but not authorized. Contact admin.

🚀 Your Next Steps

You're now equipped to:

  • Design RESTful APIs
  • Choose appropriate HTTP methods
  • Interpret status codes
  • Implement authentication

Continue learning:

  • API Documentation: Swagger/OpenAPI specifications
  • Advanced Auth: OAuth 2.0, API Keys, JWTs in depth
  • Rate Limiting: Protecting your API from abuse
  • API Versioning: /v1/ vs /v2/ strategies
  • GraphQL: An alternative to REST
  • WebSockets: Real-time bidirectional communication
  • CORS: Cross-Origin Resource Sharing
  • API Testing: Postman, cURL, automated tests

Practice by:

  • Building your own REST API
  • Exploring public APIs (GitHub, Twitter, weather services)
  • Reading API documentation from major companies
  • Contributing to open-source API projects

Remember: The best way to learn APIs is to build them! Start with a simple CRUD (Create, Read, Update, Delete) API and expand from there.


🎓 Final Mastery Check

You've truly mastered REST APIs if you can:

  • Explain the request/response lifecycle to a friend
  • Debug API errors using status codes
  • Design secure authentication flows
  • Choose the right HTTP method for any operation
  • Understand why REST is called "RESTful"

Congratulations! You now speak the language of modern web applications. APIs power everything from mobile apps to smart home devices - and you now understand how they work!

The internet is essentially millions of APIs talking to each other. You've just learned their language. 🌐


Now that you understand the fundamentals, let's dive into some crucial concepts that you'll encounter in real-world API development.


Authentication Methods Comparison

MethodCredentials sentServer stateScalabilityLogoutBest for
Basic AuthEvery requestNoneHighNot possibleSimple internal tools
Session-BasedOnce (login)Session storeLow (shared state)Easy (delete session)Traditional web apps
Token (JWT)Once (login)None (stateless)HighHard (token still valid until expiry)APIs, mobile apps, SPAs
OAuth 2.0DelegatedAuthorization serverHighRevoke tokenThird-party integrations
API KeyEvery requestNoneHighRevoke keyServer-to-server, public APIs

Key Takeaways

  1. API keys identify the calling application — simple but not suitable for user authentication, easily leaked in client-side code
  2. JWT (JSON Web Tokens) are self-contained, stateless tokens — the server can verify them without a database lookup
  3. OAuth 2.0 enables third-party access without sharing credentials — the standard for "Sign in with Google/GitHub" flows
  4. Always transmit credentials over HTTPS — API keys and tokens in plaintext HTTP can be intercepted by anyone on the network
  5. Use short-lived tokens with refresh tokens for security — limits damage if a token is compromised
Chapter complete!

Course Complete!

You've finished all 42 chapters of

Introduction to System Design

Browse courses
Up next API Path and Query Parameters
Continue