PayPal HTTP Client
paypalhttp is a lightweight, low-level HTTP client library developed by PayPal, designed to wrap API calls to REST APIs. It provides basic HTTP request and response serialization capabilities (JSON, multipart, form-encoded, text). The current stable version is 1.0.1, and it maintains a stable release cadence as a foundational utility for higher-level PayPal SDKs.
Warnings
- gotcha paypalhttp is a low-level HTTP client, not a full PayPal SDK. It provides the HTTP transport layer but does not include high-level API wrappers, specific PayPal API models, or built-in authentication for PayPal's services. Users often expect a higher-level SDK when interacting with 'PayPal' libraries.
- gotcha Authentication and full API URL construction are external concerns. `paypalhttp.HttpClient` requires a base URL during initialization and expects request objects to provide relative paths. Authentication (e.g., OAuth tokens for PayPal APIs) must be injected into requests or handled by a subclassing client, as shown in official PayPal SDKs.
- gotcha Error handling for API responses requires explicit checks. While `paypalhttp` will raise exceptions for network or client-side issues, HTTP errors returned by the API (e.g., 400 Bad Request, 404 Not Found, 500 Internal Server Error) are represented in the `response.status_code` and `response.result` attributes, not as exceptions. You must inspect these values.
Install
-
pip install paypalhttp
Imports
- HttpClient
from paypalhttp.http_client import HttpClient
- GetRequest
from paypalhttp.requests import GetRequest
- JsonSerializer
from paypalhttp.serializers import JsonSerializer
Quickstart
import os
from paypalhttp.http_client import HttpClient
from paypalhttp.requests import GetRequest
from paypalhttp.serializers import JsonSerializer
# Define a custom client that uses a base URL
class MyGenericApiClient(HttpClient):
def __init__(self):
# paypalhttp is a low-level client; it doesn't handle authentication
# or full API paths directly. It expects a base URL and relative paths.
# In real PayPal SDKs, this would be 'https://api.sandbox.paypal.com'
# or 'https://api.paypal.com'. Using httpbin.org for a runnable example.
super().__init__('https://httpbin.org')
# Instantiate the custom client
client = MyGenericApiClient()
# Create a GET request to a test endpoint
request = GetRequest('/get') # This will hit https://httpbin.org/get
# You can add headers, parameters, or a body (with a serializer)
request.headers['User-Agent'] = 'PayPalHttp-Client-Example'
print(f"Making request to: {client.environment_url}{request.path}")
try:
# Execute the request
response = client.execute(request)
print(f"\nStatus Code: {response.status_code}")
print(f"Headers: {response.headers}")
# The 'result' attribute contains the deserialized response body
# For httpbin.org/get, this is typically a JSON object.
print(f"Result (JSON): {response.result}")
except Exception as e:
print(f"\nError executing request: {e}")