> ## Documentation Index
> Fetch the complete documentation index at: https://developers.fhsc.com.vn/llms.txt
> Use this file to discover all available pages before exploring further.

# Bootstrap flow

> Cách lấy userId và subAccountId trước khi gọi các endpoint trading.

2 endpoint dưới tag **Khởi tạo** là 2 lệnh gọi tối thiểu cần thực hiện
để lấy `userId` và `subAccountId` — 2 giá trị dùng làm path parameter
cho hầu hết endpoint user-scoped. Gọi 1 lần khi khởi tạo client, cache
kết quả, rồi truyền vào các endpoint phía sau.

<Warning>
  Cả 2 call bootstrap đều thuộc Tier 2 nên phải HMAC sign. Xem
  [Authentication](/authentication) để setup signing trước khi chạy
  bootstrap.
</Warning>

<Steps>
  <Step title="Lấy userId">
    ```http theme={null}
    GET /users/v1/users/me
    ```

    Response:

    ```json theme={null}
    {
      "error_code": "0",
      "message": "success",
      "data": {
        "user_id": 123456
      }
    }
    ```

    Lưu `data.user_id` làm `userId` cho mọi endpoint user-scoped (assets
    summary, pnl-today, …).
  </Step>

  <Step title="Lấy subAccountId">
    ```http theme={null}
    GET /users/v1/users/{userId}/sub-accounts
    ```

    Response:

    ```json theme={null}
    {
      "error_code": "0",
      "message": "success",
      "result": [
        {
          "id": "0001234567",
          "cust_id": "0001234500",
          "name": "Nguyễn Văn A",
          "depository_number": "120C001234",
          "product_type_name": ".1",
          "sub_account_ext": "120C001234.1",
          "type": "Normal"
        },
        {
          "id": "0001234568",
          "cust_id": "0001234500",
          "name": "Nguyễn Văn A",
          "depository_number": "120C001234",
          "product_type_name": ".6",
          "sub_account_ext": "120C001234.6",
          "type": "Margin"
        }
      ]
    }
    ```

    Chọn entry có `type` đúng nhu cầu — `Normal` cho tài khoản thường,
    `Margin` cho tài khoản ký quỹ — rồi lấy `id` làm `subAccountId`.
  </Step>
</Steps>

## Chiến lược cache

Cả `userId` và `subAccountId` đều khá static (không đổi trong vòng đời
client):

* Lưu vào memory ngay sau bootstrap.
* Re-fetch khi nhận `401` từ endpoint user-scoped (tài khoản có thể bị
  disable / rotate).
* Không cần TTL ngắn — chỉ invalidate khi user đăng xuất hoặc chuyển
  tài khoản.

## Sử dụng tiếp

Sau khi có `userId` + `subAccountId`, truy cập được các nhóm endpoint
sau trong [API Reference](/api-reference):

<CardGroup cols={2}>
  <Card title="Tổng tài sản" icon="wallet" href="/api-reference/người-dùng/tổng-tài-sản">
    Giá trị net asset value, breakdown theo product, cash, debt, PnL.
  </Card>

  <Card title="Tiểu khoản" icon="building-columns" href="/api-reference/giao-dịch/tiểu-khoản">
    Số dư, margin, dư nợ và ngân hàng liên kết.
  </Card>

  <Card title="Danh mục đầu tư" icon="chart-pie" href="/api-reference/giao-dịch/danh-mục-đầu-tư">
    Stock đang nắm giữ kèm giá realtime và PnL theo vị thế.
  </Card>

  <Card title="Sổ lệnh" icon="list-check" href="/api-reference/giao-dịch/sổ-lệnh">
    Lệnh trong ngày, full list hoặc detail theo `orderId`.
  </Card>

  <Card title="Lãi/lỗ" icon="chart-line" href="/api-reference/giao-dịch/lãi-lỗ">
    PnL trong ngày tổng hợp qua mọi tiểu khoản.
  </Card>

  <Card title="Quyền cổ đông" icon="award" href="/api-reference/giao-dịch/quyền-cổ-đông">
    Cổ tức, quyền mua, bỏ phiếu — của tiểu khoản.
  </Card>
</CardGroup>
