Skip to main content

Rust SDK

sdk-4mica is the official Rust SDK for the 4Mica payment network. Use it to deposit collateral, sign payments, pay for HTTP 402-protected resources, withdraw, and settle cleared cycles. It has two parts:
  • Client: connect once, then deposit, withdraw, sign payments, and settle through intent builders. Amounts are U256 values in the asset’s base units.
  • X402Flow: turn the payment requirements from a 402 Payment Required response into a signed payment header.
The SDK covers the wallet side of 4Mica. It has no HTTP server middleware. To charge for a route, use @4mica/x402 (TypeScript) or 4mica-x402 (Python); see payment middleware.

Install

The crate is published as sdk-4mica and imported as sdk_4mica. It uses the Rust 2024 edition, so build it with a current stable toolchain.
alloy provides the signer types. Add the same major version the SDK depends on, so your signer implements the traits the SDK expects. The SDK’s async calls run on Tokio.

Your first session

Connect, deposit, and sign a payment. Start on Base Sepolia with a test wallet.
connect() reaches the core service for its public parameters and checks that core accepts the payment payload version this SDK signs. With the network’s hosted facilitator, the deposit is gasless where the token supports it. receipt.route reports which route ran.
Without .network(...), the client connects to Base mainnet and moves real funds. Keep Network::BaseSepolia in your code until you mean to go live.
The testnets accept Aave’s test USDC as collateral. See Get test tokens for how to mint it. Client groups its calls by what you are doing: sdk_4mica::prelude exports the names most integrations need: Client, ClientBuilder, Network, Asset, Address, U256, Credentials, Error, and the receipt and route types.

Amounts and assets

Amounts are U256 values in the asset’s base units: U256::from(5_000_000u64) is 5 USDC. Name the asset with Asset::Erc20(address) or Asset::Native for ETH.

The builder grammar

Every deposit, withdrawal, and settlement call follows the same three steps.
  1. Entry. State what to do: client.deposit.of(asset, amount), client.withdraw.request(asset, amount), client.settlement.pay(cycle_id). An entry does no I/O. It returns a builder.
  2. Route pin (optional). State how to do it: .gasless(), .eip3009(), .permit2(), .permit2().sponsor_approval(), or .self_funded(). Without a pin, the terminal takes the cheapest route available and may fall back to the wallet’s own transaction.
  3. Terminal. Run it.
Each pin changes the builder’s type, so a builder only offers the terminals that exist for its route. Every receipt reports the route that ran in receipt.route, and receipt.route.is_gasless() tells you whether someone else paid the gas.

Networks

4Mica is live on three networks; see supported networks. Select one with .network(...) on the builder or with the 4MICA_NETWORK environment variable. Ethereum mainnet is coming soon. The default is Base mainnet. Selecting a network also selects its hosted facilitator; see Configuration. Network parses from a shorthand or a CAIP-2 id, and reports its own details:
Keep private keys in environment variables instead of committing them to your code. Use a dedicated test wallet while you develop.

Next steps

Configuration

ClientBuilder, environment variables, signers, and authentication.

Client operations

Deposit collateral, withdraw, sign payments, and settle cleared cycles.

X402 flow

Turn a 402 Payment Required response into a signed payment header.

Errors

Match on error variants and handle unknown outcomes safely.