What to Do If Your Square Integration Fails During Testing
When your Square integration fails during testing, start by checking your API credentials and ensuring you are using the correct Square environment (sandbox vs. production). Then, review the error codes in your API logs,...
Diagnostic Sequence: Step by Step
Follow this order to isolate the problem quickly. Do not skip steps.
- Check your Square environment. Confirm you are sending requests to the sandbox base URL (
https://connect.squareupsandbox.com) and not the production URL. A common mistake is using live credentials in sandbox or vice versa. - Verify your API credentials. Log in to the Square Developer Dashboard. Ensure your application has a valid Access Token for the sandbox environment. Tokens expire or can be revoked. Generate a new sandbox token if needed.
- Use Square's test card numbers. Square provides specific test card numbers for different scenarios (e.g., success, decline, insufficient funds). Using a real card in sandbox will fail. Refer to Square's test values documentation for the correct numbers.
- Inspect API response codes. Every Square API call returns an HTTP status code and a JSON body with error details. Look for codes like
400 BAD_REQUEST,401 UNAUTHORIZED, or404 NOT_FOUND. The error message often tells you exactly what is wrong (e.g., missing required field, invalid card data). - Check your webhook configuration. If your integration relies on webhooks (e.g., for payment notifications), ensure the endpoint URL is publicly accessible and returns a
200 OKresponse. Use a tool like webhook.site to inspect incoming payloads during testing. - Review your code for idempotency keys. Square requires an idempotency key for certain requests (like creating payments) to prevent duplicate charges. If you reuse a key across different requests, you will get a
409 CONFLICTerror. Generate a unique key for each request. - Test with Square's API Explorer. The Square API Explorer lets you make live API calls from your browser. Use it to verify that your request payload is correct before running it from your code.
Common Causes of Test Failures
Incorrect Environment or Credentials
This is the most frequent issue. Developers accidentally use production tokens in sandbox or sandbox tokens in production. Double-check the base URL and token in your configuration file. Square sandbox tokens start with EAAA (sandbox) while production tokens start with EAAA as well but are tied to a live application. The easiest way to confirm is to make a simple ListLocations call and see if it returns test locations.
Invalid Test Card Data
Square's sandbox only accepts specific test card numbers. Using a random card number will result in a 400 BAD_REQUEST with an error like CARD_TOKEN_ISSUE. Always use the official test card numbers from Square's documentation. For example, 4111 1111 1111 1111 with any future expiry date and any CVV will simulate a successful payment.
Missing or Incorrect Idempotency Keys
Square enforces idempotency for payment and order creation requests. If you send the same request twice with the same idempotency key, Square returns the same response (no duplicate charge). But if you reuse a key for a different request, you get a 409 CONFLICT. Generate a new UUID for each unique request.
Webhook Endpoint Not Reachable
If your integration uses webhooks, the endpoint must be publicly accessible during testing. Localhost URLs (e.g., http://localhost:3000/webhook) will not work. Use a tunneling service like ngrok to expose your local server, or deploy to a staging environment. Square will retry failed webhook deliveries up to three times, but you should verify the endpoint responds with 200 OK within a few seconds.
API Rate Limits
Square applies rate limits to API calls. In sandbox, the limits are lower than production. If you make too many requests in a short period, you will receive a 429 TOO_MANY_REQUESTS error. Implement exponential backoff in your code to handle this gracefully.
Key Facts About Square Integration Testing
| Fact | Detail |
|---|---|
| Sandbox URL | https://connect.squareupsandbox.com |
| Production URL | https://connect.squareup.com |
| Test card numbers | Provided by Square; do not use real cards |
| Idempotency keys | Required for payment and order endpoints |
| Webhook testing | Use ngrok or a public staging URL |
| API Explorer | Available at https://developer.squareup.com/explorer |
| Rate limits | Stricter in sandbox; implement retry logic |
Limitations of Square Sandbox Testing
The sandbox environment is not identical to production. Some features are limited or behave differently:
- No real payment processing. You cannot test actual bank transfers or card network responses. All transactions are simulated.
- No real-time webhook delivery. Webhooks in sandbox may have delays or not fire at all for certain events. Use the Square Developer Dashboard to manually trigger webhook events for testing.
- Limited location data. Sandbox locations have dummy addresses and may not reflect your actual business setup.
- No refunds or disputes. You cannot test refund flows or chargeback handling in sandbox. Those require production testing with small amounts.
If your integration relies on these features, plan for additional testing in production with a small live transaction (e.g., $1.00) after sandbox validation passes.
Terminology You Should Know
- Access Token: A secret key that authenticates your API requests. Each Square application has separate tokens for sandbox and production.
- Idempotency Key: A unique string you send with each request to ensure Square processes it only once. Prevents duplicate charges.
- Webhook: An HTTP callback that Square sends to your server when an event occurs (e.g., payment completed).
- API Explorer: A web-based tool from Square that lets you test API calls interactively.
- Sandbox: A test environment that mimics Square's production API but uses fake data.
Frequently Asked Questions
Why do I get a 401 Unauthorized error in sandbox?
Your access token is likely invalid or belongs to a different Square application. Generate a new sandbox token from the Developer Dashboard and update your code.
Can I use my own credit card to test in sandbox?
No. Square's sandbox only accepts specific test card numbers. Using a real card will fail with a card error. Use the test numbers from Square's documentation.
How do I test webhooks locally?
Use a tunneling service like ngrok to expose your local server to the internet. Then set your webhook URL in the Square Developer Dashboard to the ngrok URL. Square will send events to that URL.
What does a 409 Conflict error mean?
You reused an idempotency key for a different request. Generate a new unique key (e.g., a UUID) for each API call that requires idempotency.
How long does it take for a sandbox webhook to arrive?
Webhooks in sandbox are not guaranteed to be real-time. They may arrive after a delay or not at all. Use the Developer Dashboard to manually trigger webhook events for reliable testing.
Can I test refunds in sandbox?
Yes, you can test refunds using the sandbox API. Use a test payment ID from a previous sandbox transaction. Refunds will be simulated and no real money moves.
What should I do if my integration works in sandbox but fails in production?
Check that you are using the production access token and base URL. Also verify that your production Square account is active and has the necessary permissions (e.g., payment processing enabled). Test with a small live transaction to isolate the issue.
Practical Scenarios and Decision Criteria
Understanding when to escalate or change your approach can save hours. Here are common scenarios and how to decide.
Scenario: You get a 400 error with "CARD_TOKEN_ISSUE"
This almost always means you used a real card number in sandbox. Switch to a test card from Square's list. If the error persists, check that the card data format matches Square's requirements (e.g., expiry as MM/YY, CVV as 3-4 digits).
Scenario: Webhooks never arrive
First, confirm your endpoint is publicly reachable. Use a tool like ngrok. Then, in the Square Developer Dashboard, go to Webhooks and click "Send test event." If the test fails, your endpoint may have a code error. Check server logs for incoming POST requests.
Scenario: Idempotency key errors on every request
You may be generating the same key for different requests. Ensure you create a new UUID per request. Some developers accidentally hardcode a static key. Use a library like uuid in Node.js or UUID.randomUUID() in Java.
Scenario: Rate limit errors during load testing
Sandbox rate limits are lower than production. If you are load testing, add delays between requests. Square recommends at least 1 second between calls. Use exponential backoff: wait 1 second, then 2, then 4, up to a maximum.
Why These Steps Matter
Each step in the diagnostic sequence targets a specific failure mode. Skipping steps leads to wasted time. For example, checking credentials first avoids debugging a correct code path with wrong tokens. Inspecting error codes gives you the exact problem, not a guess. Using test cards ensures you are not blocked by real card network rules. Webhook verification prevents silent failures where payments succeed but notifications never arrive. Idempotency keys protect against duplicate charges even in testing. The API Explorer confirms your payload structure before you write code. Together, these steps reduce debugging time from hours to minutes.
Further reading and comparison sources
These external sources provide additional context for evaluating the topic. Their inclusion is not an endorsement.
Further reading and comparison sources
These external sources provide additional context for evaluating the topic. Their inclusion is not an endorsement.
Learn more
Visit the website for more information.