Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

WooCommerce REST API refuses your key with a 401 error

ErrorHTTP 401 Unauthorized from /wp-json/wc/v3/products

Short answer

A 401 from the WooCommerce REST API means the key or signature was not accepted. Over HTTPS, send the consumer key and secret as Basic Auth. Over plain HTTP you need OAuth 1.0a. If the server drops the Authorization header, send the pair as query parameters. Also confirm the key's user and its Read or Write permission.

  • Applies to WooCommerce 9, WordPress 6.8, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Call the endpoint with curl and the key pair, and read the status and the JSON body. Use HTTPS; Basic Auth is only for HTTPS.

    curl -i -u ck_xxx:cs_xxx https://example.com/wp-json/wc/v3/products
  2. 2Match what you see, then jump to its fix

    1. 1The URL starts with http:// and the response is 401.The site is on plain HTTP but the client sends Basic AuthFix
    2. 2Over HTTPS the answer is 'Consumer key is missing' although you sent the key.The server does not pass the Authorization header to PHPFix
    3. 3The key was made for a user without access to the resource, or with Read access only.The key belongs to a user or permission that cannot do thisFix

What does the error look like?

  • HTTP 401 Unauthorized from /wp-json/wc/v3/products
  • Consumer key is missing
  • Your API keys or signature is wrong

Cause 1 of 3

The site is on plain HTTP but the client sends Basic Auth

1Check

WooCommerce accepts Basic Auth with the consumer key as username and the secret as password over HTTPS. Over HTTP it expects OAuth 1.0a one-legged signatures, so that credentials are not exposed.

curl -sI http://example.com/wp-json/ | head -n 5

2Fix

Move the site to HTTPS and use Basic Auth, or sign requests with OAuth 1.0a if you must stay on HTTP.

curl -u ck_xxx:cs_xxx https://example.com/wp-json/wc/v3/orders

3Verify

The request returns 200 and a JSON list.

curl -s -o /dev/null -w '%{http_code}
' -u ck_xxx:cs_xxx https://example.com/wp-json/wc/v3/orders

Confirmed by woocommerce.github.io

Did this fix it?

Cause 2 of 3

The server does not pass the Authorization header to PHP

1Check

Some servers do not parse the Authorization header. WooCommerce documents a fallback: the consumer key and secret as query string parameters. FastCGI setups may need the header configured.

curl -i "https://example.com/wp-json/wc/v3/orders?consumer_key=ck_xxx&consumer_secret=cs_xxx"

2Fix

Send the key pair in the query string over HTTPS, or fix the server so the header reaches PHP.

curl "https://example.com/wp-json/wc/v3/orders?consumer_key=ck_xxx&consumer_secret=cs_xxx"

3Verify

The same call now returns 200.

curl -s -o /dev/null -w '%{http_code}
' "https://example.com/wp-json/wc/v3/orders?consumer_key=ck_xxx&consumer_secret=cs_xxx"

Confirmed by developer.woocommerce.com

Did this fix it?

Cause 3 of 3

The key belongs to a user or permission that cannot do this

1Check

A key has Read, Write or Read/Write access and acts as the user chosen when it was created. That user must have access to the resource.

echo 'WooCommerce > Settings > Advanced > REST API: check User and Permissions'

2Fix

Create a new key for a user who can access orders, with the permission you need, and copy the secret at once because it is shown only one time.

# Use the new pair in the same request
curl -u ck_new:cs_new https://example.com/wp-json/wc/v3/orders

3Verify

The status code is 200.

curl -s -o /dev/null -w '%{http_code}
' -u ck_new:cs_new https://example.com/wp-json/wc/v3/orders

Confirmed by developer.woocommerce.com

Did this fix it?

How do I stop it coming back?

  • Use HTTPS for the store and for every API call.
  • Create one key per integration, so a leaked key is revoked alone.
  • Keep the key pair in server-side config. A mobile app should call your own backend, not hold the secret.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.