WooCommerce REST API refuses your key with a 401 error
HTTP 401 Unauthorized from /wp-json/wc/v3/productsShort 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.
Fix it fast
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/products2Match what you see, then jump to its fix
- 1
The URL starts with http:// and the response is 401.The site is on plain HTTP but the client sends Basic AuthFix - 2
Over HTTPS the answer is 'Consumer key is missing' although you sent the key.The server does not pass the Authorization header to PHPFix - 3
The 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
- 1
What does the error look like?
HTTP 401 Unauthorized from /wp-json/wc/v3/productsConsumer key is missingYour 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 52Fix
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/orders3Verify
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/ordersConfirmed by woocommerce.github.io
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
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/orders3Verify
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/ordersConfirmed by developer.woocommerce.com
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix