> **Can't find what you're looking for?** Use `search_docs` on the docs MCP server at `https://www.openfort.io/api/mcp` to find what you need.
>
> **Have feedback?** Use `submit_feedback` on the same MCP server.

# `useOpenfort`

:::info
If using Next.js App Router, add `"use client"` at the top of the file.
:::

Access the core Openfort context, including the SDK client instance, embedded wallet state, and user information.

:::warning
This hook provides low-level access to the Openfort SDK. For most use cases, prefer using the specialized hooks like [`useUser`](https://www.openfort.io/docs/products/embedded-wallet/react/hooks/useUser), [`useEthereumEmbeddedWallet`](https://www.openfort.io/docs/products/embedded-wallet/react/hooks/useEthereumEmbeddedWallet), [`useSolanaEmbeddedWallet`](https://www.openfort.io/docs/products/embedded-wallet/react/hooks/useSolanaEmbeddedWallet), or the authentication hooks.
:::

## Usage

```tsx
import { useOpenfort } from '@openfort/react';

function AdvancedComponent() {
  const { client, embeddedState, user, isLoading } = useOpenfort();

  // Access the underlying Openfort SDK client for advanced operations
  const handleAdvancedOperation = async () => {
    const accessToken = await client.getAccessToken();
    if (!accessToken) return;
    // Use the token for custom API calls
  };

  if (isLoading) return <div>Loading...</div>;

  return (
    <div>
      <p>Embedded state: {embeddedState}</p>
      <p>User ID: {user?.id}</p>
      <button onClick={handleAdvancedOperation}>
        Get Access Token
      </button>
    </div>
  );
}
```

## Return type

The hook returns the core Openfort context value (`OpenfortCoreContextValue`):

```ts
import type {
  ChainTypeEnum,
  EmbeddedAccount,
  EmbeddedState,
  Openfort,
  User,
  UserAccount,
} from '@openfort/openfort-js'

type UseOpenfortReturn = {
  client: Openfort
  chainType: ChainTypeEnum
  setChainType: (chainType: ChainTypeEnum) => void
  signUpGuest: () => Promise<void>
  embeddedState: EmbeddedState
  isLoading: boolean
  needsRecovery: boolean
  user: User | null
  setUser: (user: User | null) => void
  updateUser: (user?: User) => Promise<User | null>
  linkedAccounts: UserAccount[]
  setLinkedAccounts: (accounts: UserAccount[]) => void
  embeddedAccounts?: EmbeddedAccount[]
  setEmbeddedAccounts: (accounts: EmbeddedAccount[] | undefined) => void
  isLoadingAccounts: boolean
  activeEmbeddedAddress: string | undefined
  setActiveEmbeddedAddress: (address: string | undefined) => void
  logout: () => Promise<void>
  updateEmbeddedAccounts: (options?: { silent?: boolean }) => Promise<EmbeddedAccount[] | undefined>
  walletStatus: WalletFlowStatus
  setWalletStatus: (status: WalletFlowStatus) => void
  recoveryError: Error | null
  setRecoveryError: (error: Error | null) => void
}
```

The hook returns the full `OpenfortStore`; the excerpt above lists the fields most useful to application code.

## Store selectors (advanced)

Store selectors and context are available only from the internal entry point:

```ts
import {
  StoreContext,
  selectActiveAddress,
  selectChainType,
  selectEmbeddedState,
  selectIsAuthenticated,
  selectIsLoading,
  selectUser,
  selectWalletStatus,
} from '@openfort/react/internal'
import type { OpenfortStore, OpenfortStoreState } from '@openfort/react/internal'
```

* `StoreContext` — React context providing access to the `OpenfortStore`
* `selectActiveAddress` — Selector for the active wallet address
* `selectChainType` — Selector for the current chain type
* `selectEmbeddedState` — Selector for the embedded wallet state
* `selectIsAuthenticated` — Selector for authentication status
* `selectIsLoading` — Selector for loading status
* `selectUser` — Selector for the current user
* `selectWalletStatus` — Selector for wallet flow status

The internal entry point can change in any release, including patch releases. Prefer public hooks unless no public API covers the required state.

## Example: Custom API calls with access token

```tsx
import { useOpenfort } from '@openfort/react';

function CustomApiExample() {
  const { client } = useOpenfort();

  const fetchUserData = async () => {
    const accessToken = await client.getAccessToken();
    if (!accessToken) throw new Error('Not authenticated');

    const response = await fetch('https://your-api.com/user-data', {
      headers: {
        'Authorization': `Bearer ${accessToken}`,
      },
    });

    return response.json();
  };

  return <button onClick={fetchUserData}>Fetch Data</button>;
}
```

## Example: Check embedded wallet state

```tsx
import { useOpenfort } from '@openfort/react'
import { EmbeddedState } from '@openfort/openfort-js'

function WalletStateChecker() {
  const { embeddedState, needsRecovery } = useOpenfort();

  const getStateMessage = () => {
    switch (embeddedState) {
      case EmbeddedState.NONE:
        return 'No wallet configured';
      case EmbeddedState.UNAUTHENTICATED:
        return 'Please sign in';
      case EmbeddedState.EMBEDDED_SIGNER_NOT_CONFIGURED:
        return 'Wallet signer not configured';
      case EmbeddedState.CREATING_ACCOUNT:
        return 'Creating wallet...';
      case EmbeddedState.READY:
        return needsRecovery ? 'Wallet needs recovery' : 'Wallet ready';
      default:
        return 'Unknown state';
    }
  };

  return <p>{getStateMessage()}</p>;
}
```
