Skip to main content

TypeScript SDK

@4mica/sdk is the official TypeScript SDK for the 4Mica payment network. Use it to deposit collateral, sign payment guarantees, verify and settle them, run the x402 HTTP flow, and gate your own routes behind a payment. The client is made of capability sub-clients:
  • client.deposit — deposit collateral in ETH or ERC20 tokens.
  • client.withdraw — request, cancel, and finalize withdrawals.
  • client.payment — sign, issue, and verify payment guarantees.
  • client.settlement — settle a cleared cycle from either side: the debtor pays, the creditor claims.
  • client.account / client.tokens — the signer’s balances and supported-token metadata.
  • X402 flow helper (X402Flow) — turn a 402 Payment Required response into a signed payment header.
To charge for a route, use the payment middleware in @4mica/x402.

Runtime-neutral

@4mica/sdk runs on Node, Bun, Deno, and edge runtimes. HTTP uses the global fetch, and payment headers are encoded without Buffer, so it runs on the edge.

Install

Node.js 18 or later is required.

Networks

The default network is Base Sepolia. Select another with .network() or the 4MICA_NETWORK environment variable. Read network details from the exported NETWORKS constant:

Your first client

Build a config, create a Client, and always close it when you are done. Start on Base Sepolia (eip155:84532) with a test wallet.
Client.connect reaches the core service for its public parameters, validates that it accepts the payment payload version this SDK signs, and resolves the ClearingHouse, GuaranteeRegistry, and operator signer addresses that payments are signed and receipts verified against. The Ethereum RPC is only contacted lazily, on first on-chain use. From the client you reach the entry points:
  • client.deposit / client.withdraw — payer-side collateral operations.
  • client.payment — signing payments, issuing guarantees, and verifying receipts; client.settlement — settling cleared cycles.
  • X402Flow — the helper for 402-protected HTTP resources.
Keep private keys in environment variables instead of committing them to your code. Use a dedicated test wallet while you develop.

Next steps

Configuration

Initialize a client with ConfigBuilder, environment variables, or a custom signer.

Client operations

Deposit collateral, sign payments, issue guarantees, and settle cleared cycles.

X402 flow

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

Payment middleware

Charge for a route with @4mica/x402’s Express middleware.