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

# Sub Accounts

> Create and manage app-specific wallet accounts with Base Account

export const GithubRepoCard = ({title, githubUrl}) => {
  return <a href={githubUrl} target="_blank" rel="noopener noreferrer" className="mb-4 flex items-center rounded-lg bg-zinc-900 p-4 text-white transition-all hover:bg-zinc-800">
      <div className="flex w-full items-center gap-3">
        <svg height="24" width="24" className="flex-shrink-0 dark:fill-white" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
          <path fill="currentColor" fillRule="evenodd" d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0016 8c0-4.42-3.58-8-8-8z" />
        </svg>

        <div className="flex min-w-0 flex-grow flex-col">
          <span className="truncate text-base font-medium">{title}</span>
          <span className="truncate text-xs text-zinc-400">{githubUrl}</span>
        </div>

        <svg className="h-5 w-5 flex-shrink-0 text-zinc-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
          <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5l7 7-7 7" />
        </svg>
      </div>
    </a>;
};

Learn how to create and manage Sub Accounts that provide app-specific wallet accounts embedded directly in your application.

## Overview

Sub Accounts allow you to provision dedicated wallet accounts for your users within your application.
These accounts are controlled by the user's main Base Account and provide an enhanced user experience
as the user's interactions produce no passkey prompts or popups.

Users can manage all their Sub Accounts at [account.base.app](https://account.base.app).

<Note>
  If you would like to see a live demo of Sub Accounts in action, check out our [Sub Accounts Demo](https://sub-accounts-fc.vercel.app).
</Note>

### What you'll achieve

By the end of this guide, you will:

* Understand how Sub Accounts work with Base Account
* Create new Sub Accounts for users
* Retrieve and manage existing Sub Accounts
* Implement Sub Account in your Privy project

The code snippets in this guide are based on the following example project:

<GithubRepoCard title="Base Account Privy Template" githubUrl="https://github.com/base/base-account-privy" />

## Setup

Follow the [Setup](/base-account/framework-integrations/privy/setup) guide to set up Privy with Base Account.

## Implementation

### Component Setup

<CodeGroup>
  ```tsx Sub Accounts Component (components/sections/sub-accounts.tsx) expandable theme={null}
  "use client";

  import { useState, useMemo } from "react";
  import { useWallets } from "@privy-io/react-auth";

  const SubAccounts = () => {
    const { wallets } = useWallets();
    const [subAccounts, setSubAccounts] = useState<
      {
        address: string;
        factory: string;
        factoryData: string;
      }[]
    >([]);
    const [isLoading, setIsLoading] = useState(false);

    // Find the Base Account wallet
    const baseAccount = useMemo(() => {
      return wallets.find((wallet) => wallet.walletClientType === 'base_account');
    }, [wallets]);

    const handleGetSubAccounts = async () => {
      if (!baseAccount) return;

      setIsLoading(true);
      try {
        // Switch to Base Sepolia (or Base Mainnet - use 8453 for mainnet)
        await baseAccount.switchChain(84532);
        const provider = await baseAccount.getEthereumProvider();

        // Get existing Sub Accounts
        const response = await provider.request({
          method: 'wallet_getSubAccounts',
          params: [{
            account: baseAccount.address,
            domain: window.location.origin
          }]
        });

        const { subAccounts: existingSubAccounts } = response;
        setSubAccounts(existingSubAccounts || []);
      } catch (error) {
        console.error("Error getting Sub Accounts:", error);
      } finally {
        setIsLoading(false);
      }
    };

    const handleAddSubAccount = async () => {
      if (!baseAccount) return;

      setIsLoading(true);
      try {
        // Switch to Base Sepolia (or Base Mainnet - use 8453 for mainnet)
        await baseAccount.switchChain(84532);
        const provider = await baseAccount.getEthereumProvider();

        // Create new Sub Account
        await provider.request({
          method: 'wallet_addSubAccount',
          params: [{
            version: '1',
            account: {
              type: 'create',
              keys: [{
                type: 'address',
                publicKey: baseAccount.address
              }]
            }
          }]
        });

        // Refresh the Sub Accounts list
        await handleGetSubAccounts();
      } catch (error) {
        console.error("Error creating Sub Account:", error);
      } finally {
        setIsLoading(false);
      }
    };

    return (
      <div className="space-y-4">
        <div className="flex gap-4">
          <button 
            onClick={handleGetSubAccounts}
            disabled={!baseAccount || isLoading}
            className="px-4 py-2 bg-blue-600 text-white rounded disabled:opacity-50"
          >
            Get Sub Accounts
          </button>
          <button 
            onClick={handleAddSubAccount}
            disabled={!baseAccount || isLoading}
            className="px-4 py-2 bg-green-600 text-white rounded disabled:opacity-50"
          >
            Create Sub Account
          </button>
        </div>

        {subAccounts.length > 0 && (
          <div>
            <h4 className="font-medium mb-2">Existing Sub Accounts:</h4>
            <div className="space-y-2">
              {subAccounts.map((subAccount, index) => (
                <div key={index} className="p-3 border rounded-md">
                  <p><strong>Address:</strong> {subAccount.address}</p>
                  <p><strong>Factory:</strong> {subAccount.factory}</p>
                  <p><strong>Factory Data:</strong> {subAccount.factoryData}</p>
                </div>
              ))}
            </div>
          </div>
        )}
      </div>
    );
  };
  ```
</CodeGroup>

### Key Methods

#### Getting Sub Accounts

Use `wallet_getSubAccounts` to retrieve existing Sub Accounts for a domain:

```tsx Get Sub Accounts (components/sections/sub-accounts.tsx) theme={null}
const response = await provider.request({
  method: 'wallet_getSubAccounts',
  params: [{
    account: baseAccount.address,
    domain: window.location.origin
  }]
});
```

#### Creating Sub Accounts

Use `wallet_addSubAccount` to create new Sub Accounts:

```tsx Create Sub Account (components/sections/sub-accounts.tsx) theme={null}
await provider.request({
  method: 'wallet_addSubAccount',
  params: [{
    version: '1',
    account: {
      type: 'create',
      keys: [{
        type: 'address',
        publicKey: baseAccount.address
      }]
    }
  }]
});
```

### Network Configuration

Sub accounts work on both Base Mainnet and Base Sepolia:

* **Base Mainnet**: Chain ID `8453`
* **Base Sepolia**: Chain ID `84532`

### Explore further

* [Sub Accounts Guide](/base-account/improve-ux/sub-accounts)
* [Privy Sub Accounts Recipe](https://docs.privy.io/recipes/react/external-wallets/base-sub-accounts)
