> ## Documentation Index
> Fetch the complete documentation index at: https://docs.portalhq.io/llms.txt
> Use this file to discover all available pages before exploring further.

# backupWallet

> Create a backup of the user's wallet.

## Function Signature

```dart theme={null}
Future<PendingBackup> backupWallet({
  required PortalBackupMethod method,
  String? password,
})
```

## Description

Creates a backup of the user's wallet using the specified backup method. The backup allows the user to recover their wallet on a new device.

After `backupWallet` returns, call `response.confirm()` to mark the backup as complete with Portal. If your custodian storage step fails (Self-Managed Backups), call `response.discard()` instead so the native storage callback is released. For Portal-Managed Backups, `response.confirm()` and `response.discard()` are safe no-ops — Portal has already finalized the backup before `backupWallet` returns.

## Parameters

| Parameter  | Type                 | Required | Description                                       |
| ---------- | -------------------- | -------- | ------------------------------------------------- |
| `method`   | `PortalBackupMethod` | Yes      | The backup method to use                          |
| `password` | `String`             | No       | Required when using `PortalBackupMethod.password` |

### PortalBackupMethod

| Value         | Description                                 |
| ------------- | ------------------------------------------- |
| `password`    | Backup using a user-provided password       |
| `googleDrive` | Backup to Google Drive                      |
| `iCloud`      | Backup to iCloud (iOS only)                 |
| `passkey`     | Backup using passkey/WebAuthn               |
| `firebase`    | Backup using Firebase Auth + secure enclave |

## Returns

**`PendingBackup`** - An object containing:

| Property     | Type            | Description                                                                                                                                            |
| ------------ | --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `cipherText` | `String`        | The encrypted client backup share. Persist this on your own infrastructure when using Self-Managed Backups.                                            |
| `shareIds`   | `List<String?>` | List of share identifiers used internally for tracking.                                                                                                |
| `isPending`  | `bool`          | `true` when the caller must invoke `confirm()` or `discard()` (Self-Managed Backups). `false` for Portal-Managed Backups (already finalized natively). |
| `confirm()`  | `Future<void>`  | Marks the backup complete with Portal. No-op when `isPending` is `false`.                                                                              |
| `discard()`  | `Future<void>`  | Releases the native storage callback without firing it. No-op when `isPending` is `false`.                                                             |

## Example

```dart theme={null}
import 'package:portal_flutter/portal_flutter.dart';

final portal = Portal();
const method = PortalBackupMethod.password;

final response = await portal.backupWallet(
  method: method,
  password: 'user-password',
);

try {
  // (Self-Managed Backups) Persist response.cipherText on your own backend.
  await yourApi.storeEncryptedClientBackupShare(
    userId: userId,
    cipherText: response.cipherText,
  );
} catch (_) {
  // Storage step failed — release the pending callback so the user can retry.
  await response.discard();
  rethrow;
}

// Storage succeeded — mark the backup complete with Portal.
// If response.confirm() throws, let it propagate so the caller can retry.
await response.confirm();
```

## Errors

| Code                | Description                                                                                                                                                  |
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `NOT_INITIALIZED`   | Portal was not initialized                                                                                                                                   |
| `WALLET_NOT_FOUND`  | No wallet exists to backup                                                                                                                                   |
| `BACKUP_FAILED`     | The backup operation failed                                                                                                                                  |
| `NO_PENDING_BACKUP` | `confirm()` or `discard()` was called on a `PendingBackup` whose native callback was already consumed (e.g., a second `confirm()` after the first succeeded) |
| `PASSWORD_REQUIRED` | Password method selected but no password provided                                                                                                            |

## Related

* [Back up a wallet guide](../guide/back-up-a-wallet)
* [recoverWallet](./recoverwallet)
* [getClient](./getclient)
* [isWalletBackedUp](./iswalletbackedup)
