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

# Enroll User (Stage 2)

> Stage 2 of the two-stage enrollment flow — commits the user's signed session-key and creates the portfolio.

Stage 2 of the two-stage enrollment flow. Takes the `flowId`, `accountIndex`,
and `agentAccountId` returned by `POST /v2/enroll/signature`, plus the user's
`signature`, and provisions the portfolio: one smart account per requested
chain, plus the rebalance schedule derived from your tenant's execution
config. The whole operation succeeds atomically — a failure anywhere rolls
back cleanly.

* Auth: `x-api-key` header (required)
* Scope: `enroll:write`

The endpoint is idempotent on `flowId` (scoped to your API key) with a
24-hour TTL:

* Replaying the same `flowId` with the same body returns the cached response.
* Replaying with a different body returns `409 IDEMPOTENCY_KEY_CONFLICT`.
* Replaying while the original is still in-flight returns
  `409 IDEMPOTENCY_IN_PROGRESS`.
* Replaying after a typed error (e.g., signature mismatch) returns the same
  typed error so retries don't get stuck.

The signature must be computed by signing `message.raw` from the stage-1
response with the user's wallet, using viem's `signMessage({ message: { raw } })`
or the equivalent wallet primitive.

Wallet addresses are exchanged as [CAIP-10](https://chainagnostic.org/CAIPs/caip-10)
account identifiers. `ownerAccountId` and `agentAccountId` use the
chain-agnostic EVM form `eip155:0:<address>`. Each element of the response
`smartAccounts` array uses the chain-bound form `eip155:<chainId>:<address>`.
Only EVM wallets are supported today.

Common error responses:

* `400` for an invalid request body, expired flow, mismatched signature, or
  invalid strategy ownership
* `401` when `x-api-key` header is missing or the key is invalid
* `403` when the API key lacks the `enroll:write` scope
* `409` `IDEMPOTENCY_IN_PROGRESS` when an earlier request with the same `flowId`
  is still running
* `409` `IDEMPOTENCY_KEY_CONFLICT` when the same `flowId` is reused with a
  different request body
* `409` `PORTFOLIO_ALREADY_EXISTS` when the user is already enrolled at the
  given `accountIndex`
* `500` on unexpected server errors

<RequestExample>
  ```bash cURL theme={null}
  curl --request POST \
    --url 'https://api.glider.fi/v2/enroll' \
    --header 'x-api-key: gldr_sk_your_api_key' \
    --header 'Content-Type: application/json' \
    --data '{
      "ownerAccountId": "eip155:0:0xabcdef0000000000000000000000000000000001",
      "strategyId": "01JWZEE2MF30KVRMRX53N88VA4",
      "chainIds": [1, 8453],
      "accountIndex": "7",
      "agentAccountId": "eip155:0:0x1111111111111111111111111111111111111111",
      "signature": "0x9412d70d539f889ecec2d3152b68af758d689a4325a43b07811ee708814527c72256fc749d7fd3745a07f0aaa84813955050c461392d9b9ee1f80e106fac39e01b",
      "flowId": "flow_abc123",
      "portfolioName": "Alice Portfolio"
    }'
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch("https://api.glider.fi/v2/enroll", {
    method: "POST",
    headers: {
      "x-api-key": "gldr_sk_your_api_key",
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      ownerAccountId: "eip155:0:0xabcdef0000000000000000000000000000000001",
      strategyId: "01JWZEE2MF30KVRMRX53N88VA4",
      chainIds: [1, 8453],
      accountIndex: "7",
      agentAccountId: "eip155:0:0x1111111111111111111111111111111111111111",
      signature:
        "0x9412d70d539f889ecec2d3152b68af758d689a4325a43b07811ee708814527c72256fc749d7fd3745a07f0aaa84813955050c461392d9b9ee1f80e106fac39e01b",
      flowId: "flow_abc123",
      portfolioName: "Alice's Portfolio",
    }),
  });
  ```
</RequestExample>

<ResponseExample>
  ```json 201 theme={null}
  {
    "success": true,
    "data": {
      "portfolioId": "pf_01JWZG3KH9P4N5QXJVNK7M3WTV",
      "strategyId": "01JWZEE2MF30KVRMRX53N88VA4",
      "smartAccounts": [
        { "accountId": "eip155:1:0x2222222222222222222222222222222222222222" },
        { "accountId": "eip155:8453:0x3333333333333333333333333333333333333333" }
      ]
    }
  }
  ```

  ```json 400 theme={null}
  {
    "success": false,
    "error": {
      "code": "API_400",
      "message": "Signature does not match the session-key message for this user"
    }
  }
  ```

  ```json 409 theme={null}
  {
    "success": false,
    "error": {
      "code": "API_202",
      "message": "User 0xabc... is already enrolled at account index 7"
    }
  }
  ```

  ```json 409 theme={null}
  {
    "success": false,
    "error": {
      "code": "API_007",
      "message": "Another enrollment request with the same flowId is still in progress"
    }
  }
  ```
</ResponseExample>
