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-go
bash
go get github.com/team-telnyx/telnyx-go

Setup

初始化设置

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
client
is already initialized as shown above.
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")),
)
以下所有示例均假设
client
已按上述方式完成初始化。

Error 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:
401
invalid API key,
403
insufficient permissions,
404
resource not found,
422
validation error (check field formats),
429
rate limited (retry with exponential backoff).
所有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")
  }
}
常见错误码:
401
API密钥无效,
403
权限不足,
404
资源未找到,
422
校验错误(请检查字段格式),
429
触发速率限制(请使用指数退避策略重试)。

Important Notes

重要说明

  • Pagination: Use
    ListAutoPaging()
    for automatic iteration:
    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_credentials
go
	page, err := client.MobilePushCredentials.List(context.Background(), telnyx.MobilePushCredentialListParams{})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%+v\n", page)
Returns:
alias
(string),
certificate
(string),
created_at
(date-time),
id
(string),
private_key
(string),
project_account_json_file
(object),
record_type
(string),
type
(string),
updated_at
(date-time)
GET /mobile_push_credentials
go
	page, err := client.MobilePushCredentials.List(context.Background(), telnyx.MobilePushCredentialListParams{})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%+v\n", page)
返回参数:
alias
(字符串)、
certificate
(字符串)、
created_at
(日期时间)、
id
(字符串)、
private_key
(字符串)、
project_account_json_file
(对象)、
record_type
(字符串)、
type
(字符串)、
updated_at
(日期时间)

Creates a new mobile push credential

创建新的移动推送凭证

POST /mobile_push_credentials
— Required:
type
,
certificate
,
private_key
,
alias
go
	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:
alias
(string),
certificate
(string),
created_at
(date-time),
id
(string),
private_key
(string),
project_account_json_file
(object),
record_type
(string),
type
(string),
updated_at
(date-time)
POST /mobile_push_credentials
— 必填参数:
type
certificate
private_key
alias
go
	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)
返回参数:
alias
(字符串)、
certificate
(字符串)、
created_at
(日期时间)、
id
(字符串)、
private_key
(字符串)、
project_account_json_file
(对象)、
record_type
(字符串)、
type
(字符串)、
updated_at
(日期时间)

Retrieves a mobile push credential

获取单个移动推送凭证

Retrieves mobile push credential based on the given
push_credential_id
GET /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:
alias
(string),
certificate
(string),
created_at
(date-time),
id
(string),
private_key
(string),
project_account_json_file
(object),
record_type
(string),
type
(string),
updated_at
(date-time)
根据传入的
push_credential_id
获取对应移动推送凭证
GET /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)
返回参数:
alias
(字符串)、
certificate
(字符串)、
created_at
(日期时间)、
id
(字符串)、
private_key
(字符串)、
project_account_json_file
(对象)、
record_type
(字符串)、
type
(字符串)、
updated_at
(日期时间)

Deletes a mobile push credential

删除移动推送凭证

Deletes a mobile push credential based on the given
push_credential_id
DELETE /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_id
删除对应移动推送凭证
DELETE /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_credentials
go
	page, err := client.TelephonyCredentials.List(context.Background(), telnyx.TelephonyCredentialListParams{})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%+v\n", page)
Returns:
created_at
(string),
expired
(boolean),
expires_at
(string),
id
(string),
name
(string),
record_type
(string),
resource_id
(string),
sip_password
(string),
sip_username
(string),
updated_at
(string),
user_id
(string)
查询所有按需凭证
GET /telephony_credentials
go
	page, err := client.TelephonyCredentials.List(context.Background(), telnyx.TelephonyCredentialListParams{})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%+v\n", page)
返回参数:
created_at
(字符串)、
expired
(布尔值)、
expires_at
(字符串)、
id
(字符串)、
name
(字符串)、
record_type
(字符串)、
resource_id
(字符串)、
sip_password
(字符串)、
sip_username
(字符串)、
updated_at
(字符串)、
user_id
(字符串)

Create a credential

创建凭证

Create a credential.
POST /telephony_credentials
— Required:
connection_id
Optional:
expires_at
(string),
name
(string),
tag
(string)
go
	telephonyCredential, err := client.TelephonyCredentials.New(context.Background(), telnyx.TelephonyCredentialNewParams{
		ConnectionID: "1234567890",
	})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%+v\n", telephonyCredential.Data)
Returns:
created_at
(string),
expired
(boolean),
expires_at
(string),
id
(string),
name
(string),
record_type
(string),
resource_id
(string),
sip_password
(string),
sip_username
(string),
updated_at
(string),
user_id
(string)
创建一个凭证
POST /telephony_credentials
— 必填参数:
connection_id
可选参数:
expires_at
(字符串)、
name
(字符串)、
tag
(字符串)
go
	telephonyCredential, err := client.TelephonyCredentials.New(context.Background(), telnyx.TelephonyCredentialNewParams{
		ConnectionID: "1234567890",
	})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%+v\n", telephonyCredential.Data)
返回参数:
created_at
(字符串)、
expired
(布尔值)、
expires_at
(字符串)、
id
(字符串)、
name
(字符串)、
record_type
(字符串)、
resource_id
(字符串)、
sip_password
(字符串)、
sip_username
(字符串)、
updated_at
(字符串)、
user_id
(字符串)

Get 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:
created_at
(string),
expired
(boolean),
expires_at
(string),
id
(string),
name
(string),
record_type
(string),
resource_id
(string),
sip_password
(string),
sip_username
(string),
updated_at
(string),
user_id
(string)
获取现有按需凭证的详情
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_at
(字符串)、
expired
(布尔值)、
expires_at
(字符串)、
id
(字符串)、
name
(字符串)、
record_type
(字符串)、
resource_id
(字符串)、
sip_password
(字符串)、
sip_username
(字符串)、
updated_at
(字符串)、
user_id
(字符串)

Update a credential

更新凭证

Update an existing credential.
PATCH /telephony_credentials/{id}
Optional:
connection_id
(string),
expires_at
(string),
name
(string),
tag
(string)
go
	telephonyCredential, err := client.TelephonyCredentials.Update(
		context.Background(),
		"id",
		telnyx.TelephonyCredentialUpdateParams{},
	)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%+v\n", telephonyCredential.Data)
Returns:
created_at
(string),
expired
(boolean),
expires_at
(string),
id
(string),
name
(string),
record_type
(string),
resource_id
(string),
sip_password
(string),
sip_username
(string),
updated_at
(string),
user_id
(string)
更新现有凭证
PATCH /telephony_credentials/{id}
可选参数:
connection_id
(字符串)、
expires_at
(字符串)、
name
(字符串)、
tag
(字符串)
go
	telephonyCredential, err := client.TelephonyCredentials.Update(
		context.Background(),
		"id",
		telnyx.TelephonyCredentialUpdateParams{},
	)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%+v\n", telephonyCredential.Data)
返回参数:
created_at
(字符串)、
expired
(布尔值)、
expires_at
(字符串)、
id
(字符串)、
name
(字符串)、
record_type
(字符串)、
resource_id
(字符串)、
sip_password
(字符串)、
sip_username
(字符串)、
updated_at
(字符串)、
user_id
(字符串)

Delete 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:
created_at
(string),
expired
(boolean),
expires_at
(string),
id
(string),
name
(string),
record_type
(string),
resource_id
(string),
sip_password
(string),
sip_username
(string),
updated_at
(string),
user_id
(string)
删除现有凭证
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_at
(字符串)、
expired
(布尔值)、
expires_at
(字符串)、
id
(字符串)、
name
(字符串)、
record_type
(字符串)、
resource_id
(字符串)、
sip_password
(字符串)、
sip_username
(字符串)、
updated_at
(字符串)、
user_id
(字符串)