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

# Xác thực

> Tìm hiểu cách xác thực các yêu cầu API Helius một cách an toàn và hiệu quả

Helius API sử dụng khóa API để xác thực yêu cầu. Mỗi yêu cầu API phải bao gồm khóa API để xác minh danh tính và quyền của bạn.

<Warning>
  Khóa API là thông tin nhạy cảm cấp quyền truy cập vào tài khoản Helius của bạn. Tuyệt đối không để lộ khóa trong mã phía máy khách, kho lưu trữ công khai hoặc các khu vực có thể truy cập từ trình duyệt.
</Warning>

## Bắt đầu

### 1. Tạo khóa API

<Steps>
  <Step title="Sign up or log in">
    Tạo tài khoản trên [Bảng điều khiển Helius](https://dashboard.helius.dev) hoặc đăng nhập vào tài khoản hiện có.
  </Step>

  <Step title="Navigate to API Keys">
    Chuyển đến mục **API Keys** trong thanh bên của bảng điều khiển.
  </Step>

  <Step title="Generate a new key">
    Nhấp vào **Create New API Key** và đặt tên mô tả cho dự án của bạn (ví dụ: "Ứng dụng sản xuất", "Môi trường phát triển").
  </Step>

  <Step title="Copy and secure your key">
    Sao chép khóa API ngay lập tức và lưu trữ khóa một cách an toàn. Bạn sẽ không thể xem lại khóa sau khi rời khỏi trang.
  </Step>
</Steps>

### 2. Sử dụng khóa API

Thêm khóa API dưới dạng tham số truy vấn trong tất cả các yêu cầu:

<CodeGroup>
  ```bash cURL theme={"system"}
  curl "https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY" \
    -X POST \
    -H "Content-Type: application/json" \
    -d '{"jsonrpc":"2.0","id":1,"method":"getAccountInfo","params":["ACCOUNT_ADDRESS"]}'
  ```

  ```javascript JavaScript theme={"system"}
  const url = `https://mainnet.helius-rpc.com/?api-key=${YOUR_API_KEY}`;
  const response = await fetch(url, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      jsonrpc: '2.0',
      id: 1,
      method: 'getAccountInfo',
      params: ['ACCOUNT_ADDRESS']
    })
  });
  ```

  ```python Python theme={"system"}
  import requests

  url = f"https://mainnet.helius-rpc.com/?api-key={YOUR_API_KEY}"
  payload = {
      "jsonrpc": "2.0",
      "id": 1,
      "method": "getAccountInfo",
      "params": ["ACCOUNT_ADDRESS"]
  }
  response = requests.post(url, json=payload)
  ```
</CodeGroup>

## Bắt đầu (Dành cho tác nhân)

Các tác nhân có thể đăng ký tài khoản Helius, tạo dự án và tạo khóa API theo phương thức lập trình bằng [Helius CLI](/docs/vi/agents/cli).

Để xem hướng dẫn đầy đủ, hãy đọc: [https://dashboard.helius.dev/agents.md](https://dashboard.helius.dev/agents.md)

### Cài đặt Helius CLI

<CodeGroup>
  ```bash theme={"system"}
  npm install -g helius-cli
  ```
</CodeGroup>

### Tạo cặp khóa

<CodeGroup>
  ```bash theme={"system"}
  helius keygen
  ```
</CodeGroup>

### Nạp tiền vào ví đã tạo (chỉ dành cho Autopay)

Bỏ qua bước này nếu thanh toán qua liên kết thanh toán được lưu trữ (chế độ đăng ký mặc định, được hoàn tất bằng `helius signup --resume`). Autopay (`--pay`) thanh toán từ cặp khóa cục bộ: gửi 1 USDC và 0.001 SOL đến địa chỉ ví được cung cấp ở Bước 2.

### Đăng ký và nhận khóa API

<CodeGroup>
  ```bash theme={"system"}
  # Default: prints a hosted payment link — pay with any wallet in the browser
  helius signup --email you@example.com --first-name Jane --last-name Doe --json

  # After paying via the link, finalize the account
  helius signup --resume --json

  # Or autopay from the funded local keypair
  helius signup --plan agent --pay --email you@example.com --first-name Jane --last-name Doe --json
  ```
</CodeGroup>

## Các phương pháp bảo mật tốt nhất

<CardGroup cols={2}>
  <Card title="Environment Variables" icon="shield-check">
    Lưu trữ khóa API trong các biến môi trường, không lưu trong mã nguồn.

    ```bash theme={"system"}
    export HELIUS_API_KEY="YOUR_API_KEY"
    ```
  </Card>

  <Card title="IP Restrictions" icon="globe">
    Thiết lập giới hạn IP cho khóa API trong bảng điều khiển để chỉ cho phép truy cập từ các địa chỉ hoặc dải IP cụ thể.
  </Card>

  <Card title="Separate Keys" icon="key">
    Sử dụng các khóa API khác nhau cho môi trường phát triển, thử nghiệm và sản xuất để cô lập hoạt động sử dụng và tăng cường bảo mật.
  </Card>

  <Card title="Monitor Usage" icon="chart-line">
    Thường xuyên kiểm tra mức sử dụng API trong bảng điều khiển để phát hiện các mẫu bất thường hoặc vấn đề bảo mật tiềm ẩn.
  </Card>
</CardGroup>

### Quản lý bí mật

<Tabs>
  <Tab title="Node.js">
    ```javascript theme={"system"}
    // Use environment variables
    const apiKey = process.env.HELIUS_API_KEY;

    // Or use a secrets manager
    const { SecretManagerServiceClient } = require('@google-cloud/secret-manager');
    const client = new SecretManagerServiceClient();

    async function getApiKey() {
      const [version] = await client.accessSecretVersion({
        name: 'projects/PROJECT_ID/secrets/helius-api-key/versions/latest',
      });
      return version.payload.data.toString();
    }
    ```
  </Tab>

  <Tab title="Python">
    ```python theme={"system"}
    import os
    from dotenv import load_dotenv

    # Load environment variables
    load_dotenv()
    api_key = os.getenv('HELIUS_API_KEY')

    # Or use AWS Secrets Manager
    import boto3

    def get_secret():
        client = boto3.client('secretsmanager')
        response = client.get_secret_value(SecretId='helius-api-key')
        return response['SecretString']
    ```
  </Tab>

  <Tab title="Docker">
    ```dockerfile theme={"system"}
    # In your Dockerfile
    ENV HELIUS_API_KEY=""

    # Or use Docker secrets
    RUN --mount=type=secret,id=helius_key \
        cat /run/secrets/helius_key > /app/api_key.txt
    ```
  </Tab>
</Tabs>

## Giới hạn tốc độ và mức sử dụng

<Note>
  Giới hạn tốc độ thay đổi tùy theo gói đăng ký. Theo dõi mức sử dụng trong [Bảng điều khiển Helius](https://dashboard.helius.dev) để đảm bảo không vượt quá giới hạn được phân bổ.
</Note>

### Tìm hiểu về giới hạn tốc độ

* **Số yêu cầu mỗi giây**: Dựa trên cấp đăng ký của bạn
* **Hạn ngạch yêu cầu hằng tháng**: Tổng số yêu cầu được phép trong mỗi chu kỳ thanh toán
* **Mức tăng đột biến cho phép**: Lưu lượng tăng đột biến trong thời gian ngắn vượt quá giới hạn tốc độ cơ bản

### Xử lý giới hạn tốc độ

<CodeGroup>
  ```javascript JavaScript theme={"system"}
  async function makeRequest(url, data) {
    try {
      const response = await fetch(url, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(data)
      });
      
      if (response.status === 429) {
        const retryAfter = response.headers.get('Retry-After');
        console.log(`Rate limited. Retry after ${retryAfter} seconds`);
        await new Promise(resolve => setTimeout(resolve, retryAfter * 1000));
        return makeRequest(url, data); // Retry
      }
      
      return response.json();
    } catch (error) {
      console.error('Request failed:', error);
      throw error;
    }
  }
  ```

  ```python Python theme={"system"}
  import time
  import requests

  def make_request(url, data):
      try:
          response = requests.post(url, json=data)
          
          if response.status_code == 429:
              retry_after = int(response.headers.get('Retry-After', 60))
              print(f"Rate limited. Waiting {retry_after} seconds...")
              time.sleep(retry_after)
              return make_request(url, data)  # Retry
          
          response.raise_for_status()
          return response.json()
      except requests.exceptions.RequestException as e:
          print(f"Request failed: {e}")
          raise
  ```
</CodeGroup>

## Khắc phục sự cố

<AccordionGroup>
  <Accordion title="Invalid API Key Error">
    **Triệu chứng**: Lỗi 401 Unauthorized hoặc "Invalid API Key"

    **Giải pháp**:

    * Xác minh khóa API là chính xác và chưa được tạo lại
    * Kiểm tra xem bạn đã thêm khóa API dưới dạng tham số truy vấn hay chưa: `?api-key=YOUR_KEY`
    * Đảm bảo khóa API không chứa khoảng trắng hoặc ký tự thừa
    * Xác nhận khóa API chưa hết hạn hoặc bị thu hồi
  </Accordion>

  <Accordion title="Rate Limit Exceeded">
    **Triệu chứng**: Lỗi 429 Too Many Requests

    **Giải pháp**:

    * Kiểm tra mức sử dụng hiện tại trong bảng điều khiển
    * Triển khai cơ chế chờ lũy thừa trong logic thử lại
    * Cân nhắc nâng cấp gói để có giới hạn cao hơn
    * Tối ưu hóa yêu cầu để giảm các lệnh gọi không cần thiết
  </Accordion>

  <Accordion title="Forbidden Access">
    **Triệu chứng**: Lỗi 403 Forbidden

    **Giải pháp**:

    * Xác minh các giới hạn IP không chặn yêu cầu của bạn
    * Kiểm tra xem gói đăng ký có bao gồm quyền truy cập vào điểm cuối hay không
    * Đảm bảo khóa API có các quyền cần thiết
  </Accordion>
</AccordionGroup>

## Các bước tiếp theo

<CardGroup cols={2}>
  <Card title="Quickstart Guide" icon="rocket" href="/docs/vi/quickstart">
    Bắt đầu thực hiện các lệnh gọi API đầu tiên với Helius
  </Card>

  <Card title="API Reference" icon="book" href="/docs/vi/api-reference">
    Khám phá tất cả điểm cuối và phương thức hiện có
  </Card>

  <Card title="Rate Limits" icon="credit-card" href="/docs/vi/billing/rate-limits">
    Tìm hiểu về giới hạn tốc độ và các tùy chọn nâng cấp
  </Card>

  <Card title="Dashboard" icon="chart-line" href="https://dashboard.helius.dev">
    Theo dõi mức sử dụng API và quản lý khóa
  </Card>
</CardGroup>

## Hỗ trợ

Bạn cần trợ giúp về xác thực hoặc có câu hỏi về khóa API?

<CardGroup cols={2}>
  <Card title="Discord Community" icon="discord" href="https://discord.com/invite/6GXdee3gBj">
    Tham gia Discord của chúng tôi để nhận trợ giúp theo thời gian thực và hỗ trợ từ cộng đồng
  </Card>

  <Card title="Email Support" icon="envelope" href="mailto:support@helius.xyz">
    Liên hệ trực tiếp với đội ngũ hỗ trợ của chúng tôi
  </Card>
</CardGroup>
