Square Web Payments SDK integration guide

Applies to: Web Payments SDK | Payments API

Learn to use the Square Web Payments SDK to accept online payments by card, Apple Pay, Google Pay, and ACH bank transfer.

Link to section

Overview

This guide walks you through integrating online payments using the Square Web Payments SDK. It covers credit and debit cards, digital wallets (Apple Pay and Google Pay), and ACH bank transfers.

The SDK converts sensitive payment data into a single-use token on the client side. Your server never handles raw card numbers or bank details. Pass the resulting payment token as source_id to the Payments API.

Link to section

References

Link to section

How the payment flow works

Payment flow from the browser to your server and the Square API: load the SDK, collect and tokenize payment information, send the token to your server, call CreatePayment, and show the payment result to the buyer.

Link to section

Before you begin

You need the following:

Link to section

Set up the Web Payments SDK

This client-side setup is shared by all payment methods in this guide.

Include the Web Payments SDK in your HTML:

<script async src="https://web.squarecdn.com/v1/square.js"></script>

Initialize the SDK:

const payments = Square.payments(APPLICATION_ID, LOCATION_ID);

Important

The script URL and API request examples in this guide use the production environment. For testing, use the Sandbox script, API endpoint, and matching credentials described in Test your integration. Keep access tokens on your server, not in client-side code.

The examples below show individual integration steps, not a complete application. Run initialization code after the SDK has loaded and the relevant HTML elements are available. Implement sendTokenToServer() in your application to send the payment token to your backend.

Link to section

Accept credit and debit cards

Add the card container and payment button to your HTML:

<div id="card-container"></div> <button id="card-button">Pay</button>

Create and attach the card form:

const card = await payments.card(); await card.attach('#card-container');

Tokenize the payment information when the buyer submits the form:

Note

The verificationDetails object triggers buyer verification, such as 3D Secure (3DS) or Strong Customer Authentication (SCA), when required. The buyer information in this example is illustrative. Collect the buyer's details rather than hard-coding them in production.

Link to section

Accept Apple Pay

Before adding Apple Pay:

  • Register and verify your domain in the Square Developer Dashboard under Apple Pay settings.
  • Place the .well-known/apple-developer-merchantid-domain-association file in your public directory.

Add the Apple Pay button to your HTML:

<button id="apple-pay-button"></button>

Create a payment request and initialize Apple Pay:

const paymentRequest = payments.paymentRequest({ countryCode: 'US', currencyCode: 'USD', total: { amount: '1.00', label: 'Total' }, }); const applePay = await payments.applePay(paymentRequest);

Tokenize the payment information in the button's click handler:

document .getElementById('apple-pay-button') .addEventListener('click', async (event) => { event.preventDefault(); const tokenResult = await applePay.tokenize(); if (tokenResult.status === 'OK') { await sendTokenToServer(tokenResult.token); } });

Important

Apple requires you to call tokenize() directly inside the button's click handler. Don't perform asynchronous operations, such as API calls, before calling tokenize().

Link to section

Accept Google Pay

Your integration must comply with the Google Pay API Terms of Service and brand guidelines.

Add a container for the Google Pay button to your HTML:

<div id="google-pay-button"></div>

Create a payment request, initialize Google Pay, and attach the button:

const paymentRequest = payments.paymentRequest({ countryCode: 'US', currencyCode: 'USD', total: { amount: '1.00', label: 'Total' }, }); const googlePay = await payments.googlePay(paymentRequest); await googlePay.attach('#google-pay-button');

Tokenize the payment information:

document .getElementById('google-pay-button') .addEventListener('click', async () => { const tokenResult = await googlePay.tokenize(); if (tokenResult.status === 'OK') { await sendTokenToServer(tokenResult.token); } });

Important

For Apple Pay and Google Pay, call tokenize() without the card-specific verificationDetails argument. Successful wallet tokenization doesn't guarantee that buyer verification ran for every integration. Follow the buyer-verification guidance in Apple Pay and Google Pay, including a separate payments.verifyBuyer() step when required. In that flow, verify the buyer after successful tokenization and before creating the payment. Send the returned verification token along with the payment token to your server, and include it as verification_token in the CreatePayment request. The wallet examples above show tokenization only, not the separate verification step.

Link to section

Accept ACH bank transfers

ACH bank transfers have the following limitations:

  • They're available in the United States only.
  • Settlements take 2–3 business days.
  • They use Plaid for instant bank account verification.

Add form fields to collect the buyer's identity, which is required for ACH:

Initialize the ACH payment method without options. The SDK generates a transaction ID when you don't provide one:

const ach = await payments.ach();

Note

ACH doesn't have an attach() method. Calling tokenize() starts the Plaid bank authentication flow.

Register an ontokenization listener once, after initializing ach, to receive the payment token after the buyer authorizes their bank account through Plaid. Read the buyer's first and last names from the form, validate them, and pass the combined name to tokenize() to start authorization.

sendTokenToServer() must reject unsuccessful backend responses so the event listener can report an unconfirmed payment result. ACH submission doesn't mean the payment is complete; track its final status on your server.

Link to section

Create a payment on your server

Each payment method produces a token that you pass as source_id to the CreatePayment endpoint:

POST https://connect.squareup.com/v2/payments

Set SQUARE_ACCESS_TOKEN in your server environment to the access token for the environment you're using. Don't put the token in client-side code or commit it to source control.

The following example creates a $10.00 USD payment:

curl https://connect.squareup.com/v2/payments \ -X POST \ -H 'Square-Version: 2026-01-22' \ -H "Authorization: Bearer ${SQUARE_ACCESS_TOKEN}" \ -H 'Content-Type: application/json' \ -d '{ "idempotency_key": "UNIQUE_KEY_PER_UNIQUE_PAYMENT_REQUEST", "source_id": "TOKEN_FROM_FRONTEND", "amount_money": { "amount": 1000, "currency": "USD" }, "autocomplete": true }'

Use the following required fields:

FieldDescription
source_idThe payment token from the client-side SDK.
idempotency_keyA unique string, such as a UUID, for each new payment request. Reuse the same key when retrying that request to prevent duplicate charges.
amount_money.amountThe amount in the currency's smallest denomination. For example, 1000 is $10.00 USD.
amount_money.currencyThe ISO 4217 currency code.

Important

Keep the payment amount and currency consistent between client-side tokenization and the server-side payment request. The card and ACH examples use $10.00 USD; the Apple Pay and Google Pay examples use $1.00 USD. Adjust the server-side amount to match the payment you're creating.

Link to section

Authorize a payment and capture it later

To authorize a payment without immediately capturing funds, set autocomplete to false in the CreatePayment request:

curl https://connect.squareup.com/v2/payments \ -X POST \ -H 'Square-Version: 2026-01-22' \ -H "Authorization: Bearer ${SQUARE_ACCESS_TOKEN}" \ -H 'Content-Type: application/json' \ -d '{ "idempotency_key": "UNIQUE_KEY_PER_UNIQUE_PAYMENT_REQUEST", "source_id": "TOKEN_FROM_FRONTEND", "amount_money": { "amount": 1000, "currency": "USD" }, "autocomplete": false }'

The response includes a payment with the status APPROVED. Use the payment's id in subsequent requests.

Link to section

Authorization hold

For online (card-not-present) payments, the authorization hold lasts 7 days by default. To shorten the default authorization period, set delay_duration to at least 1 minute and less than 7 days.

Link to section

Complete the payment

To capture the authorized payment, replace PAYMENT_ID with the payment's id and call CompletePayment:

curl https://connect.squareup.com/v2/payments/PAYMENT_ID/complete \ -X POST \ -H "Authorization: Bearer ${SQUARE_ACCESS_TOKEN}" \ -H 'Content-Type: application/json'
Link to section

Cancel the authorization

To cancel the authorized payment, call CancelPayment:

curl https://connect.squareup.com/v2/payments/PAYMENT_ID/cancel \ -X POST \ -H "Authorization: Bearer ${SQUARE_ACCESS_TOKEN}" \ -H 'Content-Type: application/json'
Link to section

Automatic action

If you don't complete or cancel the payment within the hold period, Square cancels it by default. To capture the payment automatically when the authorization period expires, set delay_action to COMPLETE. This option is supported only when the CreatePayment request doesn't explicitly specify an order_id.

Important

ACH payments don't support delayed capture. For ACH payments, set autocomplete to true.

For more information, see Delayed capture of a card payment.

Link to section

Record cash and external payments

Use CreatePayment to record payments that weren't processed through Square. Set source_id to CASH or EXTERNAL instead of a payment token.

Note

Cash and external payments are for bookkeeping only. Square doesn't deposit these payment proceeds into the seller's Square account.

Link to section

Cash payment

{ "idempotency_key": "UNIQUE_KEY_PER_UNIQUE_PAYMENT_REQUEST", "source_id": "CASH", "amount_money": { "amount": 1000, "currency": "USD" }, "cash_details": { "buyer_supplied_money": { "amount": 2000, "currency": "USD" } } }

The API calculates change_back_money in the response.

For more information, see Cash payments.

Link to section

External payment

The following example records a payment processed by a third-party processor or delivery service:

{ "idempotency_key": "UNIQUE_KEY_PER_UNIQUE_PAYMENT_REQUEST", "source_id": "EXTERNAL", "amount_money": { "amount": 5000, "currency": "USD" }, "external_details": { "type": "OTHER", "source": "Food Delivery Service", "source_fee_money": { "amount": 500, "currency": "USD" } } }

For more information, see External payments.

Link to section

Test your integration

Link to section

1. Get Sandbox credentials

  1. Open the Square Developer Dashboard.
  2. Select your application and switch to Sandbox mode.
  3. Copy your Sandbox application ID, access token, and location ID.
Link to section

2. Use the Sandbox environment

  • API base URL: https://connect.squareupsandbox.com.
  • SDK script URL: https://sandbox.web.squarecdn.com/v1/square.js.
  • Initialize the SDK with your Sandbox application ID and location ID.
Link to section

3. Test card payments

Use the current test values in Sandbox payment testing to test successful card payments, declined cards, and CVV checks. That reference provides the supported card numbers and the special input values for each scenario. Use Sandbox test values, not real payment card information.

Link to section

4. Test digital wallets

  • Apple Pay: Register a Sandbox domain in the Square Developer Dashboard under Apple Pay settings.
  • Google Pay: Use your Sandbox credentials.
Link to section

5. Test ACH bank transfers

Use the Plaid Sandbox flow. You don't need a real bank connection.

Link to section

6. Verify the result

  • For an immediately captured card payment, confirm that the payment status is COMPLETED.
  • ACH payments initially return PENDING. Subscribe to payment.updated webhook events to track the final result.
  • Confirm that payments appear under Transactions in the Square Developer Dashboard.
Link to section

Troubleshoot common issues

IssueWhat to check
Missing HTTPSThe SDK requires HTTPS. Check the URL and certificate for your payment page.
Duplicate chargesGenerate a unique idempotency_key for each new payment. Reuse that key when retrying the same request.
Amount mismatchFor USD, client-side tokenization uses a decimal string such as '10.00', while the Payments API uses an integer in cents, such as 1000.
Expired tokensPayment tokens are single-use and short-lived. Tokenize immediately before calling your backend.
Apple Pay click handlerCall tokenize() directly inside the button's click handler. Don't add asynchronous operations before it.