> ## 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

> Connect a 4Mica session with FourMica.connect options, environment variables, a custom signer, or ConfigBuilder.

# Configuration

`sdk-4mica` needs a signer and uses defaults for everything else. Pass keyword arguments to `FourMica.connect`, or let it read `FOURMICA_*` environment variables.

## `FourMica.connect` options

| Option | Default | Description |
| - | - | - |
| `private_key` | None | Hex private key. Mutually exclusive with `signer`. |
| `signer` | None | An object that follows the `EvmSigner` protocol (CDP, KMS, MPC). See [custom signers](#using-a-custom-signer). |
| `network` | `base` (Base mainnet) | Network shorthand or CAIP-2 id. When the environment is read, this argument wins over `FOURMICA_NETWORK`. |
| `facilitator_url` | The network's hosted facilitator | Facilitator that sponsors gas. Without one, deposits are the wallet's own transactions. |
| `rpc_url` | The network's hosted core | Core API URL of a self-hosted deployment. Replaces `network`. |
| `spend` | `SpendLimits(per_payment="1 USDC")` | Limits on what `client.fetch` pays: `per_payment`, `total`, `pay_to`. See [spend limits](https://docs.4mica.io/sdks/python/x402-flow.md#spend-limits). |
| `env` | `os.environ` | Mapping to read `FOURMICA_*` variables from when neither `private_key` nor `signer` is given. |
| `protocols` | `("x402",)` | Payment protocols `client.fetch` may use, in order of preference. |
| `http_client` | A new `httpx.AsyncClient` | HTTP client for calls to sellers. One you pass stays yours to close. |

```python theme={null}
import os

from fourmica_sdk import FourMica, SpendLimits

async with await FourMica.connect(
    private_key=os.environ["PRIVATE_KEY"],
    network="base-sepolia",
    spend=SpendLimits(per_payment="0.50 USDC", total="20 USDC"),
) as client:
    print(client.address, client.network)
```

`connect` raises a `ConfigError` when no signer is given, when both `private_key` and `signer` are, or when a network, URL, or protocol name is invalid. A malformed `spend` amount raises `InvalidParamsError` here, not on the first payment.

> [!WARNING]
>
> With no `network`, the session connects to Base mainnet and its hosted
> facilitator, so funds are real. Pass `network` explicitly in every program,
> and start on a testnet with a test wallet.

## Using environment variables

With neither `private_key` nor `signer`, `FourMica.connect()` reads its configuration from the environment.

```bash theme={null}
export FOURMICA_WALLET_PRIVATE_KEY="0x..."
export FOURMICA_NETWORK="base-sepolia"   # shorthand or CAIP-2 id

# Optional overrides. Leave them unset to use the network's defaults.
# export FOURMICA_RPC_URL="https://core.example.com/"          # a self-hosted core, instead of FOURMICA_NETWORK
# export FOURMICA_FACILITATOR_URL="https://testnet.payment.4mica.io/"
# export FOURMICA_ETHEREUM_HTTP_RPC_URL="http://localhost:8545"
# export FOURMICA_CONTRACT_ADDRESS="0x..."
# export FOURMICA_BEARER_TOKEN="Bearer <access_token>"
# export FOURMICA_AUTH_URL="https://base.sepolia.api.4mica.io/"
# export FOURMICA_AUTH_REFRESH_MARGIN_SECS="60"
```

```python theme={null}
from fourmica_sdk import FourMica

async with await FourMica.connect() as client:
    print(client.address, client.network)
```

Arguments you pass still win: `FourMica.connect(network="ethereum-sepolia")` overrides `FOURMICA_NETWORK`. `FOURMICA_NETWORK` takes precedence over `FOURMICA_RPC_URL`. With neither variable set and no `network` argument, the session connects to Base mainnet. A variable set to the empty string counts as unset.

When you pass `private_key` or `signer`, the environment is not read at all. `FOURMICA_NETWORK` is ignored in that case, so pass `network` yourself.

> [!NOTE]
>
> The older `4MICA_*` names still work. Setting both names of one variable to
> different values is a `ConfigError`. Prefer `FOURMICA_*`: a shell cannot
> `export` a name that starts with a digit.

To read from somewhere other than the process environment, pass a mapping: `FourMica.connect(env={"FOURMICA_WALLET_PRIVATE_KEY": key, "FOURMICA_NETWORK": "base-sepolia"})`.

## Using a custom signer

To integrate a remote signer, KMS, or MPC wallet, pass an object that follows the `EvmSigner` protocol as `signer`. It needs an `address` property and three coroutines:

```python theme={null}
from typing import Any


class KmsSigner:
    @property
    def address(self) -> str: ...

    async def sign_typed_data(self, full_message: dict[str, Any]) -> str | bytes: ...

    async def sign_message(self, message: str | bytes) -> str | bytes: ...

    async def sign_hash(self, message_hash: bytes) -> str | bytes: ...
```

The SDK signs payments, checkpoints, and gasless authorizations as raw 32-byte digests through `sign_hash`, and the SIWE login message through `sign_message`. Return each signature in the 65-byte `r || s || v` form, as hex or bytes.

A remote signer signs payments and gasless deposits and withdrawals. It cannot send a transaction or read the wallet, so self-funded routes raise a `ClientError`, `deposit` skips its wallet balance check, and `withdrawals()` needs a local key.

### Coinbase CDP wallet

Use `create_cdp_account` for a Coinbase CDP MPC wallet whose private key never leaves CDP. It needs the `cdp` extra: `pip install "sdk-4mica[cdp]"`.

```python theme={null}
import os

from fourmica_sdk import FourMica
from fourmica_sdk.wallet import CdpAccountConfig, create_cdp_account

config = CdpAccountConfig(
    api_key_id=os.environ["CDP_API_KEY_ID"],
    api_key_secret=os.environ["CDP_API_KEY_SECRET"],
    wallet_secret=os.environ["CDP_WALLET_SECRET"],
    name="my-agent-wallet",  # idempotent: the same name returns the same wallet
)

async with await create_cdp_account(config) as signer:
    async with await FourMica.connect(signer=signer, network="base-sepolia") as client:
        print(client.address)
```

The session does not close a signer you pass. The outer `async with` closes the CDP client.

## Advanced: exact control

`FourMica` is a facade over `Client`. For anything it does not cover, `await client.low_level()` returns the session's `Client`, already configured. To build a `Client` yourself, build a `Config` with `ConfigBuilder` and pass it to `Client.connect`.

| Method | Required | Description |
| - | - | - |
| `wallet_private_key(value)` | Yes, unless `evm_signer` is set | Private key used for signing and for the wallet's own transactions. |
| `evm_signer(signer)` | Yes, unless `wallet_private_key` is | An `EvmSigner`, as described above. |
| `network(value)` | No | Network shorthand or CAIP-2 id. Defaults to `base`. Also selects the network's hosted facilitator. |
| `rpc_url(value)` | No | Core API URL of a self-hosted deployment. Clears the network's facilitator. |
| `ethereum_http_rpc_url(value)` | No | Ethereum JSON-RPC endpoint. Fetched from core when omitted. |
| `contract_address(value)` | No | Treasury contract address. Fetched from core when omitted. |
| `facilitator_url(value)` | No | Facilitator that sponsors gas. Wins over the network's own. Without one, every operation is self-funded. |
| `bearer_token(value)` | No | Static bearer token for auth. Replaces SIWE auth. |
| `auth_url(value)` | No | SIWE auth endpoint. Defaults to the core API URL. |
| `auth_refresh_margin_secs(value)` | No | Seconds before expiry at which the session refreshes. Defaults to 60. |
| `from_env(env=None)` | No | Reads the `FOURMICA_*` variables above from `env`, or `os.environ`. |

> [!NOTE]
>
> `ethereum_http_rpc_url` and `contract_address` are fetched from the core
> service by default. The SDK validates the connected chain id but does not
> verify the contract address or code. Only override these if you need values
> that differ from the server defaults.

```python theme={null}
import os

from fourmica_sdk import Client, ConfigBuilder

cfg = (
    ConfigBuilder()
    .network("base-sepolia")
    .wallet_private_key(os.environ["PRIVATE_KEY"])
    .build()
)  # or ConfigBuilder().from_env().build() to read FOURMICA_*

async with await Client.connect(cfg) as client:
    await client.login()  # optional: pre-warm the SIWE session
    print("signing as", client.signer_address)
```

A `ConfigBuilder` with no `network` or `rpc_url` call builds a Base mainnet configuration. `network()` raises a `ConfigError` for a name it does not know. It accepts `ethereum` (`eip155:1`), which is [coming soon](https://docs.4mica.io/getting-started/supported-networks.md) and has no hosted facilitator yet, so a known name is not proof that a network is live. `build()` raises one when a signer or wallet key is missing, a URL, key, or address is malformed, or the auth refresh margin is not a non-negative integer.

An Ethereum RPC endpoint is only used by paths that read chain state or send transactions. A client that only signs never touches the chain.

### SIWE authentication

Auth is enabled by default, and the first authenticated call to core logs in automatically. Connecting never triggers a login, because the bootstrap routes are public. Use a static token instead of SIWE, or turn credentials off entirely:

```python theme={null}
cfg = (
    ConfigBuilder()
    .network("base-sepolia")
    .wallet_private_key("0x...")
    .bearer_token("Bearer <access_token>")
    .build()
)

anonymous = (
    ConfigBuilder()
    .network("base-sepolia")
    .wallet_private_key("0x...")
    .disable_auth()
    .build()
)
```

`client.login()` raises a `ClientError` when auth is not configured, which is the case with a bearer token or after `disable_auth()`.

> [!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/python/overview.md)
>
> Install the SDK and see the capabilities at a glance.

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


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