SDKs overview
The UPS Developer Kit provides a suite of Application Programming Interfaces (APIs) designed to integrate UPS shipping and logistics services directly into business applications. These APIs are primarily offered as SOAP and RESTful services, supporting various functionalities such as calculating shipping rates, generating shipping labels, tracking packages, and locating UPS service points. To streamline development, UPS offers officially supported SDKs and provides comprehensive documentation to facilitate the use of these APIs.
While UPS primarily provides API specifications and sample code for direct integration, a number of official and community-contributed SDKs exist to abstract the complexities of direct HTTP requests and XML/JSON parsing. These SDKs typically wrap the core UPS APIs, enabling developers to interact with UPS services using language-specific objects and methods. Access to the UPS APIs generally requires registration and approval through the UPS Developer Kit portal.
Official SDKs by language
UPS provides comprehensive documentation and sample code rather than ready-to-install SDK packages for all languages. The primary method for integrating with UPS APIs involves consuming their SOAP and RESTful web services directly. However, the developer kit frequently includes code samples and helper classes that function as SDKs for common programming languages, particularly for .NET and Java environments. These resources are typically found within the UPS Developer Kit documentation.
For RESTful APIs, developers can use standard HTTP client libraries available in virtually any modern programming language (e.g., Python's requests, Node.js's node-fetch, Java's HttpClient) to interact with the UPS API endpoints. The primary data formats supported are XML for SOAP services and JSON for RESTful services. While a structured table of standalone SDK packages isn't directly provided by UPS for all languages, the following represents common approaches and available resources:
| Language | Approach/Package | Maturity | Notes |
|---|---|---|---|
| .NET (C#) | UPS Developer Kit Samples / Custom Wrapper | Stable | Official code samples for SOAP and REST APIs are available. Developers often build custom .NET wrappers. |
| Java | UPS Developer Kit Samples / Custom Wrapper | Stable | Similar to .NET, official samples are provided. Java developers often create their own service clients. |
| PHP | Community Libraries / Direct API Calls | Varies | No official PHP SDK; community libraries like php-ups-api exist. |
| Python | Community Libraries / Direct API Calls | Varies | No official Python SDK; community packages like python-ups are available. |
| Node.js | Community Libraries / Direct API Calls | Varies | No official Node.js SDK; developers typically use HTTP clients with JSON. |
Installation
Installation methods vary depending on whether an official sample, a community library, or a direct API integration approach is chosen. For official UPS Developer Kit samples, installation typically involves downloading the sample code package from the UPS Developer Kit website and integrating the relevant files into your project. For languages like .NET or Java, this might mean incorporating provided classes or libraries directly.
For Community Libraries (Example: PHP php-ups-api)
Community-maintained libraries are often distributed via language-specific package managers. For instance, a PHP library might be installed using Composer:
composer require michael-rotterdam/php-ups-api
For Community Libraries (Example: Python python-ups)
Python libraries are typically installed via pip:
pip install python-ups
For Direct API Integration
When integrating directly with UPS APIs, no external SDK installation is required beyond your chosen language's standard HTTP client library. For example, in JavaScript/Node.js, you might install axios or use the built-in fetch API:
npm install axios
Or for Python, the requests library is common:
pip install requests
After installation, the primary steps involve obtaining API credentials from UPS, configuring your application with these credentials, and then making authenticated requests to the appropriate UPS API endpoints, parsing the XML or JSON responses as required by the UPS API specifications.
Quickstart example
This quickstart demonstrates a simplified example of how one might initiate a package tracking request using Python, assuming direct REST API integration. This approach mirrors how many community libraries operate by making HTTP requests and processing JSON responses.
Prerequisites:
- A UPS Shipper Number and API Access Key obtained from the UPS Developer Kit.
- Python installed.
- The
requestslibrary installed (pip install requests).
Python Code for Tracking a Package:
import requests
import json
# Replace with your actual UPS credentials and tracking number
ACCESS_KEY = "YOUR_UPS_ACCESS_KEY"
SHIPPER_NUMBER = "YOUR_UPS_SHIPPER_NUMBER"
TRACKING_NUMBER = "1Z999AA10123456784" # Example tracking number
# UPS Tracking API Endpoint (Sandbox for testing, use production for live)
# Note: This is an example URL structure. Refer to official UPS docs for current endpoints.
TRACKING_URL = f"https://wwwcie.ups.com/track/v1/details/{TRACKING_NUMBER}"
headers = {
"AccessLicenseNumber": ACCESS_KEY,
"ShipperNumber": SHIPPER_NUMBER,
"transactionSrc": "testing", # Optional: for logging/tracking API calls
"Content-Type": "application/json",
"Accept": "application/json"
}
# The request body for tracking, as specified by UPS Tracking API documentation
# This is a simplified example; actual request might need more fields.
request_body = {
"TrackRequest": {
"Request": {
"TransactionReference": {
"CustomerContext": "Tracking Request from My Application"
}
},
"TrackingNumber": TRACKING_NUMBER
}
}
try:
response = requests.get(TRACKING_URL, headers=headers, json=request_body)
response.raise_for_status() # Raises HTTPError for bad responses (4xx or 5xx)
tracking_data = response.json()
print("UPS Tracking Response:")
print(json.dumps(tracking_data, indent=2))
# Example of parsing tracking status
if 'trackResponse' in tracking_data and 'shipment' in tracking_data['trackResponse']:
for shipment in tracking_data['trackResponse']['shipment']:
if 'package' in shipment:
for package in shipment['package']:
if 'activity' in package:
latest_activity = package['activity'][0]
print(f"Latest Status for {TRACKING_NUMBER}: {latest_activity['status']['description']} at {latest_activity['location']['city']}, {latest_activity['location']['stateProvinceCode']}")
elif requests.exceptions.HTTPError as http_err:
print(f"HTTP error occurred: {http_err}")
print(f"Response body: {response.text}")
elif requests.exceptions.ConnectionError as conn_err:
print(f"Connection error occurred: {conn_err}")
elif requests.exceptions.Timeout as timeout_err:
print(f"Timeout error occurred: {timeout_err}")
elif requests.exceptions.RequestException as req_err:
print(f"An unexpected error occurred: {req_err}")
This example demonstrates how to construct a request, set necessary headers, and parse a JSON response. For full details on request and response structures, refer to the official UPS Tracking API documentation.
Community libraries
Due to the widespread use of UPS services, a number of community-contributed libraries have emerged across various programming languages. These libraries often wrap the UPS SOAP or REST APIs, providing a more idiomatic interface for developers. While not officially supported by UPS, they can offer convenience and simplify integration for specific technology stacks.
It is important to note that community libraries may vary in terms of maintenance, completeness, and adherence to the latest UPS API specifications. Developers should evaluate these libraries carefully for their specific project requirements and consider the ongoing support provided by their maintainers.
- PHP: Libraries like
michael-rotterdam/php-ups-api(available on Packagist) aim to provide a structured way to interact with various UPS APIs, including shipping, tracking, and rating, within PHP applications. - Python: Projects such as
python-ups(found on PyPI) offer Pythonic wrappers for UPS services, simplifying tasks like rate calculation and label generation. - Node.js: While less common to find a single comprehensive community SDK, various npm packages exist for specific UPS API interactions, often requiring developers to combine multiple packages or write custom wrappers for broader coverage.
- Ruby: Gems like
ups-ruby(available on RubyGems.org) aim to provide a Ruby interface for UPS services, abstracting the XML complexities of the SOAP APIs.
When using community libraries, developers should consult the library's documentation and source code to understand its capabilities, limitations, and how it handles authentication and error management. It is also advisable to cross-reference with the official UPS API documentation to ensure compatibility and correctness, especially for critical business operations like label generation or customs declarations. For example, understanding how a library handles HTTP headers and authentication tokens is crucial for secure API interactions.
Developers contributing to or using community SDKs often rely on standard API patterns. For instance, the use of JSON:API or similar specifications can influence how data is structured and exchanged, even if the underlying UPS API uses XML for some services. This highlights the importance of understanding both the UPS-specific requirements and general API best practices when selecting or developing an SDK.