November 23, 2021

You’ve probably heard of REST API. Maybe you’ve heard that WordPress users benefit from it. But you may also be wondering what exactly a WordPress API is.

Understanding how APIs work isn’t as complicated as it sounds. Keep reading to learn what an API is, what REST API is, and how you can use WordPress REST API in your sites.

What is an API?

An Application Program Interface (API) is a way for two applications to “talk” to each other. One system makes a request, and the other fulfills it. The requests that a system accepts must be included in the API — otherwise the request cannot be fulfilled.

Think about it this way. Maybe you’re really hungry so you go to Waffle House. You order coffee, eggs, and pancakes. The waitress begins to write down your order — and then gets confused when you get to pancakes. Waffle House doesn’t serve pancakes. They only serve waffles.

A system requesting pancakes from an API menu that only serves waffles is going to be left hungry.

What is the REST API?

When we say REST API, we’re talking about a system of constraints designed for the internet. It describes how one website can request information from another website in a way that is scalable and uniform. REST stands for Representational State Transfer, which was defined by Roy Fielding in his doctoral dissertation.

What is the WordPress REST API?

WordPress REST API refers to requests made to a website running on WordPress. If we request a list of posts or pages, we can expect to receive that information from the WordPress REST API. WordPress will never provide any information that is not made public, such as draft posts or (more importantly) user passwords.

This is foundational for the Block Editor, which exchanges data dynamically with WordPress without reloading the page. Each endpoint returns data in a JSON format which is readable by JavaScript — or in this case blocks.

Note that the REST API is only one of the APIs provided by WordPress. There is also, for example, the Database API and the Metadata API, which are used by plugins to interact with WordPress on the server. You can learn more about WordPress APIs in the Codex.

How Does a WordPress REST API Request Work?

There are a few basic parts of the REST API, namely: routes, endpoints, requests, and responses.

Routes

A route is how we reference the URL by which the website communicates. A common route in WordPress is “/wp-admin”. Although it’s not a route of the REST API, it is a route used by the browser to access the WordPress Admin Dashboard. For our purposes, REST API routes begin with “/wp-json” (with JSON being the standard format of the data).

Endpoints

An endpoint is the combination of a route and the incoming request type. A single route can connect to multiple endpoints, as there are multiple request types. Common request types include GET (how we read data on a route) and POST (how we update data on a route).

Requests and Responses

Every communication with the REST API is sandwiched between a request and a response. One system makes a request to which the other system responds. The WordPress REST API includes handy objects for plugins to access the request data (WP_REST_Request) and manage the response data (WP_REST_Response) when extending the WordPress REST API.

How to Install REST API in WordPress

As of WordPress version 4.7, WordPress includes the REST API as a core feature.

If you are running an older version, you can install the json-rest-api feature plugin. This was used to develop the REST API prior to being fully integrated with the WordPress codebase. This plugin is technically closed, so it is recommended to update your version of WordPress instead (or maybe consider managed WordPress hosting and you’ll always be up to date.)

WordPress REST API Examples

Take a look at the GiveWP website. If you go to their blog, you’ll be able to see recent news and posts. This route renders HTML, which is nice to look at, but difficult for a programmer to interact with. Instead, posts are made available to programmers using the “/wp-json/wp/v2/posts” route.

The “/wp-json/wp/v2/posts” route tells us that this is a WordPress resource (“/wp”), that this is version 2 (“/v2/”) of the resource, and specifically that these are posts (“/posts”). If you haven’t already, visit https://givewp.com/wp-json/wp/v2/posts and compare what you see with https://givewp.com/blog/. I’ll give you a hint — it is the same information!

For an example of how the REST API is used within WordPress, consider the latest posts block. In order to get a list of posts in WordPress, the site would traditionally need to make a request to the server and then re-render the entire page.

The latest posts block can request just the posts and then re-render itself. It does this using the REST API. And it can do it multiple times to poll for new posts — without re-rendering the entire page. This is especially useful when you consider full site editing in which blocks are used to edit and display the components of the page.

Each individual component can request the data that it needs (including checking for updates) and then re-render only what has changed.

Now You Know

Now that you get the purpose and structure of a WordPress REST API, poke around to see what you can find on your own. For a complete rundown on using and extending the WordPress REST API, check out the REST API Handbook.

Remember: if someone asks you about APIs, you can’t order pancakes at Waffle House.

While you’re fine-tuning your site, don’t forget that great sites need great hosting. You get all that with managed WordPress by Nexcess. With powerful speeds, scalability options, and outstanding support, you can rely on Nexcess to power your WordPress site.

Come see why better is built in with Nexcess. Check out our WordPress fully managed hosting plans today.

Kyle Johnson
Kyle Johnson

Kyle Johnson is a Senior Developer at GiveWP, a Liquid Web brand. His afternoons are spent with his wife wrangling JSON, a black lab.

We use cookies to understand how you interact with our site, to personalize and streamline your experience, and to tailor advertising. By continuing to use our site, you accept our use of cookies and accept our Privacy Policy.