建立 Realtime WebSocket 连接
curl --request GET \
--url https://kaienapi.com/v1/realtime \
--header 'Authorization: Bearer <token>'import requests
url = "https://kaienapi.com/v1/realtime"
headers = {"Authorization": "Bearer <token>"}
response = requests.get(url, headers=headers)
print(response.text)const options = {method: 'GET', headers: {Authorization: 'Bearer <token>'}};
fetch('https://kaienapi.com/v1/realtime', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "https://kaienapi.com/v1/realtime"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", "Bearer <token>")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}建立 Realtime WebSocket 连接
OpenAI Realtime 兼容 WebSocket 接口,面向语音对话、低延迟多模态交互和持续会话客户端。
连接方式
使用 WebSocket 连接 /v1/realtime?model=<MODEL_ID>,并在请求头中携带 Authorization: Bearer <YOUR_API_KEY>。model 必须选择支持 Realtime 的模型。
连接结果
连接成功时返回 101 协议升级。升级后,客户端按 Realtime 事件协议收发消息;断线后需要重新建立连接,并恢复必要的会话状态。
错误定位
- 400:检查
modelquery 参数是否缺失,或模型是否支持 Realtime。 - 401:检查 Bearer Key 是否存在、过期或无访问权限。
- 浏览器连接失败:确认 WebSocket 地址使用
wss://,并检查代理是否允许协议升级。
GET
/
v1
/
realtime
建立 Realtime WebSocket 连接
curl --request GET \
--url https://kaienapi.com/v1/realtime \
--header 'Authorization: Bearer <token>'import requests
url = "https://kaienapi.com/v1/realtime"
headers = {"Authorization": "Bearer <token>"}
response = requests.get(url, headers=headers)
print(response.text)const options = {method: 'GET', headers: {Authorization: 'Bearer <token>'}};
fetch('https://kaienapi.com/v1/realtime', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "https://kaienapi.com/v1/realtime"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", "Bearer <token>")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}