Routes vs Endpoints
January 24th 2021 76

Routes vs Endpoints
Endpoints
are functions available through the API. This can be things like retrieving the posts, updating a post, or deleting a comment. Endpoints perform a specific function, taking some number of parameters and returning data
to the client.
A route
is the “name” you use to access endpoints, used in the URL. A route can have multiple endpoints associated with it, and which is used depends on the HTTP verb.
For example, with the URL https://www.teachmebro.com/post/javascript-asynchronous-programming-and-callbacks:
- The “route” is post/javascript-asynchronous-programming-and-callbacks – The route doesn’t include domain name because domain name is the base path for the API itself.
Routes
Routes
in the REST API are represented by URIs
. The route itself is what is tacked onto the end of https://ourawesomesite.com/wp-json
. The index route for the API is '/'
which is why https://ourawesomesite.com/wp-json/
returns all of the available information for the API. All routes should be built onto this route, the wp-json
portion can be changed, but in general, it is advised to keep it the same.
We want to make sure that our routes are unique
. For instance we could have a route for books like this: /books
. Our books route would now live at https://ourawesomesite.com/wp-json/books
. However, this is not a good practice as we would end up polluting potential routes for the API. What if another plugin we wanted to register a books route as well? We would be in big trouble in that case, as the two routes would conflict with each other and only one could be used.