> **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.

# Get Bytecode \[address.getCode]

Retrieves the bytecode at an address.

## Usage

This example retrieves the bytecode at an address.

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

const code = await client.address.getCode({
  address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
// @log: '0x60806040...'
```

```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.address.getCode` 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 code = await Actions.address.getCode(client, {
  address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
// @log: '0x60806040...'
```

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

### Inspect Historical Deployment Code

Use `blockNumber` to check whether code existed at a historical block. An `undefined` result means the selected state has no code at that address.

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

const code = await client.address.getCode({
  address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  blockNumber: 19_000_000n, // [!code focus]
})
```

```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())
```
:::

## Return Value

`Hex | undefined`

The bytecode at the address, or `undefined` when the address has no code (`0x`).

## Parameters

### address

* **Type:** `Address`

The address to retrieve bytecode from.

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

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const code = await Actions.address.getCode(client, {
  address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2', // [!code focus]
})
```

### blockHash

* **Type:** `Hex`

The bytecode at a given block hash (EIP-1898).

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

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const code = await Actions.address.getCode(client, {
  address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  blockHash: '0x89644bbd5c8d682a2e9611170e6c1f02573d866d286f006cbf517eec7254ec2d', // [!code focus]
})
```

### blockNumber

* **Type:** `bigint`

The bytecode at a given block number.

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

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const code = await Actions.address.getCode(client, {
  address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  blockNumber: 42069n, // [!code focus]
})
```

### blockTag

* **Type:** `'latest' | 'earliest' | 'pending' | 'safe' | 'finalized'`
* **Default:** `'latest'`

The bytecode at a given block tag.

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

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const code = await Actions.address.getCode(client, {
  address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  blockTag: 'safe', // [!code focus]
})
```

### requireCanonical

* **Type:** `boolean`

Whether to error if the `blockHash` is not in the canonical chain. Can only be provided alongside `blockHash`.

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

const client = Client.create({ chain: mainnet, transport: http() })
// ---cut---
const code = await Actions.address.getCode(client, {
  address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  blockHash: '0x89644bbd5c8d682a2e9611170e6c1f02573d866d286f006cbf517eec7254ec2d',
  requireCanonical: true, // [!code focus]
})
```

## Errors

| Error | Description |
| --- | --- |
| `BlockParameter.RequireCanonicalError` | `requireCanonical` was provided without a `blockHash`. |
