PayPal HTTP Client

1.0.1 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate a basic `HttpClient`, define a GET request, and execute it against a public test API (`httpbin.org`). It highlights the low-level nature of `paypalhttp`, where you define the base URL and manage request details yourself. In a real PayPal integration, `paypalhttp.HttpClient` is typically subclassed to handle authentication and specific PayPal API base URLs.

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}")

view raw JSON →