> For the complete documentation index, see [llms.txt](https://www.aevo.xyz/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.aevo.xyz/docs/api-reference/subaccounts/creating-subaccounts.md).

# Creating subaccounts

A subaccount is an Ethereum address with its own private key. To create one, the main account proves it owns the subaccount, and the subaccount's key proves it agrees to be linked. Both signatures go into one request to [`POST /subaccounts`](https://www.aevo.xyz/docs/api-reference/urls/private-api/postsubaccounts.md).

## Overview

1. **Derive the subaccount key** from a signature by the main wallet (recommended), so the key can always be recovered from the wallet.
2. **Sign `CreateSubaccount`** with the main wallet or one of the main account's signing keys.
3. **Sign `LinkSubaccount`** with the subaccount key.
4. **Send** both signatures to `POST /subaccounts` with the main account's API key.

## Step 1. Derive the subaccount key

Aevo derives subaccount keys from the main wallet, so you never need to store them. The app uses this exact scheme. If you use it too, subaccounts you create through the API also work in the app, including transfers back to the main account.

1. The **main wallet** signs the EIP-712 message `SubaccountSeed`:

   ```
   SubaccountSeed(address account,uint256 version)
   ```

   with `account` = the main account address and `version` = `1`.
2. `seed = keccak256(signature[0:64])`. Use the first 64 bytes of the 65-byte signature (`r` and `s`); leave out `v`.
3. For subaccount number `i` (starting at `1`):

   ```
   subaccount_private_key = keccak256(seed ‖ uint32_big_endian(i))
   ```
4. The subaccount address is the address of that private key.

Use the lowest `i` whose address isn't already one of your subaccounts, including archived ones. `GET /subaccounts` lists them.

{% hint style="warning" %}
The seed signature must be deterministic: the same wallet must always produce the same signature for the same message. Standard wallets (EOAs) do. Smart contract wallets and some MPC wallets don't, and can't use this scheme.
{% endhint %}

{% hint style="danger" %}
Treat the seed signature like a private key. Anyone who has it can derive every subaccount key. Don't log or store it.
{% endhint %}

## Step 2. Sign `CreateSubaccount`

Signed by the **main wallet** or one of the **main account's signing keys**:

```
CreateSubaccount(address account,address subaccount,string name)
```

* `account`: the main account address.
* `subaccount`: the subaccount address from step 1.
* `name`: 1 to 16 characters. Letters, numbers, spaces, `_` and `-` only, with no leading or trailing spaces. It must be unique among your active subaccounts.

## Step 3. Sign `LinkSubaccount`

Signed by the **subaccount's private key** from step 1:

```
LinkSubaccount(address account,address subaccount)
```

with the same `account` and `subaccount` values.

## EIP-712 domain

All three messages use the same domain as [order signing](https://www.aevo.xyz/docs/api-reference/signing-orders.md):

|           | Mainnet        | Testnet        |
| --------- | -------------- | -------------- |
| `name`    | `Aevo Mainnet` | `Aevo Testnet` |
| `version` | `1`            | `1`            |
| `chainId` | `1`            | `11155111`     |

## Step 4. Create the subaccount

```python
import json
import os

from eth_account import Account
from eth_account.messages import encode_typed_data
from eth_utils import keccak

MAIN_WALLET_KEY = os.environ["MAIN_WALLET_KEY"]  # main account wallet
SIGNING_KEY = os.environ["SIGNING_KEY"]          # main account signing key
MAIN_ACCOUNT = Account.from_key(MAIN_WALLET_KEY).address

DOMAIN = {"name": "Aevo Testnet", "version": "1", "chainId": 11155111}
DOMAIN_TYPE = [
    {"name": "name", "type": "string"},
    {"name": "version", "type": "string"},
    {"name": "chainId", "type": "uint256"},
]


def sign(private_key, primary_type, fields, message):
    typed = encode_typed_data(full_message={
        "types": {"EIP712Domain": DOMAIN_TYPE, primary_type: fields},
        "primaryType": primary_type,
        "domain": DOMAIN,
        "message": message,
    })
    return Account.sign_message(typed, private_key).signature


# Step 1: derive the key for subaccount number 1
seed_signature = sign(
    MAIN_WALLET_KEY,
    "SubaccountSeed",
    [{"name": "account", "type": "address"}, {"name": "version", "type": "uint256"}],
    {"account": MAIN_ACCOUNT, "version": 1},
)
seed = keccak(seed_signature[:64])
index = 1
sub_key = keccak(seed + index.to_bytes(4, "big"))
subaccount = Account.from_key(sub_key).address

# Step 2: the main account approves the subaccount
name = "Market Making"
parent_signature = sign(
    SIGNING_KEY,
    "CreateSubaccount",
    [
        {"name": "account", "type": "address"},
        {"name": "subaccount", "type": "address"},
        {"name": "name", "type": "string"},
    ],
    {"account": MAIN_ACCOUNT, "subaccount": subaccount, "name": name},
)

# Step 3: the subaccount key agrees to the link
subaccount_signature = sign(
    sub_key,
    "LinkSubaccount",
    [{"name": "account", "type": "address"}, {"name": "subaccount", "type": "address"}],
    {"account": MAIN_ACCOUNT, "subaccount": subaccount},
)

# Step 4: send with the main account's API key (see Authentication)
body = json.dumps({
    "subaccount": subaccount,
    "name": name,
    "parent_signature": "0x" + parent_signature.hex(),
    "subaccount_signature": "0x" + subaccount_signature.hex(),
})
print(body)
```

The response confirms the link:

```json
{
  "parent": "0xYourMainAccountAddress",
  "subaccount": "0xYourSubaccountAddress",
  "name": "Market Making"
}
```

The subaccount can trade as soon as the request succeeds. Fund it with a transfer from the main account.

## Errors

| Error                          | Meaning                                                                                                                                       |
| ------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------- |
| `SUBACCOUNT_DISABLED`          | Subaccount creation is not enabled.                                                                                                           |
| `SUBACCOUNT_LIMIT_REACHED`     | The main account already has the maximum number of active subaccounts. Archive one, or use an existing one.                                   |
| `SUBACCOUNT_INVALID_NAME`      | The name breaks the naming rules.                                                                                                             |
| `SUBACCOUNT_NAME_TAKEN`        | Another active subaccount of this main account has the same name.                                                                             |
| `SUBACCOUNT_NOT_FRESH`         | The subaccount address already has an Aevo account. Use the next index.                                                                       |
| `SUBACCOUNT_NESTING`           | A subaccount can't own subaccounts, and an account that already owns subaccounts can't become one.                                            |
| `SUBACCOUNT_PARENT_INELIGIBLE` | This account type can't own subaccounts.                                                                                                      |
| `SUBACCOUNT_INVALID_SIGNATURE` | `parent_signature` or `subaccount_signature` doesn't match the request. Check the domain, the field values and which key signed each message. |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.aevo.xyz/docs/api-reference/subaccounts/creating-subaccounts.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
