{"id":9412,"library":"woocommerce","title":"WooCommerce Python REST API Client","description":"The `woocommerce` library is an official Python wrapper for the WooCommerce REST API, enabling developers to easily interact with WooCommerce stores. It is currently at version 3.0.0, released in March 2021, and its development cadence is generally infrequent, tied to significant updates or changes in the core WooCommerce REST API.","status":"active","version":"3.0.0","language":"en","source_language":"en","source_url":"https://github.com/woocommerce/wc-api-python","tags":["woocommerce","e-commerce","api","rest","store","wordpress"],"install":[{"cmd":"pip install woocommerce","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Used for making HTTP requests to the WooCommerce API.","package":"requests","optional":false}],"imports":[{"note":"The primary class for interacting with the WooCommerce REST API.","symbol":"API","correct":"from woocommerce import API"}],"quickstart":{"code":"import os\nfrom woocommerce import API\n\n# --- Instructions to get API keys --- #\n# 1. Log into your WordPress admin for the WooCommerce store.\n# 2. Go to WooCommerce > Settings > Advanced > REST API.\n#    (Prior to WooCommerce 3.4, this was WooCommerce > Settings > API > Keys/Apps).\n# 3. Click 'Add key'.\n# 4. Enter a description, choose a user, set permissions (e.g., 'Read/Write'), and click 'Generate API key'.\n# 5. Copy the generated Consumer Key (starts with 'ck_') and Consumer Secret (starts with 'cs_').\n# 6. Set these as environment variables or replace the placeholders below.\n\n# Replace with your store URL, consumer key, and consumer secret\nSTORE_URL = os.environ.get('WOOCOMMERCE_STORE_URL', 'https://your-store.com')\nCONSUMER_KEY = os.environ.get('WOOCOMMERCE_CONSUMER_KEY', 'ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')\nCONSUMER_SECRET = os.environ.get('WOOCOMMERCE_CONSUMER_SECRET', 'cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')\n\nwcapi = API(\n    url=STORE_URL,\n    consumer_key=CONSUMER_KEY,\n    consumer_secret=CONSUMER_SECRET,\n    version='wc/v3', # Default and recommended API version\n    verify_ssl=True # Set to False for self-signed certificates (not recommended for production)\n)\n\ntry:\n    # Example: Retrieve a list of products\n    print(f\"Attempting to fetch products from {STORE_URL}...\")\n    products = wcapi.get(\"products\").json()\n    if products:\n        print(f\"Successfully fetched {len(products)} products.\")\n        print(\"First product title:\", products[0].get('name'))\n    else:\n        print(\"No products found or empty response.\")\n\n    # Example: Retrieve a specific product (replace 123 with an actual product ID)\n    # product_id = 123 # Replace with a valid product ID\n    # single_product = wcapi.get(f\"products/{product_id}\").json()\n    # if single_product:\n    #     print(f\"\\nFetched product ID {product_id}: {single_product.get('name')}\")\n\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\n    print(\"Please check your URL, API keys, permissions, and network connectivity.\")\n","lang":"python","description":"This quickstart demonstrates how to initialize the `woocommerce.API` client and fetch products from your store. Remember to replace the placeholder URL and API keys with your actual store details, preferably by loading them from environment variables for security."},"warnings":[{"fix":"Upgrade your Python environment to 3.6+ before installing or upgrading `woocommerce` to 3.0.0.","message":"Version 3.0.0 dropped support for legacy Python versions. It now requires Python 3.6 or newer. If upgrading from a pre-3.0.0 library version, ensure your Python environment meets this requirement.","severity":"breaking","affected_versions":"<3.0.0"},{"fix":"Use HTTPS for your WooCommerce store. If not possible, initialize `API(..., query_string_auth=True)`.","message":"When using Basic Authentication over plain HTTP (not HTTPS), the `Authorization` header may be stripped by some servers. To work around this, set `query_string_auth=True` in the `API` constructor to force authentication credentials into the query string. This is not recommended for production environments due to security implications.","severity":"gotcha","affected_versions":"All"},{"fix":"For `version='wc/v3'`, omit the `wp_api` parameter or ensure `wp_api` is not explicitly set to `True` unless you are specifically targeting a very old WooCommerce API.","message":"The `wp_api` parameter in the `API` constructor, previously used to enable requests to the WP REST API, is now considered deprecated or its usage inverted. For modern WooCommerce APIs (version 'wc/v3'), you typically don't need to specify `wp_api=True`. Setting `wp_api=False` might be needed for genuinely legacy WooCommerce API versions.","severity":"deprecated","affected_versions":"<3.0.0 migrating to 3.0.0"},{"fix":"Set `verify_ssl=False` during development/testing with self-signed certificates. For production, use valid SSL certificates and keep `verify_ssl=True` (the default).","message":"When testing with self-signed SSL certificates or local development setups, `verify_ssl=True` will cause SSL certificate validation errors. Set `verify_ssl=False` in the `API` constructor to bypass this validation, but be aware this compromises security and should not be used in production.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Verify that your `url` points to the correct WooCommerce store base URL (e.g., `https://your-store.com`), and that your consumer key and secret are correct and have the necessary permissions. Also, ensure your endpoint is a valid REST API path (e.g., 'products' not a regular product page URL). Check `response.text` for the actual HTML content to debug. If using HTTP, try `query_string_auth=True`.","cause":"The API returned HTML content (e.g., a login page or an error page) instead of valid JSON. This often indicates an incorrect API endpoint URL, a redirect, or authentication failure where the server responds with an HTML error page.","error":"JSONDecodeError: Expecting value: line 1 column 1 (char 0)"},{"fix":"Double-check your `consumer_key` and `consumer_secret`. Ensure the keys generated in WooCommerce admin have 'Read' or 'Read/Write' permissions as required. If using HTTP, try `query_string_auth=True`. Make sure the user associated with the API key still exists.","cause":"The API request failed due to missing or invalid authentication credentials. This could be incorrect consumer key/secret, insufficient permissions for the keys, or the `Authorization` header being stripped by your server (especially with HTTP).","error":"woocommerce.exceptions.WooCommerceHTTPError: Error: 401 Unauthorized (Consumer key is missing)"},{"fix":"Consult the WooCommerce REST API documentation for the specific endpoint you are using (e.g., `/customers`, `/products`) to identify all required fields for creation/update and ensure they are present in your `data` payload. For customers, a password is often a critical missing field.","cause":"This error occurs when attempting to create or update a resource (e.g., customer, product) via a POST/PUT request, but the provided `data` dictionary is missing one or more required fields or is empty.","error":"{'code': 'empty_data', 'message': 'Not enough data to create this user.', 'data': {'status': 400}}"},{"fix":"Run `pip install woocommerce` in your terminal. If using a virtual environment, ensure it's activated before installation and script execution.","cause":"The `woocommerce` library has not been installed in your Python environment or the environment where your script is running is not the one where the library was installed.","error":"ModuleNotFoundError: No module named 'woocommerce'"}]}