X402

X402 payment is implemented for flow deployments. See example usage in @space-operator/client test: x402_test.ts, explained below:

Configure flow to require payment

First deploy a flow, see Deploy a flow.

  const flowId = 3623;
  const id = await owner.deployFlow(flowId);

Then, add a payment requirement by inserting a row to flow_deployments_x402_fees table. There can be multiple payment requirements and the client is free to choose one of them to use.

Table description:

Name
Format
Type
Description

user_id

uuid

string

Owner user ID

id

bigint

number

ID of the row, auto-generated

deployment_id

uuid

string

Deployment ID

network

public.x402network

string

Available network: "base", "base-sepolia", "solana", "solana-devnet".

pay_to

bigint

number

Wallet ID to receive the payment.

amount

numeric

number

USDC amount.

enabled

boolean

boolean

Can use this column to disable a requirement without completely removing it.

Insert a 0.01 USDC fee on solana-devnet:

  await supabase.from("flow_deployments_x402_fees").insert(
    {
      user_id: user_id!,
      deployment_id: id,
      amount: 0.01,
      enabled: true,
      network: "solana-devnet",
      pay_to: walletId,
    },
  );

Important notes:

  • Because wallets table can only store Solana wallet at the moment, we cannot support payment on Ethereum yet.

  • Mainnet is not implemented yet.

  • Because of a recent exploit, Solana facilitator no longer creates USDC ATA automatically, therefore you need to create it manually, e.g. by depositing a small amount of USDC to it.

Start flow and automatically pay fees with x402-fetch

Use @space-operator/x402-fetch package:

Make a Solana keypair and use wrapFetchWithPayment to automatically pay with it:

Tell the space-operator client to use the wrapped xFetch function:

Call the deployment normally, if there are payment requirements, it will be handled automatically:

Last updated

Was this helpful?