Sandbox OAuth Unauthorized

I am having issues with the sandbox endpoints. I created a dotenv file that stores my tokens. However, when I try to create a customer in a js file it will state “AUTHENTICATION_ERROR, UNAUTHORIZED, This request could not be authorized.” Even though I renew my tokens from time to time and tested them in API explorer where it still works. It just won’t seem to authorize on my local workspace.

import { customersApi, ApiError } from "../utils/squareapi.js"
import { randomUUID } from "crypto";

async function createCustomer() {
    try {
        let createCustomerResponse = await customersApi.createCustomer({
            givenName: "Mary",
            familyName: "Smith",
            address: {
                addressLine1: "1455 Market St",
                addressLine2: "San Francisco, CA 94103",
            },
            idempotencyKey: randomUUID(),
        });

        const customer = createCustomerResponse.result.customer;

        console.log(customer);

    } catch (error) {
        if (error instanceof ApiError) {
            error.result.errors.forEach(function (e) {
                console.log(e.category);
                console.log(e.code);
                console.log(e.detail);
            });
        } else {
            console.log("Unexpected error occurred: ", error);
        }
    }
};

createCustomer();

I also created a different account from the “Default test Account” but nothing has been working.

What’s the application ID you’re getting this error with? The one I found the API Logs are showing that all the calls to CreateCustomer are successful. It the call to CreateBooking thats failing with UNAUTHORIZED cause the seller hasn’t yet signed up for Appointments. :slightly_smiling_face:

1 Like

The application ID is sandbox-sq0idb-bgRR4d1jzG_fqcJNZbRivg.

One of the createCustomer was made on the Sandbox Connect Website.
I also haven’t tried the bookingAPI on my local workspace. I only used it on the API explorer.

Okay, I only see logs for request from the API Explorer. Is the client that the credentials are configured set to target the sandbox environment? An UNAUTHORIZED error only happens when credentials or environment aren’t configured correctly. :slightly_smiling_face:

Yes. Environment is configured to sandbox.

I had to make a few changes but I was able to get your code to work with the following:

import { Client, Environment, ApiError } from "square";
import { randomUUID } from "crypto";

const client = new Client({
  accessToken: process.env.SQUARE_ACCESS_TOKEN,
  environment: Environment.Sandbox,
});

const {customersApi} = client;

async function createCustomer() {
    try {
        let createCustomerResponse = await customersApi.createCustomer({
            givenName: "Mary",
            familyName: "Smith",
            address: {
                addressLine1: "1455 Market St",
                addressLine2: "San Francisco, CA 94103",
            },
            idempotencyKey: randomUUID(),
        });

        const customer = createCustomerResponse.result.customer;

        console.log(customer);

    } catch (error) {
        if (error instanceof ApiError) {
            error.result.errors.forEach(function (e) {
                console.log(e.category);
                console.log(e.code);
                console.log(e.detail);
            });
        } else {
            console.log("Unexpected error occurred: ", error);
        }
    }
};

createCustomer();

I used our quickstart setup with the above. :slightly_smiling_face:

Oh I see! Thank you for the help. I appreciate it lots! :slight_smile: