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 areU256values in the asset’s base units.X402Flow: turn the payment requirements from a402 Payment Requiredresponse into a signed payment header.
@4mica/x402 (TypeScript) or 4mica-x402 (Python); see payment middleware.
Install
The crate is published assdk-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.
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 areU256 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.- 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. - 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. - Terminal. Run it.
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:
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.