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

# PublicKey.toBytes

:::info
`PublicKey.toBytes` is a re-export of ox's [`PublicKey.toBytes`](https://oxlib.sh/api/PublicKey/toBytes). Refer to the ox documentation for the full reference.
:::

Serializes a [`PublicKey.PublicKey`](/docs/utilities/publickey/types#publickey) to [`Bytes.Bytes`](/docs/utilities/bytes/types#bytes).

## Imports

```ts
import { PublicKey } from 'viem/utils'
```

## Examples

```ts twoslash
// @noErrors
import { PublicKey } from 'viem/utils'

const publicKey = PublicKey.from({
  prefix: 4,
  x: '0x8318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed75',
  y: '0x3547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5'
})

const bytes = PublicKey.toBytes(publicKey) // [!code focus]
// @log: Uint8Array [128, 3, 131, ...]
```

## Definition

```ts
function toBytes(
  publicKey: PublicKey<boolean>,
  options?: toBytes.Options,
): Bytes.Bytes
```

## Parameters

### publicKey

* **Type:** `PublicKey<boolean>`

The public key to serialize.

#### publicKey.prefix

* **Type:** `numberType`

#### publicKey.x

* **Type:** `0x${string}`

#### publicKey.y

* **Type:** `0x${string}`

### options

* **Type:** `toBytes.Options`
* **Optional**

#### options.includePrefix

* **Type:** `boolean`
* **Optional**

Whether to include the prefix in the serialized public key.

## Return Type

The serialized public key.

`Bytes.Bytes`
