Third-party APIs fail in ordinary ways. They time out, return incomplete errors, change rate limits, deliver duplicate callbacks, and occasionally report success after your request gave up. A resilient Laravel API integration contains that uncertainty at a clear boundary.
Keep vendor concepts behind an adapter
Business code should request “send shipment” or “capture payment,” not construct a vendor SDK object throughout the application. An adapter maps internal commands and results to provider-specific fields. That boundary centralises authentication, timeouts, error translation, logs, and tests.
It also makes replacement possible. You may never switch providers, but you will update SDK versions and respond to contract changes.
Set explicit connection and response timeouts
Defaults are rarely suitable for a user-facing request. Use a short connection timeout and a bounded total timeout based on the workflow. An integration should not hold PHP workers indefinitely because a remote service is degraded.
Retry selectively with backoff
Connection failures, timeouts, 429 responses, and selected 5xx errors may recover. Invalid credentials and validation errors will not. Retry transient cases with exponential backoff and a maximum elapsed time. Respect Retry-After where provided.
Only retry operations that are safe or protected by an idempotency key. Retrying a payment create request without idempotency can create a duplicate charge.
Model results, not raw responses
Translate provider responses into a small result type: success, pending, declined, unavailable, or invalid. Store the external reference and safe diagnostic code. Do not let every controller interpret status 422 differently.
final class CaptureResult
{
public function __construct(
public readonly CaptureStatus $status,
public readonly ?string $externalId,
public readonly ?string $errorCode,
) {}
}
Move slow or recoverable work to a queue
If the user does not need the final result immediately, persist intent and process it asynchronously. Queue jobs need idempotency, bounded retries, and visible failure. Keep separate queues when a slow provider should not block unrelated work.
Use webhooks as notifications, not unquestioned truth
Verify signatures, store events before acknowledgement, and deduplicate by provider ID. Important workflows should also reconcile state through the provider’s read API. The detailed Laravel webhook guide covers replay and out-of-order delivery.
Protect logs and credentials
Record request ID, operation, duration, provider status, attempt, and safe error code. Redact access tokens, authorization headers, payment details, and sensitive payloads. Use separate credentials per environment and grant only the required provider permissions.
Test more than the happy path
- Timeout before and after the provider may have accepted work
- Rate limiting with Retry-After
- Malformed or partial responses
- Duplicate and delayed webhooks
- Credential rotation and revoked access
- Reconciliation after a prolonged outage
Observe provider health separately
Track latency, success, timeout, rate limits, and queue backlog by provider and operation. A combined “external API error” metric hides which dependency is causing customer impact. Alert on sustained failures and have a product decision for degraded mode.
For payments, CRM, inventory, or logistics integrations as part of a larger product, the Laravel API development service treats error handling and operations as part of the contract—not post-launch cleanup.
Frequently asked questions
Should every failed API call be retried?
No. Retry transient failures such as timeouts and selected 5xx responses. Validation and most 4xx errors need correction, not repetition.
Why use an adapter around an SDK?
It keeps vendor concepts out of business code, centralizes failure handling, and makes replacement and testing far easier.