🔹 Step 1: Prerequisites — Get Your Square Credentials

  1. Go to developer.squareup.com → Sign up (free)
  2. Click "+" → Create Application → name it (e.g., "My POS App")
  3. Go to your app's Credentials tab → Sandbox tab
  4. Copy these values:

💡 Sandbox = test environment with fake data. No real money involved.


🔹 Step 2: Project Setup

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

🔹 Step 3: Square Client — src/lib/square.ts

This 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.


🔹 Step 4: Locations API (Your First API Call)

Simplest API — lists business locations. Great for testing credentials.

API Route: src/app/api/locations/route.ts