'What is the difference between an API and routes/endpoints?
I am new to the web world and I just read the difference between a route and an endpoint.
I know the definition of an API. But what is the difference between an API and routes (endpoints)?
It seems to me, when somebody says "build an API" or according to the YouTube tutorials that I have watched, they simply build a route using web frameworks like Express.js or Flask like '/hello', which returns "hello".
If that is an API, are an API and a route interchangeable? Like, if I have, for example, 3 routes: '/hello', '/users', '/users/<userId>'. Can I say I have 3 APIs?
Solution 1:[1]
In short to my mind:
- API is about working with data using JSON or XML (as a rule) for input/output data (CRUD operations without any UI). API should follow some rules and structure. Ex.:
GET /{entity_type}/{entity_id}means that this method will try to get entity with passed type and id - endpoint may be an API (they often are spelled together like "API endpoint") but at the same time it may be just an URL that leads somewhere without explicit manipulation with any data, for instance, trigger/webhook/gateway etc.
- route is a path to some website/page/controller with a meaningful name to interact with the user. Route receives some user input (handles user actions) and represents some results in a convenient way (for example, render markup).
Solution 2:[2]
TL;DR
API, an endpoint and a route are interchangeable but a subtle difference exist.
Long Read
API as in web API world are represented by URI or REST endpoints. Best to understand it from programming analogy. Take Java API specification, there are methods aggregated in classes and packages. You may think a class as API but you actually call its methods.
Similarly, "/users" can be called a "users" API. This is also an endpoint. You need to read its specification to understand its usage. This API can have further related REST endpoints. For example - "/users/{id}" or "/users/admin/". All those have their own specification. Collectively, it becomes part of one API documentation.
API is usually a definition term, Endpoint or route are physical representation. When somebody says "build an API" that means you have to define its specification e.g. protocol, request/response schema, (may be) security credentials and (of course) an endpoint to hit.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Slava |
| Solution 2 |
