Skip to content

feat: pluggable paymentHandler hook for x402 integration#16

Merged
acedatacloud-dev merged 3 commits intomainfrom
feat/payment-handler-hook
Apr 19, 2026
Merged

feat: pluggable paymentHandler hook for x402 integration#16
acedatacloud-dev merged 3 commits intomainfrom
feat/payment-handler-hook

Conversation

@acedatacloud-dev
Copy link
Copy Markdown
Member

What

Adds a pluggable paymentHandler (TS) / payment_handler (Python) hook to the SDK transport. When the API returns 402 Payment Required, the transport now:

  1. parses the response body
  2. passes the accepts list to the configured handler
  3. retries the request once with the headers the handler returns (typically X-Payment)

Why

Previously the SDK only supported Bearer-token auth, and @acedatacloud/x402-client had to reinvent its own HTTP client to do x402 flows. That meant two parallel ways of calling the same APIs, and features like task polling / streaming / retries were missing from the x402 path.

With this hook, the SDK stays the single entry point for every AceDataCloud endpoint, and X402Client becomes a small adapter that produces a paymentHandler.

Usage

import { AceDataCloud } from '@acedatacloud/sdk';
import { createX402PaymentHandler } from '@acedatacloud/x402-client';

const client = new AceDataCloud({
  paymentHandler: createX402PaymentHandler({
    network: 'base',
    evmProvider: window.ethereum,
    evmAddress: '0x...',
  }),
});

await client.openai.chat.completions.create({ ... });

Python accepts any callable returning {"headers": {"X-Payment": ...}} (sync or async).

Changes

  • TS: new runtime/payment.ts, Transport accepts paymentHandler, apiToken becomes optional when a handler is supplied
  • Python: new _runtime/payment.py, SyncTransport / AsyncTransport accept payment_handler, async transport awaits coroutines transparently
  • READMEs (root, TS, Python) updated with the x402 integration pattern
  • All existing tests still pass (22 passed, 29 skipped)

Companion PR in X402Client: https://github.com/AceDataCloud/X402Client/pulls (will be opened next).

@acedatacloud-dev acedatacloud-dev merged commit 9c5c6d7 into main Apr 19, 2026
7 checks passed
@acedatacloud-dev acedatacloud-dev deleted the feat/payment-handler-hook branch April 19, 2026 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant