> **Can't find what you're looking for?** Use `search_docs` on the docs MCP server at `https://v3.viem.sh/api/mcp` to find what you need.

# Prepare Transaction \[transaction.prepare]

Prepares a transaction request for signing, by populating the fields required to be signed over (for example, `nonce`, `chainId`, `type`, fees, `gas`).

## Usage

This example prepares a transaction request for signing, by populating the fields required to be signed over (e.g.

:::code-group
```ts twoslash [example.ts]
import { client } from './viem.config'

const { request } = await client.transaction.prepare({
  account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: 1n,
})
// @log: {
// @log:   account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
// @log:   chainId: 1,
// @log:   from: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
// @log:   gas: 21000n,
// @log:   maxFeePerGas: 16026200000n,
// @log:   maxPriorityFeePerGas: 1000000000n,
// @log:   nonce: 0,
// @log:   to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
// @log:   type: 'eip1559',
// @log:   value: 1n,
// @log: }
```

```ts twoslash [viem.config.ts] filename="viem.config.ts"
import { Client, http, publicActions } from 'viem'
import { mainnet } from 'viem/chains'

export const client = Client.create({
  chain: mainnet,
  transport: http(),
}).extend(publicActions())
```
:::

### Standalone Action

Call `Actions.transaction.prepare` directly by passing the Client as the first argument.

:::code-group
```ts twoslash [example.ts]
import { Actions } from 'viem'
import { client } from './viem.config'

const { request } = await Actions.transaction.prepare(client, {
  account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: 1n,
})
// @log: {
// @log:   account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
// @log:   chainId: 1,
// @log:   from: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
// @log:   gas: 21000n,
// @log:   maxFeePerGas: 16026200000n,
// @log:   maxPriorityFeePerGas: 1000000000n,
// @log:   nonce: 0,
// @log:   to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
// @log:   type: 'eip1559',
// @log:   value: 1n,
// @log: }
```

```ts twoslash [viem.config.ts] filename="viem.config.ts"
import { Client, http } from 'viem'
import { mainnet } from 'viem/chains'

export const client = Client.create({
  chain: mainnet,
  transport: http(),
})
```
:::

## Recipes

### Prepare Selected Parameters

Pass `parameters` when you only need specific missing fields.

```ts twoslash
import { Client, http, publicActions } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({
  chain: mainnet,
  transport: http(),
}).extend(publicActions())

const { request } = await client.transaction.prepare({
  account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
  parameters: ['fees', 'nonce'], // [!code focus]
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: 1n,
})
```

### Prepare Blob Sidecars

Include `sidecars` in `parameters` to derive blob sidecars with a [KZG context](/docs/utilities/kzg/from).

```ts twoslash
import { Client, http, publicActions } from 'viem'
import { mainnet } from 'viem/chains'
import { Kzg } from 'viem/utils'

declare const kzg: Kzg.Kzg

const client = Client.create({
  chain: mainnet,
  transport: http(),
}).extend(publicActions())

const { request } = await client.transaction.prepare({
  blobs: ['0x1234'], // [!code focus]
  kzg, // [!code focus]
  parameters: ['blobVersionedHashes', 'sidecars'], // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

## Return Value

`{ capabilities?: object; request: TransactionRequest }`

The prepared transaction `request` (with the populated fields), and any node `capabilities` returned alongside it.

## Parameters

### accessList

* **Type:** `AccessList`

The EIP-2930 access list to attach to the transaction.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const { request } = await Actions.transaction.prepare(client, {
  accessList: [ // [!code focus]
    { // [!code focus]
      address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2', // [!code focus]
      storageKeys: [ // [!code focus]
        '0x0000000000000000000000000000000000000000000000000000000000000001', // [!code focus]
      ], // [!code focus]
    }, // [!code focus]
  ], // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### account

* **Type:** `Account | Address`
* **Default:** `client.account`

The Account or address that sends the transaction (`from`). Defaults to the Client's
[`account`](/docs/clients/create#optionsaccount).

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const { request } = await Actions.transaction.prepare(client, {
  account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', // [!code focus]
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: 1n,
})
```

### authorizationList

* **Type:** `AuthorizationList`

The EIP-7702 (signed) authorization list to attach to the transaction.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const { request } = await Actions.transaction.prepare(client, {
  account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
  authorizationList: [ // [!code focus]
    { // [!code focus]
      address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2', // [!code focus]
      chainId: 1, // [!code focus]
      nonce: 0n, // [!code focus]
      r: '0x...', // [!code focus]
      s: '0x...', // [!code focus]
      yParity: 0, // [!code focus]
    }, // [!code focus]
  ], // [!code focus]
})
```

### blobs

* **Type:** `readonly Hex[]`

The blobs to attach to the transaction (EIP-4844). When supplied together with [`kzg`](#kzg), `blobVersionedHashes` (and optionally `sidecars`) are derived.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'
import { Kzg } from 'viem/utils'

const blobData = '0x1234' as const
const kzg = {} as Kzg.Kzg
const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const { request } = await Actions.transaction.prepare(client, {
  blobs: [blobData], // [!code focus]
  kzg, // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### blobVersionedHashes

* **Type:** `readonly Hex[]`

The versioned hashes of the EIP-4844 blobs in the transaction request.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const { request } = await Actions.transaction.prepare(client, {
  blobVersionedHashes: ['0x...'], // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### chain

* **Type:** `Chain`
* **Default:** `client.chain`

The chain the transaction targets. Used to derive the chain ID, apply chain-specific fee multipliers, and run any `chain.transaction.prepare` hooks.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet, optimism } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const { request } = await Actions.transaction.prepare(client, {
  account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
  chain: optimism, // [!code focus]
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: 1n,
})
```

### chainId

* **Type:** `number`

The chain ID of the transaction. When provided, it is preferred over the node-derived value.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const { request } = await Actions.transaction.prepare(client, {
  chainId: 1, // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### data

* **Type:** `Hex`

The calldata to send with the transaction.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const { request } = await Actions.transaction.prepare(client, {
  data: '0x06fdde03', // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### from

* **Type:** `Address`

The raw sender address. Viem uses this field only when neither [`account`](#account) nor a Client Account is available.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const { request } = await Actions.transaction.prepare(client, {
  from: '0xd2135CfB216b74109775236E36d4b433F1DF507B', // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### gas

* **Type:** `number | bigint | Hex`

The gas limit for the transaction. When provided, it is preferred over the node-derived value.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const { request } = await Actions.transaction.prepare(client, {
  gas: 21_000n, // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### gasPrice

* **Type:** `number | bigint | Hex`

The legacy gas price (in wei). Mutually exclusive with `maxFeePerGas`/`maxPriorityFeePerGas`.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const { request } = await Actions.transaction.prepare(client, {
  gasPrice: 20_000_000_000n, // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### kzg

* **Type:** `Kzg`

The KZG context, used to derive blob fields (`blobVersionedHashes`, `sidecars`) from [`blobs`](#blobs).

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'
import { Kzg } from 'viem/utils'

const blobData = '0x1234' as const
const kzg = {} as Kzg.Kzg
const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const { request } = await Actions.transaction.prepare(client, {
  blobs: [blobData],
  kzg, // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### maxFeePerBlobGas

* **Type:** `number | bigint | Hex`

The max fee per blob gas (in wei) the sender is willing to pay (EIP-4844).

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const { request } = await Actions.transaction.prepare(client, {
  maxFeePerBlobGas: 1_000_000_000n, // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### maxFeePerGas

* **Type:** `number | bigint | Hex`

The total fee per gas (in wei) the sender is willing to pay (EIP-1559). When provided, it is preferred over the node-derived value.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const { request } = await Actions.transaction.prepare(client, {
  maxFeePerGas: 20_000_000_000n, // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### maxPriorityFeePerGas

* **Type:** `number | bigint | Hex`

The max priority fee per gas (in wei) to pay to the block producer (EIP-1559).

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const { request } = await Actions.transaction.prepare(client, {
  maxPriorityFeePerGas: 1_000_000_000n, // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### nonce

* **Type:** `number | bigint | Hex`

The nonce to use for the transaction. When provided, it takes precedence over the [`nonceManager`](#noncemanager).

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const { request } = await Actions.transaction.prepare(client, {
  nonce: 69, // [!code focus]
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```

### nonceManager

* **Type:** `NonceManager`

A nonce manager used to derive the transaction nonce when [`nonce`](#nonce) is not provided.

```ts twoslash
import { Actions, Client, http, NonceManager } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const { request } = await Actions.transaction.prepare(client, {
  account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
  nonceManager: NonceManager.jsonRpc(), // [!code focus]
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: 1n,
})
```

### parameters

* **Type:** `readonly ('blobVersionedHashes' | 'chainId' | 'fees' | 'gas' | 'nonce' | 'sidecars' | 'type')[]`
* **Default:** `['blobVersionedHashes', 'chainId', 'fees', 'gas', 'nonce', 'type']`

The fields to populate. Narrow this to prepare only a subset (for example, fetch only the `nonce`).

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const { request } = await Actions.transaction.prepare(client, {
  account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
  parameters: ['nonce'], // [!code focus]
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: 1n,
})
```

### to

* **Type:** `Address | null`

The transaction recipient. Pass `null` for a contract creation request.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const { request } = await Actions.transaction.prepare(client, {
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', // [!code focus]
  value: 1n,
})
```

### type

* **Type:** `string`

Forces the transaction request to a specific type (for example, `'eip1559'`, `'eip2930'`, `'legacy'`).

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const { request } = await Actions.transaction.prepare(client, {
  to: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  type: 'eip1559', // [!code focus]
})
```

### value

* **Type:** `number | bigint | Hex`

The value (in wei) sent with the transaction.

```ts twoslash
import { Actions, Client, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const { request } = await Actions.transaction.prepare(client, {
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: 1000000000000000000n, // [!code focus]
})
```

## Errors

| Error | Description |
| --- | --- |
| `Address.InvalidAddressError` | The Account address is invalid. |
| `Actions.block.Errors.BlockNotFoundError` | The block used to prepare the transaction could not be found. |
| `Actions.fee.Errors.BaseFeeScalarError` | The chain's `baseFeeMultiplier` resolved to a value less than `1`. |
| `Actions.fee.Errors.Eip1559FeesNotSupportedError` | `maxFeePerGas`/`maxPriorityFeePerGas` were supplied for a `legacy`/`eip2930` transaction. |
| `Actions.transaction.Errors.MaxFeePerGasTooLowError` | The supplied `maxFeePerGas` is lower than the estimated `maxPriorityFeePerGas`. |
| `BlockParameter.RequireCanonicalError` | `requireCanonical` was provided without a `blockHash` in an underlying request. |
| `RpcError.ExecutionError` | The node could not execute an underlying transaction request. |
| `RpcError.FeeCapTooHighError` | `maxFeePerGas` exceeds the maximum allowed value. |
| `RpcError.TipAboveFeeCapError` | `maxPriorityFeePerGas` is greater than `maxFeePerGas`. |
