telnyx-webrtc-go
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinese<!-- Auto-generated from Telnyx OpenAPI specs. Do not edit. -->
<!-- Auto-generated from Telnyx OpenAPI specs. Do not edit. -->
Telnyx Webrtc - Go
Telnyx Webrtc - Go
Installation
安装
bash
go get github.com/team-telnyx/telnyx-gobash
go get github.com/team-telnyx/telnyx-goSetup
初始化设置
go
import (
"context"
"fmt"
"os"
"github.com/team-telnyx/telnyx-go"
"github.com/team-telnyx/telnyx-go/option"
)
client := telnyx.NewClient(
option.WithAPIKey(os.Getenv("TELNYX_API_KEY")),
)All examples below assume is already initialized as shown above.
clientgo
import (
"context"
"fmt"
"os"
"github.com/team-telnyx/telnyx-go"
"github.com/team-telnyx/telnyx-go/option"
)
client := telnyx.NewClient(
option.WithAPIKey(os.Getenv("TELNYX_API_KEY")),
)以下所有示例均假设已按上述方式完成初始化。
clientError Handling
错误处理
All API calls can fail with network errors, rate limits (429), validation errors (422),
or authentication errors (401). Always handle errors in production code:
go
import "errors"
result, err := client.Messages.Send(ctx, params)
if err != nil {
var apiErr *telnyx.Error
if errors.As(err, &apiErr) {
switch apiErr.StatusCode {
case 422:
fmt.Println("Validation error — check required fields and formats")
case 429:
// Rate limited — wait and retry with exponential backoff
fmt.Println("Rate limited, retrying...")
default:
fmt.Printf("API error %d: %s\n", apiErr.StatusCode, apiErr.Error())
}
} else {
fmt.Println("Network error — check connectivity and retry")
}
}Common error codes: invalid API key, insufficient permissions,
resource not found, validation error (check field formats),
rate limited (retry with exponential backoff).
401403404422429所有API调用都可能因网络错误、速率限制(429)、校验错误(422)或认证错误(401)而失败,在生产代码中请务必做好错误处理:
go
import "errors"
result, err := client.Messages.Send(ctx, params)
if err != nil {
var apiErr *telnyx.Error
if errors.As(err, &apiErr) {
switch apiErr.StatusCode {
case 422:
fmt.Println("Validation error — check required fields and formats")
case 429:
// Rate limited — wait and retry with exponential backoff
fmt.Println("Rate limited, retrying...")
default:
fmt.Printf("API error %d: %s\n", apiErr.StatusCode, apiErr.Error())
}
} else {
fmt.Println("Network error — check connectivity and retry")
}
}常见错误码: API密钥无效, 权限不足, 资源未找到, 校验错误(请检查字段格式), 触发速率限制(请使用指数退避策略重试)。
401403404422429Important Notes
重要说明
- Pagination: Use for automatic iteration:
ListAutoPaging().iter := client.Resource.ListAutoPaging(ctx, params); for iter.Next() { item := iter.Current() }
- 分页: 使用实现自动迭代:
ListAutoPaging().iter := client.Resource.ListAutoPaging(ctx, params); for iter.Next() { item := iter.Current() }
List mobile push credentials
查询移动推送凭证列表
GET /mobile_push_credentialsgo
page, err := client.MobilePushCredentials.List(context.Background(), telnyx.MobilePushCredentialListParams{})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", page)Returns: (string), (string), (date-time), (string), (string), (object), (string), (string), (date-time)
aliascertificatecreated_atidprivate_keyproject_account_json_filerecord_typetypeupdated_atGET /mobile_push_credentialsgo
page, err := client.MobilePushCredentials.List(context.Background(), telnyx.MobilePushCredentialListParams{})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", page)返回参数:(字符串)、(字符串)、(日期时间)、(字符串)、(字符串)、(对象)、(字符串)、(字符串)、(日期时间)
aliascertificatecreated_atidprivate_keyproject_account_json_filerecord_typetypeupdated_atCreates a new mobile push credential
创建新的移动推送凭证
POST /mobile_push_credentialstypecertificateprivate_keyaliasgo
pushCredentialResponse, err := client.MobilePushCredentials.New(context.Background(), telnyx.MobilePushCredentialNewParams{
OfIos: &telnyx.MobilePushCredentialNewParamsCreateMobilePushCredentialRequestIos{
Alias: "LucyIosCredential",
Certificate: "-----BEGIN CERTIFICATE----- MIIGVDCCBTKCAQEAsNlRJVZn9ZvXcECQm65czs... -----END CERTIFICATE-----",
PrivateKey: "-----BEGIN RSA PRIVATE KEY----- MIIEpQIBAAKCAQEAsNlRJVZn9ZvXcECQm65czs... -----END RSA PRIVATE KEY-----",
},
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", pushCredentialResponse.Data)Returns: (string), (string), (date-time), (string), (string), (object), (string), (string), (date-time)
aliascertificatecreated_atidprivate_keyproject_account_json_filerecord_typetypeupdated_atPOST /mobile_push_credentialstypecertificateprivate_keyaliasgo
pushCredentialResponse, err := client.MobilePushCredentials.New(context.Background(), telnyx.MobilePushCredentialNewParams{
OfIos: &telnyx.MobilePushCredentialNewParamsCreateMobilePushCredentialRequestIos{
Alias: "LucyIosCredential",
Certificate: "-----BEGIN CERTIFICATE----- MIIGVDCCBTKCAQEAsNlRJVZn9ZvXcECQm65czs... -----END CERTIFICATE-----",
PrivateKey: "-----BEGIN RSA PRIVATE KEY----- MIIEpQIBAAKCAQEAsNlRJVZn9ZvXcECQm65czs... -----END RSA PRIVATE KEY-----",
},
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", pushCredentialResponse.Data)返回参数:(字符串)、(字符串)、(日期时间)、(字符串)、(字符串)、(对象)、(字符串)、(字符串)、(日期时间)
aliascertificatecreated_atidprivate_keyproject_account_json_filerecord_typetypeupdated_atRetrieves a mobile push credential
获取单个移动推送凭证
Retrieves mobile push credential based on the given
push_credential_idGET /mobile_push_credentials/{push_credential_id}go
pushCredentialResponse, err := client.MobilePushCredentials.Get(context.Background(), "0ccc7b76-4df3-4bca-a05a-3da1ecc389f0")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", pushCredentialResponse.Data)Returns: (string), (string), (date-time), (string), (string), (object), (string), (string), (date-time)
aliascertificatecreated_atidprivate_keyproject_account_json_filerecord_typetypeupdated_at根据传入的获取对应移动推送凭证
push_credential_idGET /mobile_push_credentials/{push_credential_id}go
pushCredentialResponse, err := client.MobilePushCredentials.Get(context.Background(), "0ccc7b76-4df3-4bca-a05a-3da1ecc389f0")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", pushCredentialResponse.Data)返回参数:(字符串)、(字符串)、(日期时间)、(字符串)、(字符串)、(对象)、(字符串)、(字符串)、(日期时间)
aliascertificatecreated_atidprivate_keyproject_account_json_filerecord_typetypeupdated_atDeletes a mobile push credential
删除移动推送凭证
Deletes a mobile push credential based on the given
push_credential_idDELETE /mobile_push_credentials/{push_credential_id}go
err := client.MobilePushCredentials.Delete(context.Background(), "0ccc7b76-4df3-4bca-a05a-3da1ecc389f0")
if err != nil {
log.Fatal(err)
}根据传入的删除对应移动推送凭证
push_credential_idDELETE /mobile_push_credentials/{push_credential_id}go
err := client.MobilePushCredentials.Delete(context.Background(), "0ccc7b76-4df3-4bca-a05a-3da1ecc389f0")
if err != nil {
log.Fatal(err)
}List all credentials
查询所有凭证
List all On-demand Credentials.
GET /telephony_credentialsgo
page, err := client.TelephonyCredentials.List(context.Background(), telnyx.TelephonyCredentialListParams{})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", page)Returns: (string), (boolean), (string), (string), (string), (string), (string), (string), (string), (string), (string)
created_atexpiredexpires_atidnamerecord_typeresource_idsip_passwordsip_usernameupdated_atuser_id查询所有按需凭证
GET /telephony_credentialsgo
page, err := client.TelephonyCredentials.List(context.Background(), telnyx.TelephonyCredentialListParams{})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", page)返回参数:(字符串)、(布尔值)、(字符串)、(字符串)、(字符串)、(字符串)、(字符串)、(字符串)、(字符串)、(字符串)、(字符串)
created_atexpiredexpires_atidnamerecord_typeresource_idsip_passwordsip_usernameupdated_atuser_idCreate a credential
创建凭证
Create a credential.
POST /telephony_credentialsconnection_idOptional: (string), (string), (string)
expires_atnametaggo
telephonyCredential, err := client.TelephonyCredentials.New(context.Background(), telnyx.TelephonyCredentialNewParams{
ConnectionID: "1234567890",
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", telephonyCredential.Data)Returns: (string), (boolean), (string), (string), (string), (string), (string), (string), (string), (string), (string)
created_atexpiredexpires_atidnamerecord_typeresource_idsip_passwordsip_usernameupdated_atuser_id创建一个凭证
POST /telephony_credentialsconnection_id可选参数:(字符串)、(字符串)、(字符串)
expires_atnametaggo
telephonyCredential, err := client.TelephonyCredentials.New(context.Background(), telnyx.TelephonyCredentialNewParams{
ConnectionID: "1234567890",
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", telephonyCredential.Data)返回参数:(字符串)、(布尔值)、(字符串)、(字符串)、(字符串)、(字符串)、(字符串)、(字符串)、(字符串)、(字符串)、(字符串)
created_atexpiredexpires_atidnamerecord_typeresource_idsip_passwordsip_usernameupdated_atuser_idGet a credential
获取单个凭证
Get the details of an existing On-demand Credential.
GET /telephony_credentials/{id}go
telephonyCredential, err := client.TelephonyCredentials.Get(context.Background(), "id")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", telephonyCredential.Data)Returns: (string), (boolean), (string), (string), (string), (string), (string), (string), (string), (string), (string)
created_atexpiredexpires_atidnamerecord_typeresource_idsip_passwordsip_usernameupdated_atuser_id获取现有按需凭证的详情
GET /telephony_credentials/{id}go
telephonyCredential, err := client.TelephonyCredentials.Get(context.Background(), "id")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", telephonyCredential.Data)返回参数:(字符串)、(布尔值)、(字符串)、(字符串)、(字符串)、(字符串)、(字符串)、(字符串)、(字符串)、(字符串)、(字符串)
created_atexpiredexpires_atidnamerecord_typeresource_idsip_passwordsip_usernameupdated_atuser_idUpdate a credential
更新凭证
Update an existing credential.
PATCH /telephony_credentials/{id}Optional: (string), (string), (string), (string)
connection_idexpires_atnametaggo
telephonyCredential, err := client.TelephonyCredentials.Update(
context.Background(),
"id",
telnyx.TelephonyCredentialUpdateParams{},
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", telephonyCredential.Data)Returns: (string), (boolean), (string), (string), (string), (string), (string), (string), (string), (string), (string)
created_atexpiredexpires_atidnamerecord_typeresource_idsip_passwordsip_usernameupdated_atuser_id更新现有凭证
PATCH /telephony_credentials/{id}可选参数:(字符串)、(字符串)、(字符串)、(字符串)
connection_idexpires_atnametaggo
telephonyCredential, err := client.TelephonyCredentials.Update(
context.Background(),
"id",
telnyx.TelephonyCredentialUpdateParams{},
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", telephonyCredential.Data)返回参数:(字符串)、(布尔值)、(字符串)、(字符串)、(字符串)、(字符串)、(字符串)、(字符串)、(字符串)、(字符串)、(字符串)
created_atexpiredexpires_atidnamerecord_typeresource_idsip_passwordsip_usernameupdated_atuser_idDelete a credential
删除凭证
Delete an existing credential.
DELETE /telephony_credentials/{id}go
telephonyCredential, err := client.TelephonyCredentials.Delete(context.Background(), "id")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", telephonyCredential.Data)Returns: (string), (boolean), (string), (string), (string), (string), (string), (string), (string), (string), (string)
created_atexpiredexpires_atidnamerecord_typeresource_idsip_passwordsip_usernameupdated_atuser_id删除现有凭证
DELETE /telephony_credentials/{id}go
telephonyCredential, err := client.TelephonyCredentials.Delete(context.Background(), "id")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", telephonyCredential.Data)返回参数:(字符串)、(布尔值)、(字符串)、(字符串)、(字符串)、(字符串)、(字符串)、(字符串)、(字符串)、(字符串)、(字符串)
created_atexpiredexpires_atidnamerecord_typeresource_idsip_passwordsip_usernameupdated_atuser_id