What is REST API?
REST API is a way for websites and web applications to interact with a server. It is also called RESTful.
The term consists of two abbreviations, which are deciphered as follows. An API (Application Programming Interface) is a code that allows two applications to communicate with each other, for example, a client application communicates to its server. REST (Representational State Transfer) is a way to create an API using the HTTP protocol.
A REST API (also known as RESTful API) is an application programming interface (API or web API) that conforms to the constraints of REST architectural style and allows for interaction with RESTful web services.
REST is a set of architectural constraints, not a protocol or a standard. API developers can implement REST in a variety of ways.
So, in REST API, instead of web page links, we have endpoints that return parseable data, so it can be used in any other application written in any other language. It means that we can implement REST API in our application and serve data as JSON to all client applications: a website written in React, iOS, Android applications, desktop applications, or any other web applications.
Is any API REST API?
No, API is a generic term that means “Application Programming Interface.” An API represents a set of rules and functions that allow two different applications to communicate with each other. Interfaces like these facilitate application integration, enabling developers to create powerful digital solutions. The API acts as an intermediary between applications by allowing them to send requests and responses and transfer data between each other. For example, registration in the application through the user’s existing Twitter account occurs through the Twitter API, which the developers have integrated into the application. REST API is a specific API called REST that describes the protocol for interacting with a web service. Services in the REST API communicate using the HTTP protocol.
The Architectural constraints for REST are Client-server architecture, statelessness, cacheability, layered system, and uniform interface.
A web API that obeys the above REST constraints is informally described as RESTful or a REST API.
WordPress REST API
WordPress REST API is a common data and programming interface that allows you to read and write information into WordPress through different applications.
Developers are able to perform almost all data manipulation and data reading functions that are possible in PHP using the new API. What does this mean? With REST API, one can access the data in the WordPress database through API, without the need for templates or webpages containing said data. For example, one gets the list of posts by an author in textual format, without the webpage containing a header, footer, and other style elements.
With the REST API, one can also manipulate the data in the WordPress database through the API programmatically, without the need for manual input of the data through user interfaces.
There are already many sites that do this. Some of them use custom-built APIs, while others use the new WordPress REST API.
There are also many other applications: To manage WordPress content, one doesn’t have to go through the WordPress admin area. With the help of the REST API, you can create, edit and delete content in absolutely any way.
Using the API, you can easily create custom administration panels, including mobile apps. This allows developers to design a convenient control process for different applications.
What are endpoints?
An endpoint is a call to the route by a separate HTTP method. Endpoints perform a specific task, accept parameters, and return data to the client. Interaction with the WordPress REST API is carried out through endpoint classes/access points/endpoints located in the ”/wp-includes/rest-api/endpoints” directory. There are three important endpoint components: routes, schema, and controllers.
For example, with the URL `http://example.com/wp-json/wp/v2/posts/123`:
The “route” is wp/v2/posts/123
This route has 3 endpoints:
GET – triggers a get_item method, returning the post data to the client.
PUT – triggers a update_item method, taking the data to update, and returning the updated post data.
DELETE – triggers a delete_item method, returning the now-deleted post data to the client.
Extending the API
Extending the API stems from adding your own endpoints and modifying the default responses e.g. when adding a custom post type named products, add corresponding API routes for them. The WordPress front-end provides a default set of URL mappings, hence why the WordPress REST API, to the naked eye, is a set of default routes.
But It’s actually a tool for creating custom endpoints and routes. The tools used to create the default URL mappings are available for you to create your very own mappings and queries (e.g. the Rewrites API).
Each route can have any number of endpoints, and you can define the HTTP methods allowed for each endpoint, a callback function for responding to the request, and a permissions callback for creating custom permissions. Additionally, you can define allowed fields in the request and for each field specify a default value, a sanitization callback, a validation callback, and whether or not the field is even required.