I can run $client = new SquareClient(…)
and $client->payments->create(…)
in API Explorer (sandbox) with no problems
BUT when I try to run exactly the same from my website server, I get unauthorized (401) on the create payment call, with no other info about why. What am I missing??? I’ve tried EVERYTHING!
This is almost always an environment mismatch. API Explorer sends sandbox requests to the sandbox URL automatically, but the PHP SDK defaults to production. A sandbox token against production returns a bare 401.
Point the client at sandbox explicitly:
php
$client = new SquareClient( token: getenv('SQUARE_SANDBOX_ACCESS_TOKEN'), options: ['baseUrl' => Environments::Sandbox->value],);
Also double-check the env var is actually set on your web server (not just locally). An empty token gives the same bare 401.
Thanks Ashley, it was the environment like you suggested. Shame that’s not documented clearly anywhere - I’ve come across multiple code examples, all different!
Martin