In particular, the WordPress REST API enables you to connect your WordPress website with external applications. This means you can develop a mobile app using practically any programming language, and use the REST API to fetch data from WordPress. In a way, the REST API offers a way to free yourself from WordPress’ inherent structure, while harnessing it to help you create an application.

In this article, we’ll break down how the WordPress REST API works, and help you get started with it from scratch. Let’s get to work!

How the WordPress REST API Works

In the past, you needed a plugin in order to access the WordPress REST API. However, since version 4.4, this API became a part of core. As such, to use the REST API, you simply need to know how to interact with it, which boils down to using four different types of HTTP methods as part of your requests:

GET: With this method, you can fetch information from the server.

POST: This enables you to send information to the server in question.

PUT: With the put method, you can edit and update existing data.

DELETE: This enables you to delete information.

As an example, consider what happens when you visit your WordPress login page. Your browser sends a GET request to the server, which processes it using its own API. Once the page loads, you enter your credentials and send them through a POST request. If you want to change your password, it involves the PUT method, whereas deleting your account altogether would use DELETE.

We’ll show you examples of how to use these methods with the WordPress REST API in a minute. For now, let’s go over some other concepts you’ll need to understand beforehand.

Getting Familiar With WordPress REST API

To really understand how the WordPress REST API works, there are a few tips and concepts you need to familiarize yourself with. When we get to real examples shortly, you’ll understand how everything works.

REST API Concepts and Terms

When using the WordPress REST API, you’ll see the same terms popping up over and over again, which are:

‘Routes’ and ‘endpoints’: A route is a URL you enter to make a request, whereas an endpoint is the combination of a URL with an HTTP method.

Requests: When you submit an endpoint, you’re making a request to the server.

Schemas: Every response you get follows similar structures, which are governed by built-in schemas.

Controller classes: You can use these to build your own routes and endpoints, but this falls under more advanced WordPress REST API uses.

REST API Endpoints

In most cases, you’ll use routes and endpoints that already exist to submit requests via the WordPress REST API. Knowing what these endpoints are is the first step to mastering the API and using it to develop your own projects.

REST API Authentication

As you’d expect, WordPress won’t let you access certain data unless it can corroborate who you are, whether you’re requesting it via a browser or the REST API. For example, if you want to update or publish a post via commands, you’ll need to learn the basics of authentication.

How to Start Using the WordPress REST API (In 3 Steps)

We’ve gone over a lot of theory so far, so it’s time to move on to a WordPress endpoint tutorial. For this section, we’ll show you how to access the REST API, get back a list of specific data, and add new information using a specific endpoint. Let’s get to work!

Step 1. Access the REST API

You can ‘access’ the WordPress REST API from any application that can submit HTTP endpoints. For example, if you enter the following command within your favourite browser, you’ll get back a list of your WordPress posts in JSON format:

GET yourwebsiteurl.com/wp-json/wp/v2/posts

However, you’ll need to replace the placeholder URL with that of your own website. You’ll also need to use a version of WordPress greater than 4.4 for a REST API request to work (which you already should be doing).

If you want to really experiment with the REST API though, a browser isn’t the best tool to do so. Instead, we recommend you use the command line, which provides a more flexible approach.

Step 2: Fetch a Specific Post Using the REST API

The last command you ran should have returned a list of all your WordPress posts, including their post IDs. To fetch a specific post using its ID, you’d use an endpoint such as this:

GET yourwebsiteurl.com/wp-json/wp/v2/posts/535

For example, this would be ideal for showcasing a specific post translated within a mobile application. However, the WordPress REST API enables you to fetch all kinds of data from WordPress, so its practical applications are incredibly flexible.

However, let’s say you wanted to use the REST API to add metadata to a chosen post instead of merely fetching it. In other words, using the POST method instead of GET.

Step 3: Add Metadata to a Specific Post

Assuming you’ve already authenticated yourself, you can add new data to any of your posts using a similar request to that presented in the last section using POST instead of GET:

For example, if you want to add metadata you could use to create a rich snippet for a recipe, the request may look like this:

POST yourwebsiteurl.com/wp-json/wp/v2/posts/535/meta?cookingtime=25

Depending on how much metadata you want to add, you might want to specify it using JSON objects instead, which offers a much more structured approach. In any case, once you’re familiar with what the most common endpoints are and how to put them to use, a whole world of possibilities opens up.

Begin Building the WordPress Website That You Envision

The WordPress REST API has been around for a few years now. If you’ve never used it before, there’s a lot you need to learn to get to the point where you can use it to help you develop advanced applications.