What’s an API?
An API is an interface that allows applications to use, create or modify data from another application.
What’s the WordPress REST API?
The WordPress REST API takes information from the database and serves it in JSON format. This includes, posts, media, pages, and users and others. We’ll cover the Core endpoints further down.
The REST API can have two different structures depending on how permalinks are setup. On sites without pretty permalinks, the route is instead added to the URL as the
rest_route parameter. For a site using the default permalink structure the REST API full URL would then be
Plugins can extend this feature to generate their build their own API for use. A good example of this is the Woocommerce API, which we touch on below.
What is a route?
A route, in the context of the WordPress REST API, is a URI which can be mapped to different HTTP methods. The mapping of an individual HTTP method to a route is known as an “endpoint”.
HTTP Request Methods
Every time we go to a website, we’re making multiple HTTP requests. One to the server and then that sends a response which tells us we need to make more requests to get assets so that the browser renders the page properly. That is an example of a GET request. Whenever a slider changes, and triggers a request to admin-ajax.php, those are POST requests. Below is a table of different request methods availbab
|GET||Read data from an endpoint|
|POST||Send data to an endpoint to create new data|
|PUT||Used for updating already existing data|
|DELETE||Delete data from an endpoint|
|OPTIONS||Used to determine which methods an endpoint accepts|
GET requests will be cached per normal varnish rules.
POST requests will always be uncached.
It is possible for plugins and themes to extend the WordPress REST API with their own routes. Woocommerce uses
/wp-json/wc/v3 as their endpoint. They then have routes for products, orders, customers, coupons and more. Most of these requests will require authentication with a Woocommerce API key which can be generated in their Woocommerce settings page in their dashboard.
The WordPress REST API returns data in JSON format. This can then be consumed by other applications to process the data. When you load the REST API in your browser it will look similar to this:
You can make this look prettier using the JSONViewer Chrome Extension.
All of the information about a post or page can be obtained through the REST API. This is what allows developers to create phone applications, or run Headless WordPress sites, where the front end is a Node or React application powered by the WordPress REST API.