UCP Client Library for Python
Project description
UCP SDK Client Library
Overview
This directory contains two UCP client implementations:
simple_happy_path_client.py- Script demonstrating a complete "happy path" user journeyucp_client.py- Type-safe client library for programmatic UCP API interactions
Type-Safe UCP Client (ucp_client.py)
A fully type-safe Python client for the UCP Shopping API with proper error handling and async support.
Features
- Type Safety: All requests and responses are fully typed using UCP SDK models
- Async Support: Built with
httpxfor async HTTP operations - Error Handling: Custom
UCPClientErrorwith detailed error information - Authentication: Supports API keys and UCP-Agent profiles
- Idempotency: Automatic idempotency key generation
- Context Manager: Proper resource management with async context managers
Quick Start
import asyncio
from ucp_client import UCPClient
from ucp_sdk.models.schemas.shopping import checkout_create_req
async def main():
async with UCPClient(
base_url="http://localhost:8182",
api_key="your-api-key", # Optional
ucp_agent_profile='profile="https://agent.example/profile"'
) as client:
# Get merchant capabilities
profile = await client.get_merchant_profile()
print(f"Supports {len(profile.ucp.capabilities)} capabilities")
# Create checkout
checkout_req = checkout_create_req.CheckoutCreateRequest(...)
checkout = await client.create_checkout(checkout_req)
print(f"Created checkout: {checkout.id}")
asyncio.run(main())
Available Methods
Discovery
get_merchant_profile() -> UcpDiscoveryProfile
Checkout Operations
create_checkout(request, idempotency_key?) -> CheckoutResponseget_checkout(checkout_id) -> CheckoutResponseupdate_checkout(checkout_id, request, idempotency_key?) -> CheckoutResponsecomplete_checkout(checkout_id, payment_data, risk_signals?, ap2?, idempotency_key?) -> CheckoutResponsecancel_checkout(checkout_id, idempotency_key?) -> CheckoutResponse
Order Operations
get_order(order_id) -> Dict[str, Any]update_order(order_id, order_data) -> Dict[str, Any]
Testing (Development Only)
simulate_shipping(order_id) -> Dict[str, Any]
Simple Happy Path Client Script (simple_happy_path_client.py)
This script demonstrates a complete "happy path" user journey: 0. Discovery: Querying the merchant to see what they support. 1. Create Checkout: Creating a new checkout session (cart). 2. Add Items: Adding items to the checkout session. 3. Fulfillment: Triggering fulfillment option generation, selecting a destination, and choosing a shipping option. 4. Complete: Completing the checkout by processing a payment.
Prerequisites
-
Install Dependencies:
bash uv sync -
Start the Merchant Server: You need a running UCP Merchant Server to execute this client against. Follow the instructions in the Server README to start the server on port 8182.
Quick start (from
../../server/):bash uv run server.py --products_db_path=/tmp/ucp_test/products.db --transactions_db_path=/tmp/ucp_test/transactions.db --port=8182
Running the Client
Once the server is running, you can execute the client script:
uv run simple_happy_path_client.py --server_url=http://localhost:8182
Options
-
--server_url: The base URL of the UCP Merchant Server (default:http://localhost:8182). -
--export_requests_to: Path to a markdown file where the request/response dialog will be logged.uv run simple_happy_path_client.py --export_requests_to=interaction_log.md
Automated Demo (extract_json_dialog.sh)
For a fully automated demonstration that sets up the database, starts the server, runs the client, and generates a transaction log, use the included shell script:
./extract_json_dialog.sh
This will output the interaction log to /tmp/ucp_test/happy_path_dialog.md.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ucp_client-0.0.1.tar.gz.
File metadata
- Download URL: ucp_client-0.0.1.tar.gz
- Upload date:
- Size: 16.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
17ef84e2d61a59033be9b42e22fb32c0e7471546d48d6a5afe035d38ba1bf981
|
|
| MD5 |
c5e6c25a1798eef53c9aadf370a96b36
|
|
| BLAKE2b-256 |
bde52eaf1702f485f28e805729f4762f5e55c424c2dc27a1b9be9139ad36454a
|
File details
Details for the file ucp_client-0.0.1-py3-none-any.whl.
File metadata
- Download URL: ucp_client-0.0.1-py3-none-any.whl
- Upload date:
- Size: 24.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a00372c85de2fb8be2380069d1eb3a7373517a2fce5cb1a716eacd7bbcd5d27
|
|
| MD5 |
d318ed5e8c198933dff6e5ca64905ac0
|
|
| BLAKE2b-256 |
d25266cf471df9ebe5fe2e6b8cf4535f3c195d17ff66391b179ab975a3b4812e
|