121. What is a web API?
Define a web API as a documented interface through which software exchanges requests, responses, or events over web protocols. Explain endpoints, methods, headers, bodies, status codes, schemas, errors, authentication, versioning, and compatibility. Distinguish a remote HTTP API from browser-provided Web APIs such as the DOM and Fetch.
I would define a web API as a documented interface that lets software exchange requests, responses, or events over web protocols. In this design, a JavaScript client sends an HTTP request to a remote Web API. The example uses GET /users/123, and the Fetch example calls /v1/users/${id}. Requests can include headers such as Accept and Authorization. The API returns a status code, headers, and usually JSON data. The client checks the HTTP result before using the response. Authentication identifies the caller, while trusted server-side logic controls access. Versioning such as /v1 helps preserve compatibility. This remote API is different from browser Web APIs such as DOM, Fetch, and LocalStorage.
A web API is a documented way for software systems to communicate. A JavaScript app sends a request to a remote API and receives a response. The API contract explains the endpoint, HTTP method, headers, body, data shape, status codes, and errors. In this diagram, the client calls a Web API server. The API can interact with a database or external service. It then sends the response back to the client. The browser checks that response before using its JSON data. The same contract also explains authentication, versioning, and compatibility.
- What request and response shape should the client expect?
- Which authentication mechanism should the browser use?
- Which status codes and error shapes are part of the contract?
- How should API versions remain compatible with older clients?
An endpoint is a URL for a resource or action. The diagram shows examples such as /users and /orders. Its main request example is GET /users/123. The JavaScript Fetch example uses the versioned path /v1/users/${id}.
The HTTP method tells the API which action is requested. The diagram lists GET, POST, PUT, and DELETE. GET reads data. POST creates data. PUT updates data. DELETE removes data.
Headers carry extra information about a request or response. The Fetch example sends Accept: application/json. It also sends Authorization: Bearer YOUR_TOKEN. The Authorization header carries a bearer credential used for authentication.
A body carries data sent in a request or returned in a response. The diagram says JSON is commonly used for this data. The shown GET Fetch request has no request body. Its successful response contains JSON with id, name, and email.
A schema defines the expected structure and types of request and response data. It gives the client and API a shared data contract.
The JavaScript client starts the HTTP request. The request arrow moves from the client to the Web API server.
The remote API is a separate network boundary. It can interact with its database or external services. The diagram shows this as a separate bidirectional service and data interaction.
That backend interaction is not the browser response. After the API finishes the operation, the response travels from the Web API back to the client.
The shown successful response contains 200 OK, headers, and a JSON body. The Fetch example checks res.ok before parsing the expected success data with res.json().
Fetch normally resolves when an HTTP response arrives, including many HTTP error responses. Therefore, the client must check the HTTP result. A network failure is different because Fetch can reject before a usable HTTP response is received.
A status code tells the client how the HTTP request finished. The diagram shows 200 OK as the normal success result. It also shows 404 Not Found as another possible result.
The error section includes 400, 401, and 500. A 400 response means the request was not accepted as valid. A 401 response means authentication is required or was not accepted. A 500 response means the remote API encountered a server-side failure.
The diagram says errors should use a clear format with helpful messages. The browser should not treat every response as successful data. It should check the HTTP result before parsing the body expected for success.
Authentication and authorization are different ideas. Authentication establishes who the caller is. Authorization decides what that caller may access. The browser may send a bearer token, but trusted server-side logic must enforce access decisions.
Versioning allows an API contract to evolve over time. The diagram uses /v1/users as its example.
A client written for version 1 should continue receiving the version 1 behavior it expects. This is backward compatibility. It helps older clients continue working when newer API behavior is introduced.
The main trade-off is maintenance. Keeping an older contract available reduces client breakage. However, supporting more versions can increase development, testing, and documentation work.
The Web API in the main flow is a remote HTTP API. The JavaScript application reaches it across the network using HTTP or HTTPS.
Browser Web APIs are different. They are capabilities provided by the browser environment. The diagram gives DOM, Fetch, and LocalStorage as examples.
The DOM API lets JavaScript read or change page content. Fetch provides the browser interface for making network requests. LocalStorage stores data in the browser.
Fetch is therefore a browser Web API used to call the remote Web API. The remote service and the browser capability are not the same thing.
The main frontend concerns are request count, response size, parsing work, error handling, security, and API maintenance. Larger JSON responses use more network data and browser processing. More requests can increase waiting time. Clear schemas make integration safer because both sides know the expected data shape. Error handling adds frontend code, but it stops failed responses from being treated as valid data. Authentication also creates an important security boundary. The browser can send a bearer token, but trusted server-side logic must enforce authorization. Versioning such as /v1 improves backward compatibility, but supporting older versions adds maintenance and testing work. Keeping remote HTTP APIs separate from browser Web APIs also makes responsibilities easier to understand.
Interviewers ask this question to check whether a candidate understands the boundary between a frontend application and a remote API. They want correct reasoning about endpoints, HTTP methods, headers, bodies, status codes, schemas, errors, and authentication. They also look for sound judgment about versioning and backward compatibility. For frontend developers, an important signal is knowing that Fetch and the DOM are browser Web APIs, while the remote HTTP API is a separate network service.







