> ## Documentation Index
> Fetch the complete documentation index at: https://docs.4mica.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Configuration

> Configure a 4Mica Rust client with ClientBuilder, environment variables, a custom signer, and authentication.

# Configuration

`sdk-4mica` needs a signer and uses defaults for everything else. Set options on `ClientBuilder`, or let `ClientBuilder::from_env` read `4MICA_*` environment variables.

## `ClientBuilder`

`Client::builder()` returns a `ClientBuilder`. Chain the setters you need, then call `connect()`.

| Method | Default | Description |
| - | - | - |
| `signer(signer)` | None, required | The wallet that signs. Any alloy `Signer`; see [Signers](#signers). |
| `network(network)` | `Network::Base` | A hosted network. Sets the core API URL and the network's hosted facilitator. |
| `rpc_url(url)` | The network's core API URL | Core API URL of a self-hosted deployment. Replaces `network` and its hosted facilitator. |
| `facilitator_url(url)` | The network's hosted facilitator | Facilitator that sponsors gas. An explicit URL always wins over a network's. |
| `ethereum_http_rpc_url(url)` | Fetched from core | Ethereum JSON-RPC endpoint for chain reads and the wallet's own transactions. |
| `contract_address(address)` | Fetched from core | Address of the Treasury contract, which holds collateral. |
| `credentials(credentials)` | `Credentials::Siwe` | How API calls authenticate; see [Authentication](#authentication). |
| `bearer_token(token)` | None | Shorthand for `credentials(Credentials::Bearer(token))`. A blank token is ignored. |
| `auth_url(url)` | The core API URL | Auth base URL for SIWE credentials. |
| `auth_refresh_margin_secs(secs)` | `60` | Refresh the access token when less than this many seconds remain. SIWE only. |

```rust theme={null}
use alloy::signers::local::PrivateKeySigner;
use sdk_4mica::{Client, Network};

let signer: PrivateKeySigner = std::env::var("PRIVATE_KEY")?.parse()?;

let client = Client::builder()
    .network(Network::BaseSepolia)
    .signer(signer)
    .connect()
    .await?;
```

`connect()` builds the configuration and then reaches core for its public parameters, which is why it is async and fallible. Its error is the umbrella `sdk_4mica::Error`, wrapping one of:

* `ConfigError::Missing` when no signer is set.
* `ConfigError::InvalidValue` when a URL or address does not parse.
* `ClientError::Rpc` when core cannot be reached.
* `ClientError::Initialization` when core does not accept the payment payload version this SDK signs.

To separate the two steps, call `build()` for a `Config` and pass it to `Client::connect(config)` later.

> [!NOTE]
>
> You normally do not set `ethereum_http_rpc_url` or `contract_address`. Core
> publishes both. The SDK connects to the Ethereum endpoint on first use and
> checks its chain id against core's. A client that only takes gasless routes
> never opens that connection.

## Networks and the facilitator

The builder starts on Base mainnet with Base's hosted facilitator. `network(...)` moves both together.

| Call | Core API | Facilitator |
| - | - | - |
| No `network` and no `rpc_url` | Base mainnet | `https://payment.4mica.io/` |
| `network(Network::BaseSepolia)` | Base Sepolia | `https://testnet.payment.4mica.io/` |
| `network(Network::EthereumSepolia)` | Ethereum Sepolia | `https://testnet.payment.4mica.io/` |
| `rpc_url(url)` | `url` | None, unless you call `facilitator_url` |

> [!WARNING]
>
> A builder with no network connects to Base mainnet and moves real funds.
> Select the network explicitly in any code that moves money, and start on Base
> Sepolia with a test wallet.

`network` and `rpc_url` replace each other, so the last one you call wins. `facilitator_url` is independent of call order.

Without a facilitator, every operation is the wallet's own transaction and the wallet pays gas. To skip the facilitator for one operation, pin `.self_funded()` on its builder; see [gasless routes](https://docs.4mica.io/sdks/rust/client-operations.md#gasless-routes).

See the [network table](https://docs.4mica.io/sdks/rust/overview.md#networks) for shorthands, CAIP-2 ids, and URLs.

## Using environment variables

`ClientBuilder::from_env()` reads these variables and returns a builder.

| Variable | Description |
| - | - |
| `4MICA_WALLET_PRIVATE_KEY` | Hex private key. Required unless you call `signer(...)` on the returned builder. |
| `4MICA_NETWORK` | Network shorthand or CAIP-2 id. Takes precedence over `4MICA_RPC_URL`. |
| `4MICA_RPC_URL` | Core API URL of a self-hosted deployment. |
| `4MICA_FACILITATOR_URL` | Facilitator that sponsors gas. |
| `4MICA_ETHEREUM_HTTP_RPC_URL` | Ethereum JSON-RPC endpoint. |
| `4MICA_CONTRACT_ADDRESS` | Treasury contract address. |
| `4MICA_BEARER_TOKEN` | Access token. Selects bearer credentials in place of SIWE. |
| `4MICA_AUTH_URL` | Auth base URL for SIWE credentials. |
| `4MICA_AUTH_REFRESH_MARGIN_SECS` | Token refresh margin in seconds. |

With neither `4MICA_NETWORK` nor `4MICA_RPC_URL` set, the builder stays on Base mainnet.

A shell cannot `export` a name that starts with a digit. Put the variables in a `.env` file and load it in your program, for example with the `dotenvy` crate:

```ini theme={null}
4MICA_WALLET_PRIVATE_KEY=0x...
4MICA_NETWORK=base-sepolia
```

```rust theme={null}
use sdk_4mica::{ClientBuilder, Network};

dotenvy::dotenv().ok();

let client = ClientBuilder::from_env()?
    .network(Network::BaseSepolia)
    .connect()
    .await?;
```

Setters you call on the returned builder win over the environment: here `network` overrides `4MICA_NETWORK`. `from_env()` returns `ConfigError::InvalidValue` for an unknown network, a malformed private key, or a refresh margin that does not parse as an unsigned integer.

> [!NOTE]
>
> The Rust SDK reads the `4MICA_*` names only. It does not read the
> `FOURMICA_*` names the TypeScript and Python SDKs accept.

## Signers

`Client<S>` is generic over its signer. `S` can be any type that implements alloy's `Signer` trait: a local key, a hardware wallet, or a remote key service. The default is `PrivateKeySigner`.

```rust theme={null}
use alloy::signers::local::PrivateKeySigner;

let signer: PrivateKeySigner = std::env::var("PRIVATE_KEY")?.parse()?;
```

What the signer must implement depends on the route you take:

| Call | Signer bounds |
| - | - |
| `connect()` | `Signer + Sync + Clone` |
| Signing a payment, and every gasless pin (`.gasless()`, `.eip3009()`, `.permit2()`) | `Signer + Send + Sync` |
| Unpinned `.send()`, `.self_funded()` terminals, and `client.tokens.approve` | `Signer + TxSigner<Signature> + Send + Sync + Clone + 'static` |

A signing-only backend can therefore run every gasless flow. It needs transaction signing only for routes where the wallet sends its own transaction. An unpinned `.send()` counts as one of those, because it may fall back to a self-funded transaction.

`client.signer_address()` returns the address the client signs as. Every deposit credits that account.

## Authentication

Core's authenticated endpoints take a bearer access token. `Credentials` decides where it comes from.

| Variant | Behavior |
| - | - |
| `Credentials::Siwe` | The default. Signs in with the configured signer (Sign-In with Ethereum) and refreshes tokens as they near expiry. |
| `Credentials::Bearer(token)` | Sends a token you already hold. The SDK never signs in. |
| `Credentials::None` | Sends no credentials. Only public endpoints work. |

Connecting never triggers a sign-in, because the public parameters are public. With SIWE, the first authenticated call signs in for you. Call `client.login()` to sign in ahead of time:

```rust theme={null}
let tokens = client.login().await?;
println!("access token expires in {}s", tokens.expires_in);
```

`login()` returns `AuthError::MissingConfig` when the credentials are not SIWE.

Use a static token, or turn credentials off:

```rust theme={null}
use sdk_4mica::{Client, Credentials, Network};

let client = Client::builder()
    .network(Network::BaseSepolia)
    .signer(signer.clone())
    .bearer_token(std::env::var("ACCESS_TOKEN")?)
    .connect()
    .await?;

let anonymous = Client::builder()
    .network(Network::BaseSepolia)
    .signer(signer)
    .credentials(Credentials::None)
    .connect()
    .await?;
```

Pass the bare access token. The SDK adds the `Bearer ` prefix to the `Authorization` header itself.

> [!WARNING]
>
> Never commit private keys. Use a test wallet while you develop and a dedicated
> service wallet, hardware-backed signer, MPC wallet, or hosted key management
> system in production.

> [!TIP]
>
> Start on Base Sepolia (`eip155:84532`) with small amounts. Once a flow works
> end to end, move shared values into environment variables and switch networks.

## Next steps

> [card] [Overview](https://docs.4mica.io/sdks/rust/overview.md)
>
> Install the SDK and see the capabilities at a glance.

> [card] [Client operations](https://docs.4mica.io/sdks/rust/client-operations.md)
>
> Deposit collateral, withdraw, sign payments, and settle cleared cycles.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.