Testing Square Sandbox access token and location ID to create a payment using the API Explorer, but receiving an UNAUTHORIZED error

Hey , i am testing my sandbox accesss token and location id to create Payment on API explorer . but i get error
{
“errors”: [
{
“category”: “AUTHENTICATION_ERROR”,
“code”: “UNAUTHORIZED”,
“detail”: “This request could not be authorized.”
}
]
}

import { SquareClient, SquareEnvironment } from “square”;

async function main() {
const client = new SquareClient({
environment: SquareEnvironment.Sandbox,
token: “myAccessToken”,
});
await client.payments.create({
idempotencyKey: “8443bda00c53ef8596d3fa8532f96cbc”,
amountMoney: {
amount: BigInt(“2000”),
currency: “USD”,
},
sourceId: “cnon:card-nonce-ok”,
locationId: “myLocationID”,
autocomplete: true,
});
}
main();

1 Like

:waving_hand: I’m Square Dev AI, here to help answer your question!
Based on the information you’ve provided, the following documentation will help answer your question:

Additional Documentation

Postman
Sandbox Payments
Test in the Sandbox

If this documentation helped you find a solution, please check the box indicating that this reply solves the problem.
Otherwise one of our Developer Advocates will provide additional assistance shortly.

That looks like the correct request body. Have you double checked the configuration? The only way to get an unauthorized error is when something is misconfigured. :slight_smile:

I am repeatedly getting the exact same error. And the interesting thing is that a direct curl call works.

I am having trouble pasting my sample code here (getting a 403 error from the dev forum itself when I try to post). Will keep trying…

See here for my sample apps that show success with curl vs failure with nodejs:

Oops – my issue (which is different from the original poster’s issue) is that my call to new SquareClient(), I was passing accessToken: ... when I should have passed token: ...

Glad to hear that you figured it out and thank you for sharing your findings with the community. :slight_smile: