What defines an API and what are its core purposes?
An API (Application Programming Interface) is a defined contract between a client and a provider that specifies exactly how requests can be made to access functionality or data, without exposing the internal implementation of either side.
Read full answerHide full answer
Applies toall versions
Problem It Solves
Before a formal API, systems often had to be directly coupled to share capabilities, such as reading from another system’s database or calling internal functions. This created brittle integrations, made changes unsafe, and blurred boundaries between systems. An API solves this by enforcing a clear boundary: the provider decides what is exposed, and the consumer only needs to follow that contract.
Core Purposes
-
Abstraction: Hides complex internal logic. For example, an e-commerce checkout doesn’t need to know how a payment service tokenizes cards, it only needs to know how to call the payment API.
-
Integration: Allows completely separate systems to work together over a network. A chat app can integrate with a notification service without merging their codebases.
-
Reusability: A single API can be consumed by a web app, a mobile app, and even third parties, preventing the same functionality from being rebuilt multiple times.
-
Controlled access: Provides a place to enforce authentication, authorization, and restrictions on what data or actions a consumer can take.
-
Decoupling for independent evolution: Teams can refactor or scale their internal systems without forcing all consumers to change, as long as the API contract remains compatible.
Trade-offs
-
Latency: Any API call over a network is orders of magnitude slower than an in-process call (same-datacenter round trip ~0.5 ms rough order of magnitude). This cost is unavoidable when crossing system boundaries.
-
Operational complexity: Exposing an API introduces the need for availability, versioning, error handling, rate limiting, and monitoring.
-
Contract rigidity: Breaking the contract impacts every consumer at once. Versioning becomes necessary the moment multiple consumers exist.
-
Exposure of risk: Every exposed endpoint is an attack surface. Without proper access control, the API can leak data or allow unintended actions.
-
Workload-dependent payoff: For a single, tightly owned monolith with one consumer, creating a public API contract can be pure overhead. It’s only worth it when you genuinely need separation, reuse, or integration.
Real-World Example
Take a URL shortener. The redirect service doesn’t need the web frontend to know how it generates short keys or where mappings are stored. The frontend calls the URL shortener API with a long URL and gets back a short link. If the backend switches storage or changes its key generation strategy, the frontend remains unchanged, so long as the request and response shape stays the same.
What Interviewers Look For
-
Contract mindset: Seeing the API as a contract, not just “an HTTP endpoint”.
-
Awareness of boundaries: Knowing when to draw them and that crossing them has a cost.
-
Failure-aware thinking: Recognizing that APIs fail over networks (timeouts, errors, partial failures), unlike local function calls.
-
Common mistake: Treating an API as a way to “just expose everything”, instead of deliberately choosing what to expose with access control and stability in mind.