Integrations · E-commerce
Validate emails of new Square customers
Check the email address of each new Square customer. A small relay you deploy to your own Cloudflare account receives your Square application's webhook notifications, checks Square's signature, checks the new customer's address with our API and flags an invalid or disposable one: by adding the customer to a customer group of yours, with a signed notice to a URL of yours, or both. The notice holds ids and the result, never the address.
It never touches money. It never touches a payment, an order, a refund or an invoice, never deletes a customer and never changes a customer's fields. Its only write to Square is adding a flagged customer to the group you picked. A flagged customer is a hint to look (a typo is common and harmless), not a verdict on the buyer.
It cannot stop Square's checkout. Square sends its notifications after the fact, so the relay hears of a customer only once Square has created the customer profile.
Status: set up from this page. There is no Square App Marketplace listing: you run the relay in your own Cloudflare account, with your own Square application. It has not yet been tried on a real Square account, so try it in Square's sandbox first.
Download it: email-validation-square.zip holds the relay as one Cloudflare Worker module (relay.mjs) and the MIT licence.
What you need
- An API key of ours from the API keys page with Validate addresses (on by default). It must be a server key: a browser key is refused.
- A Square application in the Square Developer Console. Start in its Sandbox: it is free, and the Sandbox Square Dashboard sends no email (Square's Sandbox overview).
- For the group flag: a customer group and a Square access token that can read and write customers (step 2).
- A Cloudflare account and Cloudflare's
wranglercommand (npx wrangler, on a computer with Node). - For the notice: an HTTPS URL of yours (your own app, or an automation tool's webhook) and a secret of your own that signs it.
Our key goes only to our API, the Square token only to Square's API (https://connect.squareup.com, or https://connect.squareupsandbox.com in the sandbox). The relay's log holds event and customer ids, statuses and error codes: never an address or a key.
Set it up
Deploy the relay once from the unzipped folder, to get its address:
cd email-validation-square npx wrangler deploy relay.mjs --name ev-square-relay --compatibility-date 2026-10-01wrangler deployprints the Worker's address, such ashttps://ev-square-relay.<your subdomain>.workers.dev. Until step 4 the relay answers every notification403: it is not set up yet.For the group flag, in the sandbox first:
- Create a customer group, such as "Email flagged": in the Square Dashboard, Customers → Customer directory → Create group → Manual group (Square's help). The Sandbox Square Dashboard opens from the Developer Console: Sandbox test accounts → Square Dashboard.
- Find the group's id with ListCustomerGroups in Square's API Explorer (ListCustomerGroups). CreateCustomerGroup there makes the group and shows its id in one step.
- Copy an access token from your application's Credentials page (the Sandbox one for the sandbox). That token can do everything in its Square account; an OAuth token with only CUSTOMERS_READ and CUSTOMERS_WRITE is narrower (Square's access tokens).
Add the webhook subscription. In the Developer Console, open your application, choose Sandbox at the top, then Webhooks → Subscriptions → Add subscription: a name, the relay's address as the notification URL, an API version, and the event customer.created; then Save. Open the subscription and, in Signature Key, choose Show (Square's subscription steps).
Deploy the relay with its settings (here both the group flag and the notice), and store its secrets:
npx wrangler deploy relay.mjs --name ev-square-relay --compatibility-date 2026-10-01 \ --var SQUARE_NOTIFICATION_URL:https://ev-square-relay.….workers.dev \ --var SQUARE_ENVIRONMENT:sandbox --var SQUARE_FLAG_GROUP_ID:… \ --var FORWARD_URL:https://… npx wrangler secret put EV_API_KEY --name ev-square-relay # our API key npx wrangler secret put SQUARE_SIGNATURE_KEY --name ev-square-relay # the subscription's signature key npx wrangler secret put SQUARE_ACCESS_TOKEN --name ev-square-relay # with SQUARE_FLAG_GROUP_ID npx wrangler secret put FORWARD_SECRET --name ev-square-relay # with FORWARD_URL: signs the noticeReplace each
…with your own value: the relay's address from step 1, the group's id from step 2 and your URL.SQUARE_NOTIFICATION_URLis the notification URL exactly as you entered it in Square: it is part of Square's signature, so a trailing slash or another host makes every signature mismatch. At least one ofSQUARE_FLAG_GROUP_IDandFORWARD_URLmust be set.Production has its own subscription, signature key and access token: once the sandbox works, deploy a second relay (for example
ev-square-relay-live) with your production token, the production subscription's key and--var SQUARE_ENVIRONMENT:production(Square's move to production). It has a new address, so repeat steps 1 to 4 for it: deploy it, add the production subscription with that address, then redeploy it withSQUARE_NOTIFICATION_URLset to that address andSQUARE_FLAG_GROUP_IDset to a group of your production account.
A relay missing a setting answers 403, as to a bad signature, and logs the setting's name (npx wrangler tail ev-square-relay shows its log). A deploy replaces the settings made with --var (secrets stay), so every redeploy repeats every --var you use.
How it works
- The signature first. Square signs each notification with
x-square-hmacsha256-signature: a SHA-256 signature, in base64, of the notification URL followed by the raw body, under the subscription's signature key (Square's validation guide). The relay computes it from the exact bytes it received and compares in constant time. Anything else is answered403and starts nothing. - Answered at once. A verified notification is answered
200 okat once, and the check runs after the answer. Square wants a2xxwithin 10 seconds and sends a notification again for up to 24 hours (Square's webhooks overview). - Once per event. Each relay instance remembers every
event_idfor three days. A Worker instance can restart at any time, so a repeat may start again. Square's signature holds no time, so a notification whose event is more than 25 hours old is a replay and starts nothing. - The address. Only
customer.createdis acted on: the new customer'semail_address. A profile that brings no new address is skipped, so it does not spend a check: acreation_sourceofIMPORT,THIRD_PARTY_IMPORT,MERGE,DELETION_RECOVERYorUNMERGE_RECOVERY. A profile Square creates after a payment (INSTANT_PROFILE), one made in the Dashboard, by an invoice, an appointment or through the API is checked. - Not checked twice by one instance. A relay instance skips a customer and address it checked in the last three days (it keeps a SHA-256 of the address, never the address), and a customer created already in the flag group. Across instances there is no memory: a notification sent again that reaches another instance, or comes after a restart, can be checked again.
- The flag, for a status in
EV_FLAG(by defaultinvalidanddisposable; nevervalid):- Group (
SQUARE_FLAG_GROUP_ID): the relay reads the customer again and, while it still holds the address that was checked and is not in the group yet, adds it to the group. In the Square Dashboard's customer directory you can filter on the group. - Notice (
FORWARD_URL): a JSON notice is posted to your URL (one try, 5 seconds, redirects not followed). WithFORWARD_ALL=trueevery checked address is sent, not only a flagged one.
- Group (
The notice holds ids and the result, never the address (look the customer up by customer_id):
{ "type": "square.customer.flagged", "checked_at": "2026-11-21T12:00:00.000Z",
"square": { "merchant_id": "…", "event_id": "…", "event_type": "customer.created",
"customer_id": "…", "creation_source": "INSTANT_PROFILE" },
"result": { "status": "invalid", "reason": "no_mailbox", "flagged": true, "request_id": "req_…" } }Its header X-EV-Signature is sha256= and the base64 SHA-256 signature of the raw body under FORWARD_SECRET. Compute the same on your side and compare in constant time. The signature holds no time, so act once per event_id. A notice that is not flagged has the type square.customer.checked.
Try it free first
First on your machine: save this as try.mjs beside relay.mjs and run it with node try.mjs and your key in EMAIL_VALIDATION_API_KEY. It signs one customer.created the way Square does, hands it to the relay and prints the notice instead of sending it; only the check goes out, to our API.
import { createHmac, randomUUID } from 'node:crypto';
import relay from './relay.mjs';
const url = 'https://relay.example/'; // stands in for your relay's address
const env = {
EV_API_KEY: process.env.EMAIL_VALIDATION_API_KEY,
SQUARE_SIGNATURE_KEY: 'try-signature-key',
SQUARE_NOTIFICATION_URL: url,
FORWARD_URL: 'https://notice.example/',
FORWARD_SECRET: 'try-forward-secret',
};
// the notice is printed here instead of sent; the check goes to our API
const send = globalThis.fetch;
globalThis.fetch = (to, init) =>
String(to).startsWith(env.FORWARD_URL) ? (console.log(init.body), Promise.resolve(new Response('ok'))) : send(to, init);
const customer = { id: 'TRYCUSTOMER', creation_source: 'DIRECTORY', email_address: 'no_mailbox@sandbox.email-validation.org' };
const body = JSON.stringify({
merchant_id: 'TRYMERCHANT',
type: 'customer.created',
event_id: randomUUID(),
created_at: new Date().toISOString(),
data: { type: 'customer', id: customer.id, object: { customer } },
});
const signature = createHmac('sha256', env.SQUARE_SIGNATURE_KEY).update(url + body).digest('base64');
const headers = { 'x-square-hmacsha256-signature': signature };
const answer = await relay.fetch(new Request(url, { method: 'POST', headers, body }), env);
console.log(answer.status);It prints the notice ("status":"invalid","reason":"no_mailbox","flagged":true), the relay's log line (TRYCUSTOMER: invalid, flagged; forwarded) and 200. Sandbox checks cost no credit. That domain receives no mail, so send it nothing.
Then in Square's sandbox: in the Sandbox Square Dashboard, add one customer with your own email address. The relay's log (npx wrangler tail ev-square-relay) shows the customer's id with its status, and with FORWARD_ALL=true your URL gets a square.customer.checked notice. That check uses one credit unless the answer is unknown. The subscription's event log in the Developer Console shows the relay's answers. Try it with a real sandbox customer, not the Developer Console's test event: that sample can be older than 25 hours, and the relay then ignores it as a replay.
Settings
| Setting | Default | What it does |
|---|---|---|
EV_API_KEY | required (secret) | Our API key (Validate addresses). A browser key is refused. |
SQUARE_SIGNATURE_KEY | required (secret) | The webhook subscription's signature key. |
SQUARE_NOTIFICATION_URL | required | The subscription's notification URL, exactly as entered in Square. |
SQUARE_FLAG_GROUP_ID | none | The id of the customer group a flagged customer is added to (needs SQUARE_ACCESS_TOKEN). |
SQUARE_ACCESS_TOKEN | with the group (secret) | A Square token that can read and write customers. |
SQUARE_ENVIRONMENT | production | production or sandbox: which Square API the token belongs to. |
FORWARD_URL | none | An HTTPS URL of yours that gets the signed notice. |
FORWARD_SECRET | with FORWARD_URL (secret) | Signs the notice. |
FORWARD_ALL | false | true: a notice for every checked address, not only a flagged one. |
EV_FLAG | invalid,disposable | Statuses that flag: invalid, disposable, catch_all, unknown (never valid). |
EV_REGION | us | eu checks the address in the EU and stores its result there (same keys and credits). |
Settings marked secret go in with npx wrangler secret put; the others with --var NAME:value on the deploy command. A deploy replaces the settings made with --var (secrets stay) and wipes any set in the Cloudflare dashboard, so every redeploy repeats every --var you use.
Credits and limits
Each check uses one credit; unknown results and sandbox addresses are free. A customer and address the same relay instance checked in the last three days costs nothing again. A Worker instance can restart at any time, so a repeat that reaches another instance, or comes after a restart, may be checked and charged again, with or without the group.
A flagged customer costs the relay two Square calls (the customer read and the group write); a customer that is not flagged costs none. Put the relay on a Worker of its own.
It fails open: a timeout, a network error, a rate limit or any error answer from our API or from Square flags nothing, is logged, and the notification is still answered 200. A 429, a 503 or a refused key ran no check and used no credit, so a later notification about that customer is checked again.