Skip to main content

Python SDK for UCM — Agent-Native API Marketplace

Project description

UCM Python SDK

Python SDK for the UCM Agent Commerce Protocol.

Installation

pip install ucm-sdk

For development:

pip install ucm-sdk[dev]

Quick Start

API Key mode (simplest)

import asyncio
from ucm import UCMClient

async def main():
    # Register and get an API key
    async with UCMClient('https://registry.ucm.ai', api_key='ucm_key_...') as client:
        # Discover services
        result = await client.discover('I need web search API')
        for svc in result['services']:
            print(f"{svc['name']} - {svc['price']}")

        # Buy a service
        receipt = await client.buy('acme/web-search')
        print(receipt['access_token'])

        # Check balance
        balance = await client.balance()
        print(balance)

asyncio.run(main())

Ed25519 mode (provider operations)

import asyncio
from ucm import UCMClient, generate_keypair

async def main():
    private_key, public_key = generate_keypair()

    async with UCMClient('https://registry.ucm.ai', private_key, public_key) as client:
        # Publish a service (requires Ed25519)
        result = await client.publish({
            'acp_version': '0.1',
            'service_id': 'myorg/my-api',
            'name': 'My API',
            'description': 'Does something useful',
            'provider': {'wallet': public_key, 'name': 'My Org'},
            'base_url': 'https://api.example.com',
            'endpoints': [{'path': '/v1/query', 'method': 'POST'}],
            'tags': ['search'],
            'pricing': {
                'model': 'per-call',
                'price': '0.01',
                'currency': 'USDT',
                'unit': 'call',
            },
            'sla': {'uptime': '99.9%', 'latency_p95': '500ms'},
        })
        print(result['service_id'], result['status'])

asyncio.run(main())

Self-registration flow

import asyncio
from ucm import UCMClient

async def main():
    # Start without any auth — register to get an API key
    async with UCMClient('https://registry.ucm.ai', api_key='placeholder') as client:
        pass  # placeholder needed for constructor

    # Better: use register which auto-sets the api_key
    client = UCMClient('https://registry.ucm.ai', api_key='_')
    reg = await client.register('my-agent', description='A helpful agent')
    # client.api_key is now automatically set
    print(f"Agent ID: {reg['agent_id']}")
    print(f"API Key: {reg['api_key']}")

    # Now use authenticated endpoints
    balance = await client.balance()
    print(balance)
    await client.close()

asyncio.run(main())

Error handling

from ucm import UCMClient, ACPError, WalletRequiredError, BudgetError

async def safe_buy(client: UCMClient, service_id: str):
    try:
        return await client.buy(service_id)
    except WalletRequiredError:
        print("Need to bind a wallet first!")
    except BudgetError as e:
        print(f"Budget issue: {e.message}")
    except ACPError as e:
        print(f"ACP error [{e.code}]: {e.message} (HTTP {e.status_code})")

API Reference

generate_keypair() -> tuple[str, str]

Generate a new Ed25519 keypair. Returns (private_key_hex, public_key_hex).

UCMClient

Async client for interacting with the UCM registry.

Constructor

UCMClient(
    registry_url: str,
    private_key: str | None = None,
    public_key: str | None = None,
    api_key: str | None = None,
    timeout: float = 30.0,
)

At least one of (private_key, public_key) or api_key must be provided.

Public endpoints (no auth)

Method Description
discover(need, tags?, limit?) Search services by natural language
service_info(service_id) Get service details
verify_token(token) Verify an access token
deposit_info() Get Solana escrow information
get_onboarding_md() Fetch agent onboarding guide

Agent management

Method Description
register(name, description?) Self-register, get API key
bind_wallet(wallet, signature) Bind Solana wallet
recover(wallet, timestamp, signature) Recover account via wallet

Authenticated endpoints (Ed25519 or API key)

Method Description
buy(service_id, plan?) Purchase service access
balance() Check budget and usage
history(limit?) Get transaction history

Ed25519-only endpoints (provider/operator)

Method Description
publish(descriptor) List a new service
update_service(service_id, update) Update a service
delete_service(service_id) Delist a service
authorize(agent, rules, operator?) Create budget authorization

Exceptions

All ACP errors are mapped to typed exceptions:

Exception ACP Error Codes
ACPError Base class for all errors
InvalidRequestError INVALID_REQUEST, INVALID_DESCRIPTOR
AuthenticationError INVALID_SIGNATURE, NONCE_REUSED, TIMESTAMP_EXPIRED
BudgetError BUDGET_RULE_VIOLATED, INSUFFICIENT_BUDGET, AUTHORIZATION_EXPIRED, APPROVAL_REQUIRED
NotFoundError SERVICE_NOT_FOUND, AGENT_NOT_FOUND
ConflictError SERVICE_ALREADY_EXISTS, WALLET_ALREADY_BOUND, WALLET_IN_USE
WalletRequiredError WALLET_REQUIRED
RateLimitError RATE_LIMITED

Each exception has .code, .message, .status_code, and .detail attributes.

Authentication

The SDK supports two authentication modes:

  1. Ed25519 signatures — Required for provider operations (publish, authorize, delete). Headers: X-ACP-Pubkey, X-ACP-Timestamp, X-ACP-Nonce, X-ACP-Signature.

  2. API Key (Bearer token) — Simpler auth for agent operations (buy, balance, history). Header: Authorization: Bearer ucm_key_....

When both are configured, Ed25519 is preferred. API key is used as fallback.

License

MIT

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

ucm_sdk-0.2.0.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

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

ucm_sdk-0.2.0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file ucm_sdk-0.2.0.tar.gz.

File metadata

  • Download URL: ucm_sdk-0.2.0.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for ucm_sdk-0.2.0.tar.gz
Algorithm Hash digest
SHA256 950ad6360250752678869ff762dc04d6d438dd3d7b842b5474df64010d34c66a
MD5 764c38df434bc3315d0cb1db7171643f
BLAKE2b-256 a87b1ad5d13deed16798e20d1c828770776837f0bce922efaa72fcf401a3d7ae

See more details on using hashes here.

File details

Details for the file ucm_sdk-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: ucm_sdk-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for ucm_sdk-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 320ae63a73137574df62feca1e788827c0d2958a56258204fd42e2def1c82e9f
MD5 09e30c5cea8f6d09ce815a42837dc2e3
BLAKE2b-256 f4494878990eb4633b0f471c67dfafdc12eb7d830168ec817d5c1b689b70ea11

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