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

# Notification email

> Verify the notification inbox of an account with a one-time code, and read every outcome of the code request and the code check.

After you read this page, you can verify an inbox for an account, handle each outcome, and name the inbox in a rule document so that its `notify` actions send mail.

A rule mails its account through a `notify` action on the `email` channel. Every `notify` of a document mails `global.notification_email`. The engine refuses a stage of a document whose `notification_email` is not the verified inbox of the account (`NotificationEmailNotVerifiedError`). So verify the inbox first, then put it in the document. See the [notification email flow](https://docs.ana.fi/flows/notification-email.md).

> [!WARNING]
>
> The Anafi API accepts the code request only when its sign-in message names the domain of the Anafi dapp, `app.ana.fi`. It refuses a sign-in for another domain with status `422`. An integration on another domain cannot complete the code request today.

## The flow

1. `requestNotificationEmailCode({ aa, adapter, email, eoa })`. The owner signs one sign-in. The engine mails a six-digit code and returns a `code_token`.
2. The owner reads the code in the mail.
3. `verifyNotificationEmailCode({ aa, codeToken, code })`. No prompt.
4. On `verified`, set the inbox in the document with `documentWithNotificationEmail(yaml, canonicalEmail)`, then stage and commit.

## `requestNotificationEmailCode`

```ts theme={null}
requestNotificationEmailCode(params: {
  aa: Hex;
  adapter: Hex;
  email: string;
  eoa: Hex;
}): Promise<NotificationEmailCodeRequestReply>;
```

| Parameter | Meaning |
| - | - |
| `aa` | The account. |
| `adapter` | The adapter of the account: `delegationAdapters.safe` or `delegationAdapters.erc20_allow`. |
| `email` | The inbox as the user typed it. |
| `eoa` | The owner. The owner signs the sign-in. |

Steps:

1. `canonicalNotificationEmail(email)`. A `null` result throws `Error("notification email must be a bare ASCII address without whitespace or display-name syntax")`.
2. Render the sign-in with `notificationEmailCodeSiweMessage`. It expires 300 seconds after `Issued At`.
3. **Prompt:** `walletClient.signMessage({ account: eoa, message })`.
4. Seal `{ "adapter", "email", "siwe_message", "signature" }` to the engine key under `notificationEmailSealInfo("request_code", network, aa)`. `email` is the text as typed.
5. `POST /api/wallets/:aa/notification_email/request_code` with `{ sealed }`.
6. Open and read the reply. Throw when the reply names another account.

The sign-in message:

```text theme={null}
app.ana.fi wants you to sign in with your Ethereum account:
0x1111111111111111111111111111111111111111

Verify ada@example.com to receive notifications from wallet 0x2222222222222222222222222222222222222222.

URI: https://app.ana.fi
Version: 1
Chain ID: 80002
Nonce: 3f9c0a7d5e2b4c1a8f6e0d9b7a5c3e1f
Issued At: 2026-12-03T10:00:00.000Z
Expiration Time: 2026-12-03T10:05:00.000Z
Resources:
- urn:anafi:notification-email-code:0x2222222222222222222222222222222222222222:0x850b820c200108962c59a66ce70968c8cbd429e9e5a391765eaf59287f4482c5
```

### `NotificationEmailCodeRequestReply`

```ts theme={null}
interface NotificationEmailCodeRequestReply {
  aa: Hex;               // the account of the request
  email: string | null;  // the canonical email; null only for invalid_email
  outcome: NotificationEmailCodeRequestOutcome;
}

type NotificationEmailCodeRequestOutcome =
  | { kind: "already_verified" }
  | { kind: "invalid_email" }
  | { kind: "mail_not_configured" }
  | { kind: "retry_after"; seconds: number }
  | { kind: "code_issued"; code_token: string; delivery: CodeEmailDelivery };

type CodeEmailDelivery = "sent" | "failed";
```

| `outcome.kind` | Meaning | What to do |
| - | - | - |
| `already_verified` | The account already has this inbox verified. No mail was sent. | Use the inbox. |
| `invalid_email` | The address has no canonical form, or it cannot get mail. `email` is `null`. | Ask for another address. |
| `mail_not_configured` | This network sends no mail. | Hide the email feature. |
| `retry_after` | A send budget refuses the request now. | Ask again after `seconds`. |
| `code_issued` | The engine made a code and tried to mail it. `code_token` is `0x` and 32 lowercase hex digits. | Keep `code_token`, and ask the user for the code. |

| `delivery` | Meaning |
| - | - |
| `sent` | The mail server accepted the mail. |
| `failed` | The mail failed or timed out. The request still used one send. Offer a new request. |

Throws: `BackendRequestError` for a failed request (for example `422` for a refused sign-in), `Error` for a reply of the wrong shape, and the error of the wallet when the owner rejects the prompt.

```ts theme={null}
const reply = await anafi.requestNotificationEmailCode({
  aa: safe,
  adapter: config.delegationAdapters.safe!,
  email: "Ada@Example.com",
  eoa: owner,
});
if (reply.outcome.kind === "code_issued") codeToken = reply.outcome.code_token;
```

## `verifyNotificationEmailCode`

```ts theme={null}
verifyNotificationEmailCode(params: {
  aa: Hex;
  codeToken: string;
  code: string;
}): Promise<NotificationEmailCodeVerifyReply>;
```

Checks the code. No prompt: the `code_token` and the code from the mail prove the request.

Steps: seal `{ "code_token", "code" }` under `notificationEmailSealInfo("verify_code", network, aa)`, then `POST /api/wallets/:aa/notification_email/verify_code` with `{ sealed }`, then open and read the reply. It throws when the reply names another account.

### `NotificationEmailCodeVerifyReply`

```ts theme={null}
interface NotificationEmailCodeVerifyReply {
  aa: Hex;
  email: string | null; // the canonical pending email; null for code_token_invalid
  outcome: NotificationEmailCodeVerifyOutcome;
}

type NotificationEmailCodeVerifyOutcome =
  | { kind: "verified" }
  | { kind: "wrong_code"; attempts_left: number }
  | { kind: "code_token_invalid" };
```

| `outcome.kind` | Meaning | What to do |
| - | - | - |
| `verified` | The inbox `email` is now the verified inbox of the account. | Put `email` in the document. |
| `wrong_code` | The code is wrong. `attempts_left` attempts remain. | Ask again. At 0, request a new code. |
| `code_token_invalid` | The token is unknown, expired, for another account, or has no attempts left. | Request a new code. |

```ts theme={null}
const checked = await anafi.verifyNotificationEmailCode({ aa: safe, codeToken, code: "123456" });
if (checked.outcome.kind === "verified" && checked.email !== null) {
  yaml = documentWithNotificationEmail(yaml, checked.email);
}
```

The verified inbox also comes back in `recover` as `verified_notification_email`.

## Helpers

### `canonicalNotificationEmail`

```ts theme={null}
function canonicalNotificationEmail(raw: string): string | null;
```

Removes ASCII whitespace at both ends and returns the lowercase text. Returns `null` for an empty text, any non-ASCII character, inner ASCII whitespace, and any of `<`, `>`, `,`, `;`, `"`. It does not check the mailbox syntax or whether the address can get mail.

```ts theme={null}
canonicalNotificationEmail("  Ada@Example.COM ");     // "ada@example.com"
canonicalNotificationEmail("Ada <ada@example.com>");   // null
```

### `notificationEmailCodeSiweMessage`

```ts theme={null}
function notificationEmailCodeSiweMessage(fields: NotificationEmailCodeSignInFields): string;

interface NotificationEmailCodeSignInFields {
  domain: string;
  uri: string;
  owner: Hex;
  chainId: number;
  nonce: string;
  issuedAt: string;   // RFC 3339
  expiresAt: string;  // RFC 3339
  aa: Hex;
  canonicalEmail: string;
}
```

Renders the code request sign-in. It throws `Error("canonicalEmail must already be in notification-email canonical form")` when `canonicalEmail` is not canonical. The statement is `Verify <canonicalEmail> to receive notifications from wallet <aa lowercase>.`, and the one resource is `notificationEmailCodeResourceUri(aa, canonicalEmail)`.

### `notificationEmailCodeResourceUri`

```ts theme={null}
function notificationEmailCodeResourceUri(aa: Hex, canonicalEmail: string): string;
// urn:anafi:notification-email-code:<aa lowercase>:<keccak256(utf8(canonicalEmail))>
```

Binds the sign-in to one account and to the hash of one inbox.

### `notificationEmailSealInfo`

```ts theme={null}
type NotificationEmailRoute = "request_code" | "verify_code";
function notificationEmailSealInfo(route: NotificationEmailRoute, network: string, aa: Hex): Uint8Array;
```

The HPKE `info` of a request: the UTF-8 text `anafi/notification-email/request-code/<network>/` or `anafi/notification-email/verify-code/<network>/`, then the 20 raw bytes of the account. A request sealed for one route, network or account does not open for another.

### Reply readers

```ts theme={null}
function readNotificationEmailCodeRequestReply(value: unknown): NotificationEmailCodeRequestReply;
function readNotificationEmailCodeVerifyReply(value: unknown): NotificationEmailCodeVerifyReply;
```

Read an opened reply, and throw on any field of the wrong shape, an unknown `kind`, or an `email` that does not fit the outcome (for example `invalid_email` with an email).

### `NotificationEmailNotVerifiedError`

The stage methods throw it when the document names a `notification_email` that is not the verified inbox of the account. It extends `DslStageRefusedError`. See [errors](https://docs.ana.fi/sdk/errors.md#notificationemailnotverifiederror).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.