EAAAl...)sandbox-sq0...)💡 Sandbox = test environment with fake data. No real money involved.
npx -y create-next-app@latest .
npm install square dotenv uuid
Create .env.local in project root:
SQUARE_ACCESS_TOKEN=your_sandbox_access_token_here
SQUARE_APPLICATION_ID=sandbox-sq0idb-xxxxxxxxxxxx
SQUARE_LOCATION_ID=your_sandbox_location_id_here
NEXT_PUBLIC_SQUARE_APPLICATION_ID=sandbox-sq0idb-xxxxxxxxxxxx
NEXT_PUBLIC_SQUARE_LOCATION_ID=your_sandbox_location_id_here
src/lib/square.tsThis is the foundation file. Everything else imports from here.
import { SquareClient, SquareEnvironment } from "square";
import { v4 as uuidv4 } from "uuid";
const squareClient = new SquareClient({
token: process.env.SQUARE_ACCESS_TOKEN,
environment: SquareEnvironment.Sandbox,
});
function generateIdempotencyKey() {
return uuidv4();
}
export { squareClient, generateIdempotencyKey };
Key Concept — Idempotency Keys: Square requires a unique key for every write operation. If your network fails and you retry, Square won't process the same operation twice. Always use a fresh UUID.
Simplest API — lists business locations. Great for testing credentials.
src/app/api/locations/route.ts