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

# WebSocket认证和频道过滤器

> 对OddsPapi WebSocket网关进行认证并配置频道订阅。按体育、锦标赛、赛事和博彩公司过滤以获取目标数据流。

## 登录消息

连接后立即发送`login`消息：

```json theme={null}
{
  "type": "login",
  "apiKey": "YOUR_API_KEY"
}
```

**规则：**

* 必须是第一条消息
* 在10秒内发送
* 定义所有订阅和过滤器

***

## 带频道和过滤器的登录

```json theme={null}
{
  "type": "login",
  "apiKey": "YOUR_API_KEY",
  "receiveType": "binary",
  "lang": "zh",
  "channels": ["fixtures", "scores", "odds"],
  "sportIds": [10, 11, 12, 13],
  "tournamentIds": [35430, 39351],
  "fixtureIds": ["id1103543066138356", "id1103935163991375"],
  "bookmakers": ["stake"]
}
```

***

## 过滤模式

| 字段              | 类型         | 描述                                  |
| --------------- | ---------- | ----------------------------------- |
| `channels`      | `string[]` | 您想要接收的流。                            |
| `sportIds`      | `number[]` | 限制为这些体育运动。                          |
| `tournamentIds` | `number[]` | 限制为这些锦标赛。                           |
| `fixtureIds`    | `string[]` | 精确的赛事（仅限赛事范围频道）。                    |
| `futureIds`     | `string[]` | 特定的期货（期货范围频道）。                      |
| `bookmakers`    | `string[]` | 仅接收这些博彩公司（博彩公司门控）。                  |
| `lang`          | `string`   | 翻译语言（使用 `"zh"` 获取中文，默认 `"en"`）。     |
| `receiveType`   | `string`   | `"json"`或`"binary"`。                |
| `clientName`    | `string`   | 可选的调试/指标标签。                         |
| `serverEpoch`   | `string`   | 用于恢复。                               |
| `lastSeenId`    | `object`   | `{ "<channel>": "<entryId>" }`用于恢复。 |

> 像`fixtureId`和`futureId`这样的ID是结构化的，但在您的逻辑中应被视为不透明的。参见[概念](/zh/api-reference/concepts)。

***

## 访问权限：实时 vs 赛前

登录后，服务器会告诉您允许接收什么：

```json theme={null}
{
  "access": { "live": true, "pregame": false }
}
```

这些由您的`apiKey`决定，而不是客户端过滤器。

***

## 博彩公司门控频道

这些频道需要明确的博彩公司访问权限：

* `odds`、`bookmakers`
* `oddsFutures`、`bookmakersFutures`

您可以限制接收哪些博彩公司：

```json theme={null}
{
  "type": "login",
  "apiKey": "YOUR_API_KEY",
  "channels": ["odds", "bookmakers"],
  "receiveType": "binary",
  "lang": "zh",
  "bookmakers": ["stake", "pinnacle"]
}
```

***

## Python示例

```python theme={null}
import asyncio, json, websockets, msgpack

WS_URL = "wss://v5.oddspapi.io/ws"
API_KEY = "your-api-key"

LOGIN = {
    "type": "login",
    "apiKey": API_KEY,
    "channels": ["fixtures", "scores", "odds"],
    "receiveType": "binary",
    "lang": "zh",
    "sportIds": [10, 11],
    "bookmakers": ["stake"],
}

async def main():
    async with websockets.connect(WS_URL) as ws:
        await ws.send(json.dumps(LOGIN))

        async for raw in ws:
            if isinstance(raw, str):
                print("CONTROL:", json.loads(raw))
            else:
                msg = msgpack.unpackb(raw, raw=False)
                print("DATA:", msg.get("channel"), msg.get("entryId"))

asyncio.run(main())
```
