That’s an excellent initiative — migrating Magento 2 USPS integration from SOAP to REST is both forward-looking and increasingly necessary, since USPS has deprecated many SOAP-based endpoints and newer APIs (Shipping API v2, Label API, etc.) are REST-first.
Let’s break this into a complete migration guide, covering what you need to replace, reimplement, and test.
Overview
Current (SOAP-based)
Magento 2’s core USPS carrier module (Magento\Usps\Model\Carrier) historically used:
USPS RateV4, IntlRateV2, etc. via XML-over-HTTP (SOAP-like).
Requests built manually using XML payloads, sent with
\Magento\Framework\HTTP\ZendClient.
New (REST-based)
USPS now offers:
USPS Web Tools REST API (2024+ version) for:
Rate Quotes (
/rates/v1/rate)Labels (
/labels/v1/label)Tracking (
/tracking/v1/tracking)
Auth via OAuth 2.0 client credentials instead of static
USERID.JSON payloads instead of XML.
🧩 Step-by-Step Migration Plan
1. Get USPS Web Tools REST Credentials
Log in to USPS Web Tools Developer Portal.
Create an app and obtain:
Client IDClient Secret
Enable relevant APIs (Rates, Labels, Tracking).
2. Extend or Replace the Existing USPS Carrier Module
You can either:
✅ Extend
Magento\Usps\Model\Carrier(recommended for compatibility)❌ Or replace it entirely with a custom carrier (for clean REST-only logic)
Create your custom carrier class, e.g.:
3. Implement REST Request Logic
Old SOAP (simplified):
4. OAuth2 Token Handling
Implement a helper to handle token caching and refreshing:
5. Rate Collection
Override collectRates() in your new carrier model:
6. Testing & Validation
Use USPS sandbox API endpoints first.
Enable debug logging:
Validate:
Domestic and international rates
Handling of multiple packages
Label printing (if implemented)
Tracking numbers
7. Backward Compatibility
If you want to keep the old USPS SOAP active:
Add a toggle in Admin Configuration (
carriers/usps_rest/enable_rest_api)Allow switching between old XML and new REST logic dynamically.
⚙️ USPS REST API References
| API | Endpoint | Description |
|---|---|---|
| OAuth2 | /oauth2/v1/token | Obtain access token |
| Rate Quotes | /rates/v1/rate | Get shipping rates |
| Labels | /labels/v1/label | Create shipping labels |
| Tracking | /tracking/v1/tracking | Track shipments |
Full docs:
🔗 https://developer.usps.com/apis
🧠 Migration Tips
Don’t reuse old XML payload logic — REST uses JSON and new field names.
Handle USPS service names carefully — they differ (e.g., “PriorityMail” → “PRIORITY”).
Cache tokens and rates to improve performance.
Use try/catch around Curl requests — USPS REST APIs sometimes return 502/504 intermittently.
Buy plugin by below link
https://www.ecomplugins.com/magento2-usps-shipping-with-rest-api
