On this page
No Headings
Last updated: June 17, 2026
Correctly processing API responses ensures that your application can gracefully manage successful payments, declines, errors, and edge cases. This improves user experience, reduces failed transactions, and helps maintain compliance with PCI DSS, the security standards for handling credit card data.
This guide provides software developers with best practices, tools, and integration details for handling PayPal API responses. It includes tips for both back-end and front-end environments.
When integrating with PayPal, keep these principles in mind:
This image shows the basic flow for handling API responses:
PayPal API responses generally fall into two categories:
A successful response has a status code between 200 and 299 and includes the requested data:
{
"id": "PAY-1234567890ABCDEF",
"status": "COMPLETED",
"amount": {
"currency": "USD",
"value": "15.00"
}
}Error responses have status codes outside the 200-299 range and include error details:
{
"name": "VALIDATION_ERROR",
"message": "Invalid request - see details",
"debug_id": "9adb23571c146",
"details": [
{
"field": "amount",
"issue": "cannot be negative"
}
]
}The following table shows common HTTP status codes from PayPal APIs and what they mean:
| HTTP status code | Error name | What it means | What to do |
|---|---|---|---|
400 Bad Request | INVALID_REQUEST | Formatting errors in your request. | Check your JSON structure. |
401 Unauthorized | AUTHENTICATION_FAILURE | Missing or expired access token. | Get a new access token. |
403 Forbidden | NOT_AUTHORIZED | No permission for this action. | Check account permissions. |
404 Not Found | RESOURCE_NOT_FOUND | Resource doesn't exist. | Verify the ID or URL. |
422 Unprocessable Entity | UNPROCESSABLE_ENTITY | Business rule errors. | Check error details. |
500 Internal Server Error | INTERNAL_SERVER_ERROR | PayPal server issue. | Try again later. |
For more details, see the Common errors page.
| Name | Purpose | Integration location |
|---|---|---|
| Rest API response handling | Get formatted answers for payments and orders. | Back-end and front-end |
| Error handling and codes | Find and fix errors and warnings. | Back-end and front-end |
| Negative testing tools | Create test errors and declined payments. | Sandbox (back-end and front-end) |
| Card decline and funding failure handling | Help users when cards are declined. | Back-end and front-end |
PayPal APIs send back structured JSON answers when you make requests about payments, orders, and accounts. These answers include status codes, results, and error details. Always check the status code and read the full response to handle both successes and errors well. Learn more in the REST API responses guide.
PayPal gives you specific error codes and messages to help you fix issues. By checking these codes and showing helpful messages to users, your app can guide people through problems and make payments easier.
Here are the most common error codes you'll see and how to fix them:
| Error | What it means | What to do |
|---|---|---|
VALIDATION_ERROR | Required fields are missing or have incorrect values. | Check the validation error page for field requirements. |
DUPLICATE_INVOICE_ID | You used the same invoice ID more than once. | Use a unique invoice ID for each transaction. |
CARD_EXPIRED | Customer's card is expired. | Ask them to use a different card. |
ORDER_ALREADY_CAPTURED | The payment was already processed. | Check your records for the original payment. |
PAYER_ACTION_REQUIRED | The customer needs to do something. | Redirect the customer to the rel:payer-action HATEOAS link in the response. |
These guides have more details about handling errors:
PayPal offers tools to test how your app handles errors. These tools let you create test errors and declined payments without real problems. Use special headers and settings to test your error handling.
You can create specific test errors by adding a header to your test API requests:
PayPal-Mock-Response: {"mock_application_codes":"INSTRUMENT_DECLINED"}This lets you test error handling without causing real errors.
Learn more about testing from these guides:
When a card is declined or payment fails, your app needs to handle it smoothly. Your system should spot these issues and give users clear feedback and options to try again. This helps them understand what went wrong and how to fix it.
Learn more about handling payment problems:
This section explains how to handle PayPal API responses and errors in both back-end and front-end environments.
The back end handles secure tasks like creating payments, checking transactions, and storing API keys. It talks directly to PayPal's APIs, processes responses, handles errors, and keeps sensitive data safe.
Code examples in various programming languages show how to get access tokens and handle API responses securely. These examples show the right patterns to follow in your back-end code.
curl -X POST https://api-m.sandbox.paypal.com/v1/payments/payment \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ACCESS_TOKEN" \
-d '{...}'The front end interacts with users, starts payments, and shows feedback based on API responses. Good response handling ensures users know the payment status, can retry if needed, and get clear guidance when something goes wrong.
Important: Never send your API keys or tokens from front-end code. The front-end should talk to your secure back end, which then talks to PayPal.
Code examples show how to handle responses and errors in various front-end frameworks. These examples help you create user-friendly payment flows.
// paypal.js
export async function createPayment() {
const response = await fetch('https://api-m.sandbox.paypal.com/v1/payments/payment', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ACCESS_TOKEN'
},
body: JSON.stringify({...})
});
const data = await response.json();
if (response.status === 201) {
alert('Payment created!');
} else {
alert('Error: ' + response.status);
}
}
// index.html
<script type="module">
import { createPayment } from './paypal.js';
document.getElementById('pay-btn').addEventListener('click', createPayment);
</script>
<button id="pay-btn">Pay with PayPal</button>Testing your security setup is vital to protect against threats. Consider these key tests:
For complete testing info, see the Testing your response handling guide.
Follow these steps to test how your app handles PayPal responses:
PayPal-Mock-Response: {"mock_application_codes":"INSTRUMENT_DECLINED"}.You might face these common issues:
| Problem | Solution |
|---|---|
Getting 401 errors regularly | Set up token refresh before tokens expire. |
| Webhooks not working | Make sure your webhook URL is public and events are verified. |
| Can't process refunds | Use the correct transaction ID from the original payment. |
| Test payments always fail | Check if your test buyer account has enough funds. |
When building mobile apps with PayPal: