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

# 身份验证与设置

> 获取你的 Solana Tracker Yellowstone gRPC 凭据、使用 x-token 配置客户端身份验证，并连接到流式端点开始订阅数据。

## 获取您的 API 密钥

<Steps>
  <Step title="订阅服务">
    独立 gRPC 方案 **€200/月**，或与 RPC Business、Professional 方案免费捆绑。详见 [定价与限制](/cn/pricing)。

    你将获得以下访问权限:

    * 欧盟端点: `https://grpc.solanatracker.io`
    * 美国端点: `https://grpc-us.solanatracker.io`
    * 通过 Jito Shreds 加速
    * 无带宽费用
  </Step>

  <Step title="接收凭据">
    订阅后您将收到:

    * 您的 API 令牌
    * 端点 URL
    * 配置指南
  </Step>

  <Step title="测试连接">
    通过创建简单的测试连接验证您的凭据是否有效
  </Step>
</Steps>

## 身份验证方法

### 基于请求头的身份验证

将您的 API 令牌添加到请求头中:

<ParamField header="x-token" type="string" required>
  用于身份验证 gRPC 请求的 API 令牌
</ParamField>

## 客户端配置

### TypeScript/JavaScript

```bash theme={null}
npm install @triton-one/yellowstone-grpc
```

```typescript theme={null}
import Client from "@triton-one/yellowstone-grpc";

const client = new Client(
  "https://grpc.solanatracker.io",  // or https://grpc-us.solanatracker.io
  "your-api-token",
  {
    // Recommended settings
    "grpc.max_receive_message_length": 64 * 1024 * 1024,  // 64MB
    "grpc.max_send_message_length": 64 * 1024 * 1024,     // 64MB
    "grpc.keepalive_time_ms": 30000,                      // 30 seconds
    "grpc.keepalive_timeout_ms": 5000,                    // 5 seconds
  }
);
```

### Rust

```toml theme={null}
[dependencies]
yellowstone-grpc-client = "9.0.1"
yellowstone-grpc-proto = "9.0.1"
tokio = { version = "1.0", features = ["full"] }
tonic = "0.10"
```

```rust theme={null}
use yellowstone_grpc_client::GeyserGrpcClient;
use std::time::Duration;

let endpoint = "https://grpc.solanatracker.io";
let token = Some("your-api-token".to_string());

let client = GeyserGrpcClient::build_from_shared(endpoint)?
    .x_token(token)?
    .connect_timeout(Duration::from_secs(10))
    .timeout(Duration::from_secs(10))
    .max_decoding_message_size(64 * 1024 * 1024)
    .connect()
    .await?;
```

### Go

```bash theme={null}
go get github.com/rpcpool/yellowstone-grpc/examples/golang@latest
go get google.golang.org/grpc@v1.67.1
```

```go theme={null}
import (
    "google.golang.org/grpc"
    "google.golang.org/grpc/metadata"
)

conn, err := grpc.Dial(
    "grpc.solanatracker.io:443",
    grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")),
    grpc.WithDefaultCallOptions(
        grpc.MaxCallRecvMsgSize(64 * 1024 * 1024),
        grpc.MaxCallSendMsgSize(64 * 1024 * 1024),
    ),
)

// Add authentication
ctx := metadata.AppendToOutgoingContext(context.Background(), "x-token", "your-api-token")
```

## 端点选择

选择为您的基础设施提供最佳延迟的端点:

<CardGroup cols={2}>
  <Card title="主端点" icon="globe" iconType="duotone">
    **[https://grpc.solanatracker.io](https://grpc.solanatracker.io)**

    * 最适合全球应用
    * 自动路由
    * 高可用性
  </Card>

  <Card title="美国端点" icon="location-dot" iconType="duotone">
    **[https://grpc-us.solanatracker.io](https://grpc-us.solanatracker.io)**

    * 针对北美优化
    * 美国应用延迟更低
    * 直接美国路由
  </Card>
</CardGroup>

<Tip>
  测试两个端点并使用从您部署区域延迟较低的那个。
</Tip>

## 配置最佳实践

### 消息大小限制

设置适当的消息大小限制以处理大区块和交易批次:

```typescript theme={null}
{
  "grpc.max_receive_message_length": 64 * 1024 * 1024,  // 64MB
  "grpc.max_send_message_length": 64 * 1024 * 1024      // 64MB
}
```

### Keepalive 设置

配置 keepalive 以维持持久连接:

```typescript theme={null}
{
  "grpc.keepalive_time_ms": 30000,        // Send keepalive every 30s
  "grpc.keepalive_timeout_ms": 5000,      // Wait 5s for keepalive response
  "grpc.keepalive_permit_without_calls": 1 // Allow keepalive without active calls
}
```

### 连接超时

设置合理的超时值:

```typescript theme={null}
{
  "grpc.initial_reconnect_backoff_ms": 1000,  // Start with 1s backoff
  "grpc.max_reconnect_backoff_ms": 30000,     // Max 30s backoff
  "grpc.min_reconnect_backoff_ms": 1000       // Min 1s backoff
}
```

## 测试您的连接

创建一个简单的测试来验证您的设置:

```typescript theme={null}
import Client, { CommitmentLevel } from "@triton-one/yellowstone-grpc";

async function testConnection() {
  const client = new Client(
    "https://grpc.solanatracker.io",
    "your-api-token",
    {
      "grpc.max_receive_message_length": 64 * 1024 * 1024
    }
  );

  try {
    const stream = await client.subscribe();
    
    stream.on("data", (data) => {
      console.log("Connection successful! Received data:", data);
      stream.end();
      client.close();
      process.exit(0);
    });

    stream.on("error", (error) => {
      console.error("Connection error:", error);
      process.exit(1);
    });

    // Subscribe to slot updates (lightweight test)
    const request = {
      slots: { slotSubscribe: {} },
      commitment: CommitmentLevel.CONFIRMED
    };

    stream.write(request);

    console.log("Testing connection...");
  } catch (error) {
    console.error("Failed to connect:", error);
    process.exit(1);
  }
}

testConnection();
```

## 故障排除

<Accordion title="身份验证失败">
  **错误:** `UNAUTHENTICATED` 或 `Invalid token`

  **解决方案:**

  * 验证您的 API 令牌是否正确
  * 检查令牌是否正确设置在请求头中
  * 确保订阅有效
</Accordion>

<Accordion title="连接超时">
  **错误:** 连接超时或无法建立

  **解决方案:**

  * 验证端点 URL 正确(包括 `https://`)
  * 检查防火墙是否允许出站 gRPC 连接
  * 尝试备用端点
  * 增加连接超时设置
</Accordion>

<Accordion title="消息大小错误">
  **错误:** `RESOURCE_EXHAUSTED` 或消息大小错误

  **解决方案:**

  * 将 `max_receive_message_length` 增加到 64MB
  * 将 `max_send_message_length` 增加到 64MB
  * 使用数据切片减少消息大小
  * 应用更具体的过滤器
</Accordion>

## 安全最佳实践

<Warning>
  **保护您的 API 令牌**

  * 切勿将令牌提交到版本控制中
  * 使用环境变量存储令牌
  * 定期轮换令牌
  * 实施令牌管理系统
</Warning>

### 环境变量

安全地存储您的凭据:

```bash theme={null}
# .env file
GRPC_ENDPOINT=https://grpc.solanatracker.io
GRPC_API_TOKEN=your-api-token
```

```typescript theme={null}
import * as dotenv from 'dotenv';
dotenv.config();

const client = new Client(
  process.env.GRPC_ENDPOINT!,
  process.env.GRPC_API_TOKEN!,
  { /* ... */ }
);
```

## 下一步

<CardGroup cols={2}>
  <Card title="快速入门指南" icon="rocket" iconType="duotone" href="/cn/yellowstone-grpc/quickstart">
    构建您的第一个流式应用程序
  </Card>

  <Card title="账户监控" icon="user" iconType="duotone" href="/cn/yellowstone-grpc/account-monitoring">
    学习监控账户变化
  </Card>
</CardGroup>
