A Python wrapper for the Wordpress REST API that also works on the WooCommerce REST API v1-3 and WooCommerce WP-API v1. Forked from the Wordpress API written by Claudio Sanches @ WooThemes and modified to work with Wordpress: https://github.com/woocommerce/wc-api-python
I created this fork because I prefer the way that the wc-api-python client interfaces with the Wordpress API compared to the existing python client, https://pypi.python.org/pypi/wordpress_json which does not support OAuth authentication, only Basic Authentication (very unsecure)
- [x] Create initial fork
- [ ] Implement 3-legged OAuth on Wordpress client
Your site should have the following plugins installed on your wordpress site:
- WP REST API (recommended version: 2.0+)
- WP REST API - OAuth 1.0a Server (https://github.com/WP-API/OAuth1)
- WP REST API - Meta Endpoints (optional)
Download this repo and use setuptools to install the package
pip install setuptools
git clone https://github.com/derwentx/wp-api-python
python setup.py install
Generate API credentials (Consumer Key & Consumer Secret) following these instructions: http://v2.wp-api.org/guide/authentication/
Check out the Wordpress API endpoints and data that can be manipulated in http://v2.wp-api.org/reference/.
Setup for the old Wordpress API:
from wordpress import API
wpapi = API(
url="http://example.com",
consumer_key="XXXXXXXXXXXX",
consumer_secret="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
api="wp-json",
version=None
)
Setup for the new WP REST API v2:
#...
wpapi = API(
url="http://example.com",
consumer_key="XXXXXXXXXXXX",
consumer_secret="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
api="wp-json",
version="wp/v2"
)
Setup for the old WooCommerce API v3:
#...
wcapi = API(
url="http://example.com",
consumer_key="ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
consumer_secret="cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
api="wc-api",
version="v3"
)
Setup for the new WP REST API integration (WooCommerce 2.6 or later):
#...
wcapi = API(
url="http://example.com",
consumer_key="ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
consumer_secret="cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
api="wp-json",
version="wc/v1"
)
Params | Type | Description |
---|---|---|
endpoint |
string |
Wordpress API endpoint, example: posts or user/12 |
data |
dictionary |
Data that will be converted to JSON |
.get(endpoint)
.post(endpoint, data)
.put(endpoint, data)
.delete(endpoint)
.options(endpoint)
All methods will return Response object.
Example of returned data:
>>> r = wpapi.get("posts")
>>> r.status_code
200
>>> r.headers['content-type']
'application/json; charset=UTF-8'
>>> r.encoding
'UTF-8'
>>> r.text
u'{"posts":[{"title":"Flying Ninja","id":70,...' // Json text
>>> r.json()
{u'posts': [{u'sold_individually': False,... // Dictionary data
- Initial fork