# Getting the embedded state

Configure Openfort's embedded wallets in your application with two steps:

1. Configure the embedded wallet.
2. Wait for the signer to be ready.

## Waiting for ready

When calling `embeddedWallet.configure`, `embeddedWallet.create` or `embeddedWallet.recover`, the SDK will go through a series of states before it is ready to be used. These states are represented by the enum:

| State                                   | Description                                                            |
| --------------------------------------- | ---------------------------------------------------------------------- |
| **0** - NONE                            | The initial state of the SDK.                                          |
| **1** - UNAUTHENTICATED                 | Before the user is authenticated.                                      |
| **2** - EMBEDDED\_SIGNER\_NOT\_CONFIGURED  | Before calling `embeddedWallet.configure`.                          |
| **3** - CREATING\_ACCOUNT                | If no account exists for the current chainID, when it will be created. |
| **4** - READY                           | The embedded wallet is ready to be used                                |

**As a consequence, it's important to wait until the `embeddedState` has finished initializing before you use the embedded wallet**, to ensure that the state you consume is accurate and not stale.

## Awaiting initialization

After constructing the `Openfort` instance, call `waitForInitialization()` to ensure async setup (including storage access) is complete before using any SDK methods:

```ts
import openfort from "./openfortConfig";

await openfort.waitForInitialization();
// SDK is now fully initialized — safe to access auth, embeddedWallet, etc.
```

:::warning
If you skip `waitForInitialization()`, SDK methods that depend on internal state (such as `auth` and `embeddedWallet`) may throw errors.
:::

## Checking embedded state

To determine whether the Openfort SDK has initialized the embedded wallet, you can call the method `embeddedWallet.getEmbeddedState` and check if the state is `READY`:

:::code-group

```ts [main.ts]
import openfort from "./openfortConfig";

const state = await openfort.embeddedWallet.getEmbeddedState();
```

```ts [openfortConfig.ts]
import { Openfort } from "@openfort/openfort-js";

const openfort = new Openfort({
  baseConfiguration: {
    publishableKey: "YOUR_OPENFORT_PUBLISHABLE_KEY",
  },
  shieldConfiguration: {
    shieldPublishableKey: "YOUR_SHIELD_PUBLISHABLE_KEY",
  },
});

export default openfort;
```

:::

## Watching embedded state reactively

For reactive UIs, use `watchEmbeddedState()` to subscribe to state changes instead of polling:

```ts
import openfort from "./openfortConfig";

const unsubscribe = openfort.embeddedWallet.watchEmbeddedState({
  onChange: (state) => {
    console.log('Embedded state changed:', state);
    // Update your UI based on the new state
  },
  onError: (error) => {
    console.error('State watch error:', error);
  },
  pollingInterval: 1000, // Optional: polling interval in ms
});

// Later, to stop watching:
unsubscribe();
```

The `watchEmbeddedState` method uses a combination of event-driven updates and polling to ensure your UI stays in sync with the wallet state. It returns an unsubscribe function to stop monitoring.
