# How to bridge to a recipient

This guide shows you how to bridge USDC to a recipient on the destination chain with Fast Bridge. To call contracts with it on the destination chain instead, see [How to bridge and call contracts](/guides/how-to/fast-bridge/bridge-and-call).

## Prerequisites

* A Pimlico API key with Fast Bridge enabled.
* An account holding USDC on the origin chain.

:::code-group
```bash [npm]
npm install viem
```

```bash [pnpm]
pnpm install viem
```

```bash [bun]
bun install viem
```
:::

## Steps

::::steps
### Set up a bridge client

Create the account and the functions that call the Fast Bridge API: one to get a chain's [bridge addresses](/references/fast-bridge/endpoints#get-addresseschain), and one to bridge the tokens.

:::code-group
```ts [main.ts]
import { type Address, type Hex, parseUnits, toHex } from "viem"
import { privateKeyToAccount } from "viem/accounts"
import { arbitrum, base } from "viem/chains"
import type {
	BridgeAddresses,
	BridgeRequest,
	BridgeStatus,
	Eip3009Authorization,
} from "./types"

const apiKey = "YOUR_PIMLICO_API_KEY"

const USDC_BASE: Address = "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913"
const USDC_ARBITRUM: Address = "0xaf88d065e77c8cC2239327C5EDb3A432268e5831"

// The account that holds the USDC on the origin chain
const user = privateKeyToAccount(process.env.USER_PRIVATE_KEY as Hex)

// Throws the bridge's { code, message } if it rejected the request
const readResponse = async (response: Response) => {
	const body = await response.json()
	if (!response.ok) {
		throw new Error(`${body.code}: ${body.message}`)
	}
	return body
}

// Who authorizations on a chain are made out to
const getBridgeAddresses = async (
	chainId: number,
): Promise<BridgeAddresses> => {
	const response = await fetch(
		`https://api.pimlico.io/v1/bridge/addresses/${chainId}?apikey=${apiKey}`,
	)
	return readResponse(response)
}

// Bridges the tokens to the recipient and waits for the outcome
const bridgeTokens = async (request: BridgeRequest): Promise<BridgeStatus> => {
	const response = await fetch(
		`https://api.pimlico.io/v1/bridge/execute/sync?apikey=${apiKey}`,
		{
			method: "POST",
			headers: { "Content-Type": "application/json" },
			body: JSON.stringify(request),
		},
	)
	return readResponse(response)
}
```

```ts [types.ts]
import type { Address, Hex } from "viem"

export type BridgeAddresses = { approvalAddress: Address; multicaller: Address }

export type Eip3009Authorization = {
	type: "eip3009"
	token: Address
	from: Address
	to: Address
	amount: string
	validAfter: number
	validBefore: number
	nonce: Hex
	signature: Hex
}

export type BridgeRequest = {
	originChainId: number
	destinationChainId: number
	originToken: Address
	destinationToken: Address
	authorization: Eip3009Authorization
	// Defaults to authorization.from
	recipient?: Address
}

export type BridgeStatus = {
	requestId: string
	status: "pending" | "success" | "failed" | "refunded"
	amountIn: string
	fee: string
	amountOut: string
	origin: { chainId: number; status: string; transactionHash?: string }
	destination: {
		chainId: number
		recipient: string
		status: string
		transactionHash?: string
		error?: { code: string; message: string }
	}
}
```
:::

:::warning
Don't ship your API key in code that runs in your users' browsers or apps. See [How to protect your API keys](/guides/how-to/security/protect-api-keys).
:::

### Sign the authorization

Sign an EIP-3009 `ReceiveWithAuthorization` for the amount, with `to` set to the origin chain's `approvalAddress`. For USDC on BSC, see [How to bridge from BSC](/guides/how-to/fast-bridge/bridge-with-permit2).

```ts
// Authorizations are made out to the origin chain's approvalAddress
const { approvalAddress } = await getBridgeAddresses(base.id)

const amount = parseUnits("25", 6) // 25 USDC
const validBefore = Math.floor(Date.now() / 1000) + 5 * 60 // 5 minutes
const nonce = toHex(crypto.getRandomValues(new Uint8Array(32)))

const signature = await user.signTypedData({
	// USDC's EIP-712 domain on Base
	domain: {
		name: "USD Coin",
		version: "2",
		chainId: base.id,
		verifyingContract: USDC_BASE,
	},
	types: {
		ReceiveWithAuthorization: [
			{ name: "from", type: "address" },
			{ name: "to", type: "address" },
			{ name: "value", type: "uint256" },
			{ name: "validAfter", type: "uint256" },
			{ name: "validBefore", type: "uint256" },
			{ name: "nonce", type: "bytes32" },
		],
	},
	primaryType: "ReceiveWithAuthorization",
	message: {
		from: user.address,
		to: approvalAddress,
		value: amount,
		validAfter: 0n,
		validBefore: BigInt(validBefore),
		nonce,
	},
})

const authorization: Eip3009Authorization = {
	type: "eip3009",
	token: USDC_BASE,
	from: user.address,
	to: approvalAddress,
	amount: amount.toString(),
	validAfter: 0,
	validBefore,
	nonce,
	signature,
}
```

### Send the request

Send the authorization with the origin and destination chains, the tokens and the `recipient`. `bridgeTokens` returns once the bridge has finished, and throws if the request is rejected. See the [error codes](/references/fast-bridge/errors).

```ts
const result = await bridgeTokens({
	originChainId: base.id,
	destinationChainId: arbitrum.id,
	originToken: USDC_BASE,
	destinationToken: USDC_ARBITRUM,
	authorization,
	recipient: user.address,
})
```

### Check the result

Read `status` from the response:

* `success`: the USDC was delivered in `destination.transactionHash`.
* `failed`: nothing was bridged, and the USDC is still on the origin chain.
* `pending`: the bridge hasn't finished. See [How to check a bridge's status](/guides/how-to/fast-bridge/track-request).

```ts
if (result.status === "success") {
	console.log("Delivered:", result.destination.transactionHash)
} else if (result.status === "failed") {
	console.log("Nothing was bridged")
} else {
	console.log("Still pending")
}
```
::::

## Related

* [How to bridge and call contracts](/guides/how-to/fast-bridge/bridge-and-call)
* [How to bridge from BSC](/guides/how-to/fast-bridge/bridge-with-permit2)
* [How to check a bridge's status](/guides/how-to/fast-bridge/track-request)
* [Fast Bridge reference](/references/fast-bridge)
* [Fast Bridge endpoints](/references/fast-bridge/endpoints)
* [Fast Bridge errors](/references/fast-bridge/errors)
