Skip to content

feat(x402): optional beforePayment hook to approve or block a payment - #1577

Open
snefhew wants to merge 1 commit into
coinbase:mainfrom
factline-dev:x402-before-payment-hook
Open

snefhew wants to merge 1 commit into
coinbase:mainfrom
factline-dev:x402-before-payment-hook

Conversation

@snefhew

@snefhew snefhew commented Oct 19, 2026

Copy link
Copy Markdown

Closes #1576

Adds an optional beforePayment (TypeScript) / before_payment (Python) field to the x402 action provider config. The provider builds its x402 client internally, so users could not reach the SDK's existing hook; this passes the function to x402Client.onBeforePaymentCreation / x402ClientSync.on_before_payment_creation. Returning { abort: true, reason } (TS) or an AbortResult (Python) blocks the payment before it is signed. Uses: screening payTo against a sanctions list, per-seller spend policies, audit logging.

x402ActionProvider({
  maxPaymentUsdc: 1,
  beforePayment: async ({ selectedRequirements }) => {
    if (blocked(selectedRequirements.payTo)) return { abort: true, reason: "payTo not allowed" };
  },
});
  • No new dependencies; no behaviour change when the field is not set.
  • The TS type comes from the SDK: Parameters<x402Client["onBeforePaymentCreation"]>[0].
  • Tests (TS and Python): the hook is registered when configured and not otherwise.
  • Changeset and changelog fragment included.

Checked locally: jest src/action-providers/x402 (23 passed), tsc --noEmit, eslint, prettier; pytest tests/action_providers/x402 (12 passed), ruff check and format.

🤖 Generated with Claude Code

@snefhew
snefhew requested a review from murrlincoln as a code owner October 19, 2026 17:53
@cb-heimdall

cb-heimdall commented Oct 19, 2026 •

Copy link
Copy Markdown

🟡 Heimdall Review Status

Requirement Status More Info
Reviews 🟡 0/1
Denominator calculation
Show calculation
1 if user is bot 0
1 if user is external 0
2 if repo is sensitive 0
From .codeflow.yml 1
Additional review requirements
Show calculation
Max 0
0
From CODEOWNERS 0
Global minimum 0
Max 1
1
1 if commit is unverified 0
1 if authored by an app that is not allowlisted and lacks verified human attribution 0
Sum 1

@github-actions github-actions Bot added documentation Improvements or additions to documentation action provider New action provider python typescript labels Oct 19, 2026
@snefhew

snefhew commented Oct 30, 2026

Copy link
Copy Markdown
Author

Friendly ping: checks pass and the branch merges cleanly. The hook is optional and off by default, so existing x402 action provider users see no change; it only lets an agent approve or block a payment (for example after a pay guard or spend policy check) before signing. Glad to adjust naming or move it behind a config flag if that fits the action provider better.

@Nikoble1926

Copy link
Copy Markdown

Adding a concrete use case in support of this. We run a payee screening service for x402, and the place such a check has to run is exactly here: after the 402 is read and a payment option is selected, before anything is signed, with payTo and network in hand. Today an AgentKit user cannot do that without forking the provider, because the x402 client is built inside it.

It may also help with triage. #1539, #1544, #1545, #1553, #1585, #1646, #1657 and #1662 all ask for some form of check before an x402 payment (sanctions, spend policy, seller checks). With this hook, each of those becomes a function a user passes in rather than a separate action provider to review, and the x402 provider itself stays vendor neutral.

One small suggestion: if a blocked payment returned its reason to the agent in the same JSON shape as the existing "Payment exceeds limit" error, the model could tell the user why it stopped.

yayabough pushed a commit to yayabough/tolca that referenced this pull request Nov 25, 2026
For frameworks that create their own x402 client and accept a hook
(x402Client.onBeforePaymentCreation, or a beforePayment option such as
the one proposed in coinbase/agentkit#1577). Same checks and options as
withSafePay; the check is paid by the x402Client passed in options.client.
4 new tests (18 total), checked on @x402/fetch 2.30 and 2.32.
@yayabough

Copy link
Copy Markdown

Supporting this. Here is a concrete use for the hook, already published.

I maintain Tolca, a pre-payment seller check for x402 (it screens the payTo wallet against OFAC SDN crypto addresses and Circle's USDC freeze, checks the asset is official USDC, and compares price and payTo with the seller's Bazaar listing; allow / review / block with a signed receipt). tolca-safe-pay 0.2.0 now exports a ready-made hook typed to match this PR's beforePayment option:

import { x402Client } from "@x402/fetch";
import { ExactEvmScheme } from "@x402/evm";
import { tolcaBeforePayment } from "tolca-safe-pay";

const checkClient = new x402Client().register("eip155:8453", new ExactEvmScheme(signer));

x402ActionProvider({
  beforePayment: tolcaBeforePayment({ client: checkClient }),
});

Block stops the payment, review stops it unless an onReview callback approves, and Tolca being unreachable fails closed by default. The check costs $0.01 and the verdict is reused for 24 h per wallet. Code and tests: https://github.com/yayabough/tolca/tree/main/safe-pay

With this hook merged, the separate action provider in #1646 becomes optional: developers could plug the check in with one option instead.

@snefhew
snefhew force-pushed the x402-before-payment-hook branch from 520fc05 to 1e48e05 Compare November 25, 2026 21:55

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action provider New action provider documentation Improvements or additions to documentation python typescript

Development

Successfully merging this pull request may close these issues.

x402 action provider: optional beforePayment hook to approve or block a payment

4 participants