Skip to main content
RESTful Principles
CHAPTER 02 Beginner

Understanding APIs and Web Services

Updated: May 13, 2026
5 min read

# CHAPTER 2

Understanding APIs and Web Services

1. Introduction

In the previous chapter, we established what an API is and why REST is highly favored in the modern web. Now, it's time to dig a little deeper into the mechanics. How exactly do machines talk to one another? What is the difference between a standard web service and an API? In Chapter 2, we will explore the Client-Server architecture, understand how communication flows, and demystify the terminology that backend developers use every day.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Explain the Client-Server architecture model.
  • Define what a Web Service is.
  • Understand the technical difference between an API and a Web Service.
  • Describe the basic lifecycle of API communication.
  • Understand how RESTful communication fits into the broader web ecosystem.

3. Beginner-Friendly Explanation

Think of a traditional transaction at a bank.
  • The Client: You (the customer) walking into the bank with a request (e.g., "I want to withdraw $50").
  • The Server: The bank teller who has access to the vault, verifies your identity, processes your request, and hands you the cash.

In web development, your web browser or mobile app is the Client. The powerful computers sitting in a data center holding the database and logic are the Server. The client asks for something, and the server provides it. This request-and-response cycle is the absolute foundation of the internet and all web APIs.

4. Real-World Examples

  • Client-Server Flow: You open the Instagram app (Client) and refresh your feed. The app sends a request to Instagram's backend (Server). The server gathers the latest photos and sends them back to your phone to be displayed.
  • Web Service vs API: All web services are APIs, but not all APIs are web services. A web service specifically communicates over a network (the internet). The software that connects your computer's operating system to its graphics card is an API, but since it doesn't use the internet, it is *not* a web service.

5. Detailed Code Examples

Let's simulate a basic client-server interaction using simple PHP.

The Server (api.php):

php
1234567891011121314151617
<?php
// This script acts as the server handling the request
header("Content-Type: application/json");

// Check if the client is asking for the 'greeting'
if (isset($_GET[&#039;action']) && $_GET['action'] == 'greeting') {
    echo json_encode([
        "status" => "success",
        "message" => "Hello from the Server!"
    ]);
} else {
    echo json_encode([
        "status" => "error",
        "message" => "Unknown action"
    ]);
}
?>

6. Request/Response Examples

How does the client talk to our api.php server?

Client Request:

http
12
GET /api.php?action=greeting HTTP/1.1
Host: localhost

Server Response:

http
1234567
HTTP/1.1 200 OK
Content-Type: application/json

{
  "status": "success",
  "message": "Hello from the Server!"
}

7. HTTP Examples

When the client and server communicate, they must speak the same language. On the web, this language is HTTP.
http
1234
# The client requesting data
GET /users/profiles HTTP/1.1
Host: api.socialnetwork.com
Accept: application/json

8. JSON Examples

When the server processes a request, it packages the data logically before sending it over the network.
json
12345678
{
  "client_ip": "192.168.1.1",
  "server_response_time_ms": 45,
  "data_payload": {
    "user_role": "admin",
    "access_granted": true
  }
}

9. Best Practices

  • Strict Separation of Concerns: The client should handle UI, display, and user interaction. The server should handle business logic, database management, and security. They should never overlap responsibilities.
  • Stateless Communication: Every request from the client to the server must contain all the information needed to understand the request. The server should not rely on a "stored memory" of previous requests.

10. Common Mistakes

  • Mixing Client and Server Code: Beginners often try to put database queries (server-side) directly into frontend JavaScript (client-side), which is a massive security risk and breaks the client-server model.
  • Assuming the network is fast: Communication takes time. Clients must be built to handle delays, showing loading spinners while waiting for the server's response.

11. Mini Exercises

  1. 1. Identify the Client and Server in the following scenario: "A smart TV app requesting the latest movies from Netflix."
*(Answer: Smart TV app = Client, Netflix backend = Server)*
  1. 2. Open your browser's Developer Tools (F12), go to the "Network" tab, and refresh any website. Watch the list of Client requests and Server responses flow in.

12. Coding Challenges

Challenge 1: Write a simple PHP script named server.php that checks if a ?type=time query parameter is passed in the URL. If it is, return the current server time in JSON format.

13. MCQs with Answers

Question 1

In the Client-Server model, which entity is responsible for storing data in a database?

Q2. Are all APIs considered Web Services? A) Yes, API and Web Service are exact synonyms. B) No, an API can exist offline (like OS APIs), but Web Services require a network. *Answer: B*
Question 3

What does "Separation of Concerns" mean in this context?

14. Interview Questions

  • Q: Explain the Client-Server architecture and how an API fits into it.
  • Q: Differentiate between a Web Service and an API. Can you give an example of an API that is not a web service?
  • Q: Why is the separation of concerns important between frontend (client) and backend (server)?

15. FAQs

Q: Can a single computer be both the Client and the Server? A: Yes! When you are developing locally on your machine (using tools like XAMPP or Node.js), your browser acts as the client and your local server software acts as the server.

Q: Does communication have to go over the internet? A: No. It can occur over a local area network (LAN) inside an office building, or even internally within the same machine (localhost).

16. Summary

In Chapter 2, we learned that API communication is based on the Client-Server model. The client requests data or actions, and the server processes the request and responds. We also clarified that while all web services are APIs, not all APIs are web services, as web services strictly require network communication. Finally, we emphasized the importance of separating UI logic from backend business logic.

17. Next Chapter Recommendation

Now that you understand how clients and servers interact, we need to study the actual language they speak to each other. Proceed to Chapter 3: HTTP Basics for REST APIs, where we will break down the HTTP protocol, request lifecycles, and headers.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·