Get a public URL for Square webhooks#
Square can't deliver to localhost. An OtterKit webhook endpoint gives you a public HTTPS URL that answers Square around the clock and keeps every request, so nothing is lost while your laptop is closed or your handler is half written:
npx otterkit login
npx otterkit webhook --daemon --subdomain square-dev
# https://square-dev.otterkit.appThe name is reserved to your account, so the URL stays the same every time you start it. An endpoint costs $0.01 per hour while it is live and never more than $3 in any 30 days, and new accounts get 10 free credits. To look at a payload before you sign up, the try page gives you a live URL in the browser with no account.
Register the URL in Square#
- In the Square Developer Console, open your application and go to Webhooks, then Subscriptions. Pick Sandbox or Production at the top.
- Add a subscription, paste the endpoint URL as the notification URL and choose the events, such as
payment.updated. - Save, then open the subscription and reveal its signature key.
Use https://square-dev.otterkit.app as the URL. Add a path if your handler expects one, such as https://square-dev.otterkit.app/webhooks/square: the endpoint accepts any path and records it.
Verify the signature key#
Square sends x-square-hmacsha256-signature: a base64 HMAC-SHA256, keyed with the signature key, of the notification URL followed by the raw body. The URL must be exactly the one registered on the subscription, including the scheme and path.
That detail matters when requests pass through a proxy or a replay: the URL your handler sees may not be the one Square signed. Verify against the registered URL, not the incoming one.
OtterKit can run the same check on every request as it arrives. Start the endpoint again with the secret, and each capture is marked valid, invalid or missing:
npx otterkit stop square-dev
npx otterkit webhook --daemon --subdomain square-dev --verify square:your-signature-keyA failed check comes with its reason, such as signature_mismatch, so a wrong secret and a changed body look different. See signature verification for all 16 presets and the generic HMAC mode.
Send a test event#
Open the subscription and click Send test event, or make a payment in the Sandbox with a test card.
Then watch it land, or wait for it in a script:
# Follow requests as they arrive, with headers and bodies
npx otterkit inspect square-dev --follow
# Or block until the next POST, then print it as JSON
npx otterkit await square-dev --method POST --count 1 --timeout 120s --jsonReplay Square events into your local handler#
Once an event is captured you can send it to your handler as often as you like, with the original headers and body, without asking Square to fire it again:
# Send the latest captured event to a server on port 3000
npx otterkit replay square-dev --target 127.0.0.1:3000
# Or a specific one (1 is the oldest, -1 the latest)
npx otterkit replay square-dev --index 3 --target 127.0.0.1:3000Replays cost nothing and are added to the capture log with your handler's response next to them. If you would rather have events delivered live while you work, expose the handler with a tunnel instead and register the tunnel URL.
Retries, timeouts and duplicates#
- Square expects a
2xxwithin ten seconds and retries failures with growing gaps for up to a day. Each notification has anevent_idyou can use to skip duplicates. - Sandbox and Production have separate subscriptions and separate signature keys.
To see how your code copes when a delivery fails, the HTTP status codes guide has URLs that return any status on demand.
Questions about Square webhooks#
Why does my Square signature check fail?#
Usually the URL. The signed text starts with the notification URL exactly as registered, so a different host, a missing path or http instead of https changes the result.
What about the x-square-signature header?#
That is the older SHA-1 signature. Check x-square-hmacsha256-signature.
Can I replay a Square event into localhost and still verify it?#
Yes, as long as your handler verifies against the registered public URL rather than localhost.