Welcome to our comprehensive WordPress REST API tutorial. In modern web development, WordPress is no longer confined to traditional template rendering. By exposing a structured, machine-readable interface, it can function as a fully decoupled, headless CMS powering dynamic frontends built with React, Vue, mobile apps, or custom integrations. Understanding how this API works opens up a world of possibilities for developers looking to push the boundaries of modern digital experiences.
In this guide, we will break down the essential concepts of the WordPress REST API, including routes, endpoints, authentication strategies, and raw HTTP requests. Whether you want to fetch your latest articles on an external site or build an interactive custom portal, this tutorial will give you the practical coding examples and core knowledge you need to get started. Additionally, if you prefer managing your site from the command line, you can read our comprehensive WP-CLI beginner’s guide.
The WordPress REST API is a developer-facing tool built directly into the WordPress core as of version 4.7. It allows external applications to communicate securely with your WordPress site by exchanging JSON (JavaScript Object Notation) data. REST stands for Representational State Transfer, which is an architectural style designed for network-based applications to request and manipulate resources easily.
Before the REST API, developers had to rely on complex custom queries, XML-RPC, or heavy AJAX hooks to bridge the gap between WordPress and external frameworks. However, with the native REST API, WordPress speaks a standardized language that almost any programming language—from JavaScript and Python to Swift and Go—can easily interpret.
To navigate and work with the API efficiently, you must understand its three foundational pillars: routes, endpoints, and the API schema.
While these terms are often used interchangeably, they represent distinct concepts in API development:
/wp-json/wp/v2/posts is a route representing the posts resource.GET request to /wp-json/wp/v2/posts, that is one endpoint (retrieve posts). When you perform a POST request to the same route, that is a different endpoint (create a new post).Here is a reference table for standard WordPress REST API routes and methods:
| HTTP Method | API Route Prefix | Function Description | Authentication Required? |
|---|---|---|---|
| GET | /wp-json/wp/v2/posts | Retrieve a list of published posts | No (Public) |
| POST | /wp-json/wp/v2/posts | Create a new post or save a draft | Yes (Authenticated) |
| GET | /wp-json/wp/v2/posts/<id> | Fetch details of a single post by ID | No (Public) |
| DELETE | /wp-json/wp/v2/posts/<id> | Move a post to the trash or permanently delete it | Yes (Authenticated) |
| GET | /wp-json/wp/v2/users | Retrieve a list of public site authors | No (Public details only) |
The schema acts as the blueprints for your data. It defines the structure of the JSON objects returned by each endpoint. It informs external systems of the properties they should expect (such as id, title, content, and date) and the correct data types of those properties (e.g., string, integer, or array). This predictable structure ensures flawless compatibility with external applications.
By default, public content like published posts and public user profiles can be requested anonymously. However, destructive or private actions (such as creating, modifying, or deleting posts) require you to prove your identity. There are three primary authentication systems available:
Cookie-based authentication is the built-in method used by WordPress inside the admin area. It relies on standard browser cookies to authorize requests. Therefore, it is ideal for custom plugins or theme scripts that run directly on your own WordPress dashboard. To secure these requests against cross-site request forgery (CSRF), you must pass a WordPress “nonce” (number used once) in the HTTP header of your fetch request: X-WP-Nonce.
Introduced natively in WordPress 5.6, Application Passwords are the most straightforward solution for external scripting, system integrations, and testing tools. You can generate unique, long-string passwords for specific user accounts via the WordPress Admin area (Users > Profile). Because these keys are independent of your main login credentials, they can be easily revoked at any time without compromising account security.
JWT authentication is the industry standard for decoupled frontends, such as standalone React or mobile applications. When using JWT, the user logs in once with their standard credentials, and the server returns a signed, temporary token. This token is then saved on the client-side and sent along with the authorization header for subsequent requests. This method is highly secure and stateless, though it requires a helper plugin to implement in WordPress.
The core philosophy of the REST API revolves around sending structural HTTP requests and expecting clean, semantic JSON responses. Let’s look at how HTTP methods map directly to actions:
Every response returned by the server includes an HTTP status code. Understanding these codes is critical for debugging:
Common API Status Codes:
200 OK: Request succeeded.
201 Created: Resource successfully created.
400 Bad Request: The server could not understand the request due to malformed syntax.
401 Unauthorized: Missing or invalid credentials.
403 Forbidden: Correct credentials, but the user does not have permission for the action.
404 Not Found: The requested endpoint or resource does not exist.
Now, let’s look at real-world applications of these concepts. In this part of our WordPress REST API tutorial, we will walk through retrieving public blog posts and creating a new post remotely.
Below is a clean, modern JavaScript snippet using the native fetch() API to query the latest five posts from a WordPress website and append them dynamically to a list elements block.
const apiEndpoint = 'https://your-domain.com/wp-json/wp/v2/posts?per_page=5';
fetch(apiEndpoint)
.then(response => {
if (!response.ok) {
throw new Error('Could not fetch WordPress posts');
}
return response.json();
})
.then(posts => {
const postContainer = document.getElementById('api-posts');
posts.forEach(post => {
const listItem = document.createElement('li');
listItem.innerHTML = '<h4><a href=\'' + post.link + '\' target=\'_blank\'>' + post.title.rendered + '</a></h4><p>' + post.excerpt.rendered + '</p>';
postContainer.appendChild(listItem);
});
})
.catch(error => {
console.error('API Error:', error);
});If you are writing a backend automation script in Python, PHP, or Bash, you can create new articles programmatically. Here is a sample terminal command using curl to submit a raw POST request using Application Passwords authentication:
curl -X POST https://your-domain.com/wp-json/wp/v2/posts \
-u "admin_username:your_application_password_here" \
-H "Content-Type: application/json" \
-d '{
"title": "Published via the REST API",
"content": "This post was created successfully during our WordPress REST API tutorial.",
"status": "publish"
}'While the API is exceptionally powerful, letting any user or external system make programmatic requests to your website introduces security and server performance concerns. Ensure you follow these best practices:
By following this WordPress REST API tutorial, you now have the foundational knowledge and developer patterns required to connect your platform to other advanced digital ecosystems. Start experimenting with fetch requests to build your next-generation application today!