Skip to content

Add x402 monetization - let AI agents pay per API call#3

Open
sam00101011 wants to merge 34 commits intookx:masterfrom
sam00101011:add-x402-monetization
Open

Add x402 monetization - let AI agents pay per API call#3
sam00101011 wants to merge 34 commits intookx:masterfrom
sam00101011:add-x402-monetization

Conversation

@sam00101011
Copy link

@sam00101011 sam00101011 commented Mar 15, 2026

What is this?

x402 is an open protocol that lets AI agents pay for API calls using the HTTP 402 status code. Instead of API keys and billing dashboards, agents pay per call in USDC on Base.

This PR adds the foundation for monetizing okx/agent-trade-kit's API endpoints. We found 10 endpoints in your README that agents could pay to access.

Why agents would pay for okx/agent-trade-kit

Each of these capabilities becomes a pay-per-call endpoint that AI agents discover and pay for automatically:

  • Execute complex algo orders like OCO, trailing stops, and conditional trades — agents pay per call instead of needing credentials or rate-limited free tiers
  • Access full market data including orderbooks, candles, and funding rates — agents pay per call instead of needing credentials or rate-limited free tiers
  • Manage account balances, positions, and audit logs with granular control — agents pay per call instead of needing credentials or rate-limited free tiers
  • Automate trading strategies using built-in Grid and DCA bot modules — agents pay per call instead of needing credentials or rate-limited free tiers
  • Deploy secure, local-only trading agents via the Model Context Protocol — agents pay per call instead of needing credentials or rate-limited free tiers
  • Implement safety-first trading with read-only modes and module filtering — agents pay per call instead of needing credentials or rate-limited free tiers

With x402, every feature above is instantly monetizable. Agents on Claude, Cursor, and any MCP client can discover and pay for access via 402.bot.

What's included

File Purpose
.well-known/x402.json Declares your endpoints, pricing, and wallet address - agents and routing networks use this for discovery
x402-middleware-example.ts Drop-in middleware that returns 402 and verifies payments

How it works

Agent -> calls your API
     <- 402 Payment Required (price: $0.01, wallet, network)
Agent -> signs USDC payment on Base
     -> retries with x-payment header
     <- normal API response

Settlement is instant. No intermediary holds funds. Revenue goes directly to your wallet.

What you'd earn

~$1500/mo projected (10 endpoints x ~150 agent calls/mo x $0.01)

To activate

  1. Replace YOUR_WALLET_ADDRESS in .well-known/x402.json with your Base wallet
  2. Add the middleware to your server (see x402-middleware-example.ts)
  3. Deploy - your API is now discoverable by every agent on the 402.bot routing network

Your endpoints will automatically appear in Claude, Cursor, and any MCP-compatible agent via the 402.bot MCP server.

Live demo

See how okx/agent-trade-kit works on the x402 network ->

Links


This PR was opened by 402.bot. x402 is an open protocol - you keep 100% of revenue. We just route agents to providers. If this isn't relevant, feel free to close.

numpy0001 and others added 30 commits March 12, 2026 15:56
Unit tests should import from the module under test, not through
the top-level entrypoint re-export. This decouples help.test.ts
from index.ts so the test won't silently break if the re-export
is ever removed.

Closes #40
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
smol-toml was added as an explicit CLI dependency in MR !85 to fix
an undeclared implicit dep. This completes the fix by removing the
direct declaration — TOML functionality is now fully provided through
@agent-tradekit/core which bundles smol-toml internally.

- Remove smol-toml from packages/cli/package.json dependencies
- Remove smol-toml from tsup.config.ts noExternal list
- Update bundle.test.ts noExternal array to match

Closes #39

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ll-notice.js

Add `scripts/postinstall-notice.js` at monorepo root as the single source
of truth. The build step now copies it to each package's `scripts/` dir;
generated copies are gitignored. Resolves #50.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Replace Unix-only `2>/dev/null || true` with cross-platform `|| exit 0`
  so postinstall does not fail on Windows cmd.exe
- Expand `clean` script to also remove generated `scripts/` directory
  in addition to `dist/`, ensuring thorough cleanup of all build outputs

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Change /npm/dm/ to /npm/dt/ in both README.md and README.zh-CN.md
- Update badge labels to "mcp+total+downloads" / "cli+total+downloads"
- Affects 4 badge URLs total; click-through links unchanged

Closes #52
- Move earn.ts → tools/earn/savings.ts
- Move onchain-earn.ts → tools/earn/onchain.ts
- Add tools/earn/index.ts to aggregate exports
- Update all import paths
- Consistent with bot/ sub-module pattern

Closes #64

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
New tool `market_get_stock_tokens` fetches all instruments via
GET /api/v5/public/instruments and filters client-side by
instCategory=3 (stock tokens, e.g. AAPL-USDT-SWAP).

- packages/core: add market_get_stock_tokens ToolSpec
- packages/cli: add cmdMarketStockTokens, wire okx market stock-tokens
- help: add stock-tokens entry to market command list
- docs: update CHANGELOG (EN/ZH), README (EN/ZH) tool count 95→96

Closes #65

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- core/test/tools.test.ts: 9 cases covering endpoint routing,
  default instType=SWAP, instId passthrough, client-side filtering
  by instCategory=3, empty result, and non-array passthrough
- cli/test/index.test.ts: dispatch coverage for handleMarketPublicCommand
  stock-tokens action + cmdMarketStockTokens table/JSON/empty output

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
earn.savings, earn.onchain, and earn.dcd are now included when using
--modules all. Previously required explicit --modules all,earn opt-in.

Closes #66

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Extend spot_place_algo_order ordType enum with "move_order_stop" and add
  callbackRatio, callbackSpread, activePx optional fields to schema and handler
- Extend swap_place_algo_order similarly so it supports trailing stop natively
- Update spot_get_algo_orders parallel queries from 2 to 3 (add move_order_stop)
- Add CLI cmdSpotAlgoTrailPlace and route "okx spot algo trail" subcommand
- Mark swap_place_move_stop_order as [DEPRECATED] (kept for backward compat)
- Update tests: fix spot_get_algo_orders parallel count assertions (2 → 3)
- Add spot-trail CLI unit tests covering callbackRatio, callbackSpread, output
- Update CHANGELOG.md and CHANGELOG.zh-CN.md

Closes #67

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…asing

OKX API expects callBackRatio and callBackSpread (capital B) but we were
sending callbackRatio and callbackSpread (lowercase b), causing sCode 50015
errors on move_order_stop algo orders.

Fixed both swap_place_algo_order and swap_place_move_stop_order handlers.
MCP input schema parameter names remain unchanged (callbackRatio/callbackSpread).

Added unit tests asserting capital-B key names in POST body.

Closes #69

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…criptions

- Replace per-module normalize() with shared normalizeResponse from helpers.ts
- Extract buildAttachAlgoOrds() for TP/SL parameter assembly
- Trim verbose tool schema descriptions

Closes #70

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…helper, trim tool descriptions

- Remove 9 local normalize() copies; use shared normalizeResponse from helpers.ts
- Extract buildAttachAlgoOrds() helper in helpers.ts, replace 5 inline TP/SL blocks
  in spot/swap/futures trade handlers
- Trim rate-limit and endpoint-type labels from tool descriptions (~token reduction)
- Update CHANGELOG.md and CHANGELOG.zh-CN.md

Closes #70

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Add 7 futures core tools: futures_amend_order, futures_close_position,
  futures_set_leverage, futures_get_leverage, futures_batch_orders,
  futures_batch_amend, futures_batch_cancel
- Add 5 futures algo tools (registerFuturesAlgoTools): futures_place_algo_order,
  futures_place_move_stop_order, futures_amend_algo_order,
  futures_cancel_algo_orders, futures_get_algo_orders
- Fix swap_get_algo_orders hardcoded instType: add optional instType param
  (default "SWAP", supports "FUTURES") instead of hardcoding "SWAP"
- Update CLI futures.ts to call futures_* tools directly (no longer re-exporting
  swap functions), giving futures its own dedicated API surface
- Update tests, CHANGELOG, README tool counts (96 → 108, futures 6 → 18)

Closes #71

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…se 2)

- Add attachAlgoOrds (TP/SL) to option_place_order
- Add option_place_algo_order, option_amend_algo_order,
  option_cancel_algo_orders, option_get_algo_orders
- Add CLI commands: option algo place/amend/cancel/orders
- Update docs and CHANGELOG

Closes #72

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Add option algo entries to [Unreleased] in CHANGELOG.md and CHANGELOG.zh-CN.md:
  4 new core tools (option_place_algo_order/amend/cancel/get_algo_orders),
  option_place_order TP/SL attach support, and 4 new CLI subcommands
  (okx option algo place/amend/cancel/orders)

Closes #72
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Add callbackRatio, callbackSpread, activePx to opts type of
  cmdSpotAlgoPlace, cmdSwapAlgoPlace, cmdFuturesAlgoPlace
- Pass the three params through to the tool runner in each function
- Update handleSpotAlgoCommand, handleSwapAlgoCommand,
  handleFuturesAlgoCommand "place" branches in index.ts to forward
  the params from CliValues
- Add unit tests for ordType=move_order_stop scenario across all three
  modules, plus no-regression tests for conditional ordType

Previously, passing --callbackRatio/--callbackSpread/--activePx via
algo place was silently dropped, causing API error 50015 (missing
required param) for trailing stop orders.

Closes #74

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
cmdSwapAlgoCancel was passing { instId, algoId } directly to
swap_cancel_algo_orders, but the handler expects { orders: [{ instId, algoId }] },
causing "orders must be a non-empty array" error at runtime.

Wraps the args in the orders array, consistent with futures/option
cancel commands. Adds unit test to verify correct param format.

Closes #76
jay.fan and others added 4 commits March 15, 2026 09:40
- Add CHANGELOG.md / CHANGELOG.zh-CN.md entries for #76 fix under
  [1.2.4-beta.6] ### Fixed (blocker resolved)
- Refactor muteStdout as a wrapper around captureStdout to eliminate
  duplicated stdout-patching logic in swap-algo-cancel.test.ts (nit resolved)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@fanqi1909
Copy link
Collaborator

Hi @sam00101011 , Thanks for the idea and PR!

x402 is an interesting protocol, but it doesn't fit agent-trade-kit directly — this is an MCP server that runs locally on users' machines with their own OKX API keys, not a public HTTP API.

That said, there's a related effort where x402 could be valuable: the Agent Skills ecosystem.

I'm involved in discussions around a standardized Skills Marketplace (see agentskills/agentskills Discussion #252). One open question is how to create sustainable incentives for skill authors. x402's pay-per-call model could be a great fit here — imagine skill creators earning revenue when agents use their skills.

If you're interested in exploring this direction, I'd suggest joining that discussion. The Skills layer is where monetization makes architectural sense, and x402 could be a strong building block for the ecosystem.

Closing this PR since it doesn't apply to agent-trade-kit, but appreciate you reaching out — and would welcome your input on the Skills side.

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.

4 participants