In this article we’re going to learn how to consume the WP REST API, we’ll do CRUD (Create, Read, Update and Delete) operations using Postman HTTP request helper.

Although we are using Posts in the following examples, the same concepts and workflow applies to all WP REST API endpoints.

Ok let’s start playing with the API 🙂

List Posts

In order to make the API endpoints accessible we need to setup our permalink structure in Settings / Permalinks / Common Settings.

As you can see in the video, we are sending a GET /wp-json/wp/v2/posts request and receiving a JSON response from the API:

Create a Post

To create a new Post from the API we need to do an authenticated request, in this article we use HTTP Basic Authentication but is important to note that this method is only used for development and testing purposes, in production we should use more secure authentication methods like OAuth1 or JSON Web Token.

In order to add Basic Authentication to our WordPress site we need to install Basic-Auth plugin, in the above video I’m cloning the plugin from the repo, also you can install the plugin downloading the .zip and install it from Plugins / Add New / Upload plugin.

Once the plugin is installed and active we are ready to create a new Post using Basic authentication, let’s see how to do it in Postman.

To create a new post use POST /wp-json/wp/v2/posts with arguments.

  • In Autorization tab, set Type as Basic Auth
  • Then fill Username and Password fields with WordPress User login and password
  • Click Update Request button to populate the Headers of the request
  • Switch to Headers tab and you’ll see the Authorization field added
  • Switch to Body tab and add arguments
  • Finally, click Send button

In the video we are only adding Title argument but we can add more arguments, for example we can add status argument with a value of publish to publish the post automatically when created.

Retrieve a Post

To retrieve a post by ID use GET /wp-json/wp/v2/posts/id.

Update a Post

To update a post use POST /wp-json/wp/v2/posts/id with arguments.

Delete a Post

To delete a Post, use DELETE /wp-json/wp/v2/posts/id.

As you can see, playing with the API is not so complicated, once we have our authentication method in place, doing requests to the API is easy using a HTTP request helper like Postman.

Published by emili