HomeBytesheetsRoadmapsUI TemplatesUI ToolsStack Discussion
3 Topics
Chapter•100 Minutes

Web and API Foundations

Build the mental model required to understand APIs before learning REST itself.

What Is an API?

Concept:APIDifficulty:Beginner

An API (Application Programming Interface) is a contract through which one software component can interact with another software component.

An API does not necessarily mean HTTP. APIs can exist between functions, libraries, operating-system components, databases, processes, and distributed services. A REST API is specifically an API exposed using HTTP concepts and REST architectural constraints.

For example, a frontend may request GET /users/42. The backend interprets that request, retrieves user 42, serializes the result, and returns an HTTP response.

The important idea is contract over implementation. The client should not need to know whether the server uses MongoDB, PostgreSQL, Redis, Java, Node.js, Go, or another technology.

Basic API Interaction
Key Takeaways
  • An API is a communication contract.
  • HTTP APIs are only one type of API.
  • Clients should depend on the API contract rather than server implementation.
Knowledge Check

What is the primary purpose of an API?

Client–Server Architecture

REST APIs commonly follow a client-server architecture. The client initiates requests, while the server processes requests and returns representations of resources.

The client might be a browser, mobile application, CLI, another backend service, or an IoT device. The server owns application logic and data.

Separating client and server allows them to evolve independently. A React application can consume an API implemented by Node.js, Java, Go, Python, or another technology without knowing the implementation details.

Responsibilities
Client: Requests resources and operations
The client decides what it needs and sends HTTP requests.
Server: Processes requests
The server validates input, executes business logic, and creates responses.
Database: Persists application state
The database is an implementation detail behind the API.
Client–Server Separation
Key Takeaways
  • Clients and servers can evolve independently.
  • The API forms a boundary between client and server.
  • Database details should normally remain behind the API.

The Complete Request–Response Lifecycle

Request–Response Lifecycle

A request travels through several layers before the application handler executes.

A browser or client first resolves the hostname using DNS. It establishes a TCP connection and, for HTTPS, performs a TLS handshake. The client sends an HTTP request through potentially several network components such as a CDN, load balancer, reverse proxy, or API gateway. The application authenticates and validates the request, executes business logic, accesses data, serializes a response, and sends it back.

Understanding this lifecycle is essential for debugging latency, authentication problems, proxy failures, CORS issues, and production outages.

HTTP Request Lifecycle
Key Takeaways
  • An API request can pass through many layers.
  • Latency is not necessarily caused by application code.
  • DNS, TLS, proxies, gateways, databases, and serialization can all affect performance.