## RPC Cheatcodes

### Signature

```solidity
function rpcUrl(string calldata rpcAlias) external returns (string memory url);
function rpcUrls() external returns (string[2][] memory urls);
function rpc(string calldata method, string calldata params) external returns (bytes memory data);
```

### Description

Provides cheatcodes to access all RPC endpoints configured in the `rpc_endpoints` object of `foundry.toml`, and the ability to make JSON-RPC calls using the configured fork URL.

### Functions

#### `rpcUrl`

Returns the URL for a configured RPC alias.

| Parameter  | Type     | Description                    |
|------------|----------|--------------------------------|
| `rpcAlias` | `string` | The alias name from config     |

| Returns  | Description              |
|----------|--------------------------|
| `string` | The configured RPC URL   |

#### `rpcUrls`

Returns all configured (alias, URL) pairs.

| Returns        | Description                          |
|----------------|--------------------------------------|
| `string[2][]`  | Array of \[alias, url] pairs          |

#### `rpc`

Performs an Ethereum JSON-RPC request to the current fork URL.

| Parameter | Type     | Description                           |
|-----------|----------|---------------------------------------|
| `method`  | `string` | The JSON-RPC method name              |
| `params`  | `string` | JSON-encoded array of parameters      |

| Returns | Description                    |
|---------|--------------------------------|
| `bytes` | The RPC response data          |

### Configuration

Configure RPC endpoints in your `foundry.toml`:

```toml [foundry.toml]
[rpc_endpoints]
optimism = "https://optimism.alchemyapi.io/v2/..."
mainnet = "${RPC_MAINNET}"
```

:::note
Environment variables must be wrapped in `${}` syntax.
:::

### Examples

#### Get a configured RPC URL

```solidity [test/Rpc.t.sol]
string memory url = vm.rpcUrl("optimism");
assertEq(url, "https://optimism.alchemyapi.io/v2/...");
```

#### Handle missing environment variables

```solidity [test/Rpc.t.sol]
vm.expectRevert("Failed to resolve env var `${RPC_MAINNET}` in `RPC_MAINNET`: environment variable not found");
string memory url = vm.rpcUrl("mainnet");
```

#### Get all configured RPC URLs

```solidity [test/Rpc.t.sol]
string[2][] memory allUrls = vm.rpcUrls();
assertEq(allUrls.length, 2);

string[2] memory val = allUrls[0];
assertEq(val[0], "optimism");

string[2] memory env = allUrls[1];
assertEq(env[0], "mainnet");
```

#### Make an RPC call

```solidity [test/Rpc.t.sol]
// Get balance at block 18332681
bytes memory result = vm.rpc(
    "eth_getBalance",
    "[\"0x8D97689C9818892B700e27F316cc3E41e17fBeb9\", \"0x117BC09\"]"
);
assertEq(hex"10b7c11bcb51e6", result);
```

### Gotchas

:::warning
If an RPC alias references an environment variable that is not set, `rpcUrl()` will revert with an error message.
:::

### Related Cheatcodes

* [`createFork`](/reference/cheatcodes/create-fork) - Creates a new fork
* [`createSelectFork`](/reference/cheatcodes/create-select-fork) - Creates and selects a fork

### See Also

* [RPC Endpoints Configuration](/config/reference/testing#rpc_endpoints)
