What is the difference between REST and RESTful API?

👨‍💻 Frontend Developer 🟡 Often Asked 🎚️ Medium
#API #REST #Backend

Brief Answer

REST and RESTful API are related but different concepts:

  1. REST — architectural style, set of principles 📋
  2. RESTful API — concrete implementation of REST principles 🔧
  3. Theory vs practice — REST concept, RESTful implementation ⚖️
  4. Strictness — REST strict rules, RESTful flexible implementation 📏
  5. HTTP methods — RESTful uses GET, POST, PUT, DELETE 🌐
  6. Stateless — both without state between requests 🔄
  7. Resources — both work with resources through URLs 🎯
// REST - architecture principles
// RESTful API - practical implementation
GET /api/users/123     // ✅ RESTful implementation of REST principles
POST /getUserById      // ❌ Not RESTful, doesn't follow REST

Full Answer

REST and RESTful API are often confused, but they’re different levels of abstraction! Understanding the differences helps create better APIs. 🚀

What is REST?

REST (Representational State Transfer) — architectural style:

// REST - set of principles:
// 1. Client-Server architecture
// 2. Stateless
// 3. Cacheable
// 4. Uniform Interface
// 5. Layered System
// 6. Code on Demand

What is RESTful API?

RESTful API — concrete implementation of REST principles:

// RESTful API - practical implementation
const api = {
  getUsers: () => fetch('/api/users'),           // GET
  createUser: (data) => fetch('/api/users', {    // POST
    method: 'POST',
    body: JSON.stringify(data)
  }),
  updateUser: (id, data) => fetch(`/api/users/${id}`, { // PUT
    method: 'PUT',
    body: JSON.stringify(data)
  }),
  deleteUser: (id) => fetch(`/api/users/${id}`, { // DELETE
    method: 'DELETE'
  })
};

Main differences

1. Level of abstraction

// REST - conceptual level
// Defines WHAT should be
 
// RESTful - practical level
// Defines HOW to implement it

2. Strictness of following principles

REST (strict principles):

// Must comply with ALL 6 principles
// Complete stateless architecture
// Mandatory cacheability
// Strict uniform interface

RESTful (flexible implementation):

// Can partially follow principles
// Practical compromises
// Adaptation to real tasks

3. HTTP methods usage

RESTful API examples:

// Correct HTTP methods usage
GET    /api/products        // Get all products
GET    /api/products/123    // Get product by ID
POST   /api/products        // Create new product
PUT    /api/products/123    // Update product completely
PATCH  /api/products/123    // Partially update product
DELETE /api/products/123    // Delete product

Non-RESTful examples:

// Incorrect usage
POST /api/getProducts       // ❌ GET in POST
GET  /api/deleteUser/123    // ❌ DELETE in GET
POST /api/updateUserName    // ❌ UPDATE in POST

REST principles in RESTful implementation

1. Stateless

// RESTful stateless implementation
// Each request contains all necessary information
const headers = {
  'Authorization': 'Bearer token123',
  'Content-Type': 'application/json'
};
 
fetch('/api/user/profile', { headers }); // ✅ All info in request

2. Uniform Interface

// RESTful uniformity
const apiEndpoints = {
  users: '/api/users',
  products: '/api/products', 
  orders: '/api/orders'
};
 
// Same operations for all resources
// GET, POST, PUT, DELETE work the same way

3. Resource-based

// RESTful resources
/api/users          // Collection of users
/api/users/123      // Specific user
/api/users/123/posts // User's posts
 
// Not RESTful
/api/getUserById/123     // ❌ Action in URL
/api/createNewUser       // ❌ Action in URL

Practical differences

REST theory vs RESTful practice

// REST theory - ideal world
// All 6 principles mandatory
// Complete stateless architecture
// Strict standard compliance
 
// RESTful practice - real world
// Compromises for performance
// Adaptation to business requirements
// Practicality over purity

Examples of compromises

// RESTful compromises
// Client-side caching (violates stateless)
localStorage.setItem('userToken', token);
 
// Batch operations (deviation from uniform interface)
POST /api/users/batch-update
 
// Search with parameters (URL flexibility)
GET /api/products?search=laptop&category=electronics

RESTful maturity assessment

Richardson Maturity Model:

// Level 0 - Plain Old XML (POX)
POST /api/endpoint
// One URL, one HTTP method
 
// Level 1 - Resources
GET /api/users
GET /api/products
// Multiple URLs, one HTTP method
 
// Level 2 - HTTP methods
GET    /api/users     // ✅ RESTful starts here
POST   /api/users
PUT    /api/users/123
DELETE /api/users/123
 
// Level 3 - HATEOAS
{
  "id": 123,
  "name": "John",
  "_links": {
    "self": "/api/users/123",
    "posts": "/api/users/123/posts"
  }
}

RESTful API design

Correct URL structure

// ✅ RESTful URL structure
/api/v1/users                    // Collection
/api/v1/users/123               // Item
/api/v1/users/123/posts         // Nested collection
/api/v1/users/123/posts/456     // Nested item
 
// ❌ Not RESTful
/api/getUser?id=123
/api/createUser
/api/user_delete/123

HTTP status codes

// RESTful status codes usage
const responses = {
  200: 'OK - successful GET, PUT',
  201: 'Created - successful POST',
  204: 'No Content - successful DELETE',
  400: 'Bad Request - client error',
  401: 'Unauthorized - no authorization',
  404: 'Not Found - resource not found',
  500: 'Internal Server Error - server error'
};

Tools and validation

RESTful compliance checking

// Tools for checking RESTful API
// - Postman Collections
// - Swagger/OpenAPI
// - REST API validators
 
// Evaluation criteria:
// ✅ Uses HTTP methods correctly
// ✅ Resources in URLs, not actions
// ✅ Correct status codes
// ✅ Consistent response structure

Modern alternatives

// GraphQL - RESTful alternative
query {
  user(id: 123) {
    name
    posts {
      title
    }
  }
}
 
// gRPC - for high-performance APIs
// WebSocket - for real-time communication

Best practices

  1. Use nouns in URLs, not verbs 📝
  2. HTTP methods for resource actions 🔧
  3. API versioning — /api/v1/ 🔄
  4. Correct status codes — informative responses 📊
  5. Documentation — OpenAPI/Swagger 📚

Common mistakes

Wrong:

GET /api/deleteUser/123    // Action in URL + wrong method
POST /api/users/getAll     // GET operation via POST

Correct:

DELETE /api/users/123      // Correct method and resource
GET /api/users             // Correct method for retrieval

Conclusion

Key differences between REST and RESTful API:

  • REST — architectural principles and theory
  • RESTful API — practical implementation of principles
  • Flexibility — RESTful allows reasonable compromises
  • Goal — creating understandable and scalable APIs

Strive for RESTful implementation with understanding of REST principles! 🎯