# Cancel Migration

Abort a key migration before the key has been exported, returning the wallet to server custody.

## Overview

A migration started with [Request Export Token](https://docs.aureahub.com/docs/wallets-export-token.md) can be cancelled only while the wallet is `client_side_pending` **and** no key has been exported yet. The user's password is verified again.

On success the server, in one database transaction:

- invalidates any unused export token for the wallet;
- restores `keyManagementScheme` to `sss_2of2_akv` if the wallet has a database key share, or `aes_single` otherwise;
- clears any key-deletion state.

Server-held key material is never removed while a wallet is `client_side_pending`, so the wallet is server-custodial again immediately.

> ⚠️ After a successful `POST /v1/wallets/export-key` this endpoint returns `409`: the migration must be completed with [Confirm Client Custody](https://docs.aureahub.com/docs/wallets-confirm-custody.md).

## Endpoint

### `POST /v1/wallets/cancel-migration`

Authentication: bearer token required.

Reverts a client_side_pending wallet to server custody. Blocked with 409 once the key has been exported.

**Request body**

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `walletId` | string (uuid) | yes | Wallet whose migration should be cancelled. |
| `password` | string | yes | The user's account password. |

**Responses**

`200` OK

```json
{ "cancelled": true }
```

`400` Bad Request

```json
{
  "statusCode": 400,
  "error": "BadRequestError",
  "message": "Wallet is not in client_side_pending state"
}
```

## Errors

Error responses

|  |  |  |
| --- | --- | --- |
| 400 | — | `Wallet is not in client_side_pending state` |
| 400 | — | `Invalid password` |
| 404 | — | `Wallet not found` — unknown id, or the wallet belongs to another user. |
| 409 | — | `Key has already been transmitted to the device. Migration cannot be cancelled after export. Complete the migration instead.` |

## Implementation

```javascript
async function cancelMigration(accessToken, walletId, password) {
  const res = await fetch('https://api.aureahub.com/v1/wallets/cancel-migration', {
    method: 'POST',
    headers: { Authorization: `Bearer ${accessToken}`, 'Content-Type': 'application/json' },
    body: JSON.stringify({ walletId, password })
  });
  if (res.status === 409) {
    throw new Error('Key already exported — complete the migration instead');
  }
  if (!res.ok) throw new Error((await res.json()).message);
  return res.json(); // { cancelled: true }
}
```

---

Web version: https://docs.aureahub.com/#wallets-cancel-migration
