SDKs overview
The WordPress REST API is officially integrated into WordPress Core, enabling external applications and services to interact with WordPress sites using standard HTTP requests to retrieve and manage content (WordPress REST API handbook). While WordPress itself is primarily developed in PHP, its REST API is designed to be language-agnostic. This means that developers can build applications that communicate with a WordPress site using any programming language capable of making HTTP requests. The official documentation for the WordPress REST API focuses on the API endpoints and their usage rather than providing a suite of official, language-specific SDKs in the traditional sense.
Instead of a large collection of first-party SDKs, the WordPress ecosystem relies on direct HTTP client implementations or community-contributed libraries to interact with the API. This approach provides flexibility, allowing developers to choose tools and libraries that best fit their project's technology stack. For client-side applications, JavaScript libraries are commonly used, often leveraging built-in browser APIs like fetch or popular HTTP clients. Server-side applications can utilize robust HTTP client libraries available in languages such as Python, Ruby, Java, Go, and PHP itself.
The design philosophy encourages developers to understand the REST API's structure and endpoints directly. This knowledge enables the creation of custom integrations or the use of generic REST client libraries without being tied to a specific WordPress-branded SDK. The emphasis is on interoperability and adherence to REST principles, making the API accessible from a wide range of development environments (WordPress REST API key concepts).
Official SDKs by language
Due to the REST API's language-agnostic nature and WordPress's core development focus, there are no extensive, formally maintained SDKs provided directly by the WordPress project for a wide array of programming languages. The primary interaction is via direct HTTP requests. However, for internal WordPress development and JavaScript-based front-ends, certain components serve a similar function to an SDK.
JavaScript (Client-side and Node.js)
For client-side interactions, especially within the context of the Gutenberg editor and Block Editor projects, WordPress provides JavaScript utilities that abstract some REST API interactions. These are not standalone SDKs but rather modules within the WordPress ecosystem. When developing custom blocks or plugins for the Block Editor, developers often use packages like @wordpress/api-fetch (WordPress api-fetch package documentation) to make authenticated and unauthenticated requests to the WordPress REST API.
PHP (Server-side)
Within the WordPress core and plugin development, direct PHP functions and classes are used to interact with the REST API, particularly for registering endpoints and processing requests. For making outgoing requests from a WordPress site to its own or another WordPress site's REST API, the WordPress HTTP API is utilized. This API provides an abstraction layer for making HTTP requests in PHP (WordPress HTTP API handbook), supporting various transport methods.
The table below outlines common approaches rather than a list of official, distinct SDK packages:
| Language | Package/Approach | Installation/Usage | Maturity/Purpose |
|---|---|---|---|
| JavaScript | @wordpress/api-fetch |
npm install @wordpress/api-fetch |
Mature, primarily for Block Editor development and client-side interactions within WordPress context. |
| PHP | WordPress HTTP API | Built-in to WordPress Core functions (e.g., wp_remote_get()) |
Mature, for server-side HTTP requests within WordPress plugins/themes. |
| Various | Generic HTTP Clients | Project-specific (e.g., Python requests, Ruby HTTParty) |
Highly mature, general-purpose for any language to interact with REST APIs. |
Installation
Installation depends on the chosen language and approach. For generic HTTP client libraries, developers typically use their language's package manager.
JavaScript (@wordpress/api-fetch)
If you are developing a WordPress block or a custom script within the WordPress build process, you can install @wordpress/api-fetch using npm:
npm install @wordpress/api-fetch --save
Then, import it into your JavaScript file:
import apiFetch from '@wordpress/api-fetch';
PHP (WordPress HTTP API)
The WordPress HTTP API is fundamental to WordPress core. No separate installation is required when developing within a WordPress environment. Functions like wp_remote_get(), wp_remote_post(), etc., are available globally within the WordPress execution context (WordPress HTTP API request methods).
Other Languages (Generic HTTP Clients)
For languages like Python, Ruby, Java, Go, or Node.js (outside of WordPress-specific packages), you would install a general-purpose HTTP client. For example:
- Python: The
requestslibrary is a common choice. Install via pip:
pip install requests
axios or node-fetch are popular. Install via npm:npm install axios
HTTParty or Faraday. Install via Bundler:gem install httparty
These examples illustrate the general process of installing client libraries in different programming environments. The specific choice of library will depend on project requirements and developer preference, allowing integration with the WordPress REST API by making standard HTTP calls (HTTP/1.1 Message Syntax and Routing specification).
Quickstart example
This example demonstrates how to fetch posts from a WordPress site's REST API using JavaScript (client-side fetch API) and Python (requests library). These examples show direct API interaction, which is the most common approach.
JavaScript (Browser/Client-side)
This snippet fetches the 10 most recent posts from a WordPress site and logs their titles. Replace your-wordpress-site.com with your actual domain.
const siteUrl = 'https://your-wordpress-site.com'; // Replace with your WordPress site URL
fetch(`${siteUrl}/wp-json/wp/v2/posts?_fields=title,link&per_page=10`)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(posts => {
console.log('Recent Posts:');
posts.forEach(post => {
console.log(`- ${post.title.rendered}: ${post.link}`);
});
})
.catch(error => console.error('Error fetching posts:', error));
Python (Server-side)
This Python example uses the requests library to perform a similar action, fetching posts and printing their titles. Remember to install requests first (pip install requests).
import requests
site_url = 'https://your-wordpress-site.com' # Replace with your WordPress site URL
api_url = f'{site_url}/wp-json/wp/v2/posts?_fields=title,link&per_page=10'
try:
response = requests.get(api_url)
response.raise_for_status() # Raise an HTTPError for bad responses (4xx or 5xx)
posts = response.json()
print('Recent Posts:')
for post in posts:
print(f"- {post['title']['rendered']}: {post['link']}")
except requests.exceptions.HTTPError as http_err:
print(f'HTTP error occurred: {http_err}')
except Exception as err:
print(f'Other error occurred: {err}')
Community libraries
Given the flexibility of the WordPress REST API, the community has developed various client libraries and wrappers in different programming languages. These libraries often aim to simplify authentication, error handling, and object mapping, reducing the boilerplate code required for direct HTTP interactions.
While an exhaustive list is difficult to maintain due to the dynamic nature of community projects, here are examples of types of community libraries you might find:
- PHP Clients: Libraries that provide a more object-oriented interface for interacting with the WordPress REST API from other PHP applications, sometimes extending the built-in HTTP API.
- JavaScript/TypeScript Wrappers: Beyond
@wordpress/api-fetch, developers might create custom hooks for React or Vue.js applications to manage state and API calls more efficiently. - Python WordPress Clients: Libraries that abstract API endpoints into Python objects, making it easier to perform CRUD operations on posts, pages, users, and other content types.
- Ruby/Go/Java Clients: Similar wrappers exist in these languages, often found on GitHub or respective package managers, offering a more idiomatic way to interact with the API.
When choosing a community library, consider its maintenance status, community support, and compatibility with the specific version of the WordPress REST API you are targeting. Checking project repositories on platforms like GitHub for recent commits, open issues, and documentation is advisable. Developers can also find discussions and recommendations on the official WordPress.org forums or developer communities (WordPress support forums).
These community-driven efforts demonstrate the adaptability of the WordPress REST API and enable developers to work within their preferred language ecosystems while benefiting from higher-level abstractions that streamline development.