Skip to main content

UCP Client Library for Python

Project description

UCP SDK Client Library

Overview

This directory contains two UCP client implementations:

  1. simple_happy_path_client.py - Script demonstrating a complete "happy path" user journey
  2. ucp_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 httpx for async HTTP operations
  • Error Handling: Custom UCPClientError with 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?) -> CheckoutResponse
  • get_checkout(checkout_id) -> CheckoutResponse
  • update_checkout(checkout_id, request, idempotency_key?) -> CheckoutResponse
  • complete_checkout(checkout_id, payment_data, risk_signals?, ap2?, idempotency_key?) -> CheckoutResponse
  • cancel_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

  1. Install Dependencies: bash uv sync

  2. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ucp_client-0.0.1.tar.gz (16.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ucp_client-0.0.1-py3-none-any.whl (24.9 kB view details)

Uploaded Python 3

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

Hashes for ucp_client-0.0.1.tar.gz
Algorithm Hash digest
SHA256 17ef84e2d61a59033be9b42e22fb32c0e7471546d48d6a5afe035d38ba1bf981
MD5 c5e6c25a1798eef53c9aadf370a96b36
BLAKE2b-256 bde52eaf1702f485f28e805729f4762f5e55c424c2dc27a1b9be9139ad36454a

See more details on using hashes here.

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

Hashes for ucp_client-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6a00372c85de2fb8be2380069d1eb3a7373517a2fce5cb1a716eacd7bbcd5d27
MD5 d318ed5e8c198933dff6e5ca64905ac0
BLAKE2b-256 d25266cf471df9ebe5fe2e6b8cf4535f3c195d17ff66391b179ab975a3b4812e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page