Skip to content

Configuration

In an Astro app, run configureSetup once by putting the wagmi + SDK wiring in a small module and importing that module from your root layout (side-effect import). The wagmi config must list every chain you use and define a transport per chain (for example http() for defaults, or http(url) for your own RPC).

src/lib/cooperative.ts

import { configureSetup, createWagmiChainClient } from "cooperative";
import { createConfig, http } from "wagmi";
import { mainnet, polygon, base } from "viem/chains";
const wagmiConfig = createConfig({
chains: [mainnet, polygon, base],
transports: {
[mainnet.id]: http(),
[polygon.id]: http(),
[base.id]: http(),
},
});
configureSetup({
chainClient: createWagmiChainClient(wagmiConfig),
});

For production, point each chain at a provider you control (Alchemy, Infura, and so on) so you avoid public RPC rate limits and get more predictable behavior.

Add environment variables at the project root in .env. Prefix names with PUBLIC_ when the value must be available in code that Astro ships to the client (typical for wagmi and wallet flows):

.env.local
PUBLIC_ETH_RPC_URL=https://eth-mainnet.g.alchemy.com/v2/your_key
PUBLIC_POLYGON_RPC_URL=https://polygon-mainnet.g.alchemy.com/v2/your_key
PUBLIC_BASE_RPC_URL=https://base-mainnet.g.alchemy.com/v2/your_key

src/lib/cooperative.ts

import { configureSetup, createWagmiChainClient } from "cooperative";
import { createConfig, http } from "wagmi";
import { mainnet, polygon, base } from "viem/chains";
const wagmiConfig = createConfig({
chains: [mainnet, polygon, base],
transports: {
[mainnet.id]: http(import.meta.env.PUBLIC_ETH_RPC_URL),
[polygon.id]: http(import.meta.env.PUBLIC_POLYGON_RPC_URL),
[base.id]: http(import.meta.env.PUBLIC_BASE_RPC_URL),
},
});
configureSetup({
chainClient: createWagmiChainClient(wagmiConfig),
});
Chain IDNameNative token
1EthereumETH
137PolygonPOL
8453BaseETH