# Close & Withdraw

## Overview

When a channel is finished, it is closed and the balance is reconciled: the payee keeps the settled
amount and the payer reclaims the rest. The payee (or an operator) can close immediately with the
latest voucher, while the payer starts a close timer that protects the payee before withdrawing.

[See the Payment Channels guide](https://docs.tempo.xyz/guide/machine-payments/pay-as-you-go)

## Recipes

These recipes assume you have [set up a Tempo client](/tempo).

### Close from the Payee Side

The payee can close immediately with the latest voucher using
[`channel.closeSync`](/tempo/actions/channel.close), capturing the paid amount and releasing the rest
to the payer.

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

const result = await client.channel.closeSync({
  captureAmount: parseUnits('40', 6),
  cumulativeAmount: parseUnits('40', 6),
  channel,
  signature: '0x...',
})
```

```ts twoslash [viem.config.ts] filename="viem.config.ts"
// [!include ~/snippets/tempo/viem.config.ts:setup]

// [!include ~/snippets/tempo/viem.config.ts:channel]
```
:::

### Request Close from the Payer Side

The payer cannot close instantly. Start the close timer with
[`channel.requestCloseSync`](/tempo/actions/channel.requestClose), which gives the payee a window to
settle outstanding vouchers first.

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

const result = await client.channel.requestCloseSync({ channel })
```

```ts twoslash [viem.config.ts] filename="viem.config.ts"
// [!include ~/snippets/tempo/viem.config.ts:setup]

// [!include ~/snippets/tempo/viem.config.ts:channel]
```
:::

### Withdraw After Closing

Once the channel is closed (or the close timer has elapsed), the payer reclaims the remaining deposit
with [`channel.withdrawSync`](/tempo/actions/channel.withdraw).

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

const result = await client.channel.withdrawSync({ channel })
```

```ts twoslash [viem.config.ts] filename="viem.config.ts"
// [!include ~/snippets/tempo/viem.config.ts:setup]

// [!include ~/snippets/tempo/viem.config.ts:channel]
```
:::

## Best Practices

### Let the Payee Close When Possible

Payee-side close is instant because the payee presents the final voucher. Prefer it for a clean
settlement. Reserve the payer-side request-close flow for when the payee is unresponsive.

### Respect the Close Timer

The payer's close timer exists so the payee can settle outstanding vouchers before funds are
returned. Wait for it to elapse before withdrawing, and have the payee settle promptly when a close
is requested.

### Settle Before You Close

Make sure the latest voucher is settled (or included in the close) so the payee captures everything
they are owed. Closing on a stale amount can leave value on the table.

## See More

<Cards>
  <Card icon="lucide:receipt" title="Send & Settle Vouchers" description="Settle the latest voucher before closing the channel." to="/tempo/guides/payment-channels/vouchers" />

  <Card icon="lucide:plug" title="Open & Fund a Channel" description="Open a new channel after closing an old one." to="/tempo/guides/payment-channels/open" />

  <Card icon="lucide:square-function" title="channel.requestClose" description="Start the payer close timer for a channel." to="/tempo/actions/channel.requestClose" />

  <Card icon="lucide:book-open" title="Tempo Docs: Payment Channels" description="Channel closing, timers, and withdrawal." to="https://docs.tempo.xyz/guide/machine-payments/pay-as-you-go" />
</Cards>
