REST APIs: Simple Communication for Powerful Apps

Understand REST APIs, the simple and flexible way for applications to talk to each other. Learn advantages, disadvantages, common use cases, and HTTP methods (GET, POST, PUT, etc.) with clear examples.
REST APIs: Simple Communication for Powerful Apps

What is a REST API?

REST API is a way for different computer programs to talk to each other over the internet. It's like a waiter in a restaurant, taking orders from customers and bringing food from the kitchen.
REST stands for:

  • RE - REpresentational
  • S - State
  • T - Transfer

It's an architectural style for designing networked applications. The idea is that you represent the state of resources (like data objects) and transfer that representation between client and server.

Advantages

  • Easy to understand and use. Example: A weather app uses a REST API to get current temperature data. It simply sends a request to openweathermap.org and receives the temperature in response https://openweathermap.org/current.
  • Works with many programming languages. Example: The same weather API can be used by an iOS app written in Swift, an Android app in Java, and a website using Rust/ Go/ Python/ Ruby/ PHP/ JavaScript, etc.
  • Scalable - can handle lots of users. Example: Twitter's REST API can handle millions of requests from various apps and websites accessing tweets simultaneously.
  • Stateless - each request is independent. Example: When you check your bank balance through a banking app, each request for your balance is complete on its own, not relying on previous requests.

Disadvantages

  • Can be slower for complex operations. Example: To get a user's profile, friends list, and recent posts on a social media platform, you might need three separate API calls, which takes longer than a single call.
  • May require multiple requests for related data. We can fetch data with GraphQL: A Modern Approach to APIs https://vulehuan.com/en/blog/2024/7/data-fetching-with-graphql-a-modern-approach-to-apis-6688a92fc9dec2dec37a1b88.html
  • Not ideal for real-time updates. Example: A stock trading app using REST API might not show price changes instantly, as it needs to keep making new requests to get updates.

When to use REST API

  • Building mobile or web applications. Example: A recipe app that fetches recipes from a central database.
  • Creating public APIs for others to use. Example: Google Maps API, allowing developers to integrate maps into their own apps.
  • When you need simple, standard communication between systems. Example: A weather widget on a news website that gets data from a weather service.

When not to use REST API

  • Real-time applications (like chat or gaming). Example: An online multiplayer game needs constant, real-time communication, which is better served by technologies like WebSockets.
  • Complex operations requiring multiple steps. Example: A banking system processing a mortgage application, which involves multiple checks and operations.
  • When you need to maintain state between requests. Example: An online shopping cart that needs to remember what items you've added across multiple page views.

REST APIs are widely used because they're simple and flexible. They're great for many projects, but not always the best choice for everything. Consider your specific needs when deciding whether to use a REST API.

Main HTTP methods used in REST APIs

  • GET
    • Purpose: Retrieve data
    • Example: GET /users/123 (fetches data for user with ID 123)
  • POST
    • Purpose: Create new data
    • Example: POST /users (creates a new user)
  • PUT
    • Purpose: Update existing data (replace entire resource)
    • Example: PUT /users/123 (updates all fields for user 123)
  • PATCH
    • Purpose: Partially update existing data
    • Example: PATCH /users/123 (updates only specified fields for user 123)
  • DELETE
    • Purpose: Remove data
    • Example: DELETE /users/123 (deletes user with ID 123)
  • HEAD
    • Purpose: Similar to GET, but retrieves only headers, not body
    • Example: HEAD /users/123 (checks if user 123 exists)
  • OPTIONS
    • Purpose: Describes communication options for the target resource
    • Example: OPTIONS /users (returns available methods on /users endpoint)

These methods follow the CRUD (Create, Read, Update, Delete) operations that are fundamental to interacting with most data storage systems.

Remember, the exact implementation can vary, but these are the standard methods used in RESTful APIs. The choice of method depends on what you want to do with the data.

Example

Implements the GET operation to retrieve and display books from the REST API:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
</head>
<body>
  <h1>Book List</h1>
  <ul id="bookList"></ul>
  <script>
  const apiUrl = 'http://localhost:3000/api/books';
  $(document).ready(function(){
    $.get(apiUrl, function(books){
      books.forEach(function(book) {
        $('#bookList').append(`<li>${book.title} by ${book.author}</li>`);
      });
    });
  });
  </script>
</body>
</html>