Loading...
Loading...
Implement OAuth 2.0 authentication flows for Telnyx API access. This skill provides Go SDK examples.
npx skill4agent add team-telnyx/skills telnyx-oauth-gogo get github.com/team-telnyx/telnyx-goimport (
"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")),
)clientimport "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")
}
}401403404422429ListAutoPaging()iter := client.Resource.ListAutoPaging(ctx, params); for iter.Next() { item := iter.Current() }GET /.well-known/oauth-authorization-server response, err := client.WellKnown.GetAuthorizationServerMetadata(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", response.AuthorizationEndpoint)authorization_endpointcode_challenge_methods_supportedgrant_types_supportedintrospection_endpointissuerjwks_uriregistration_endpointresponse_types_supportedscopes_supportedtoken_endpointtoken_endpoint_auth_methods_supportedGET /.well-known/oauth-protected-resource response, err := client.WellKnown.GetProtectedResourceMetadata(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", response.AuthorizationServers)authorization_serversresourceGET /oauth/authorize err := client.OAuth.GetAuthorize(context.Background(), telnyx.OAuthGetAuthorizeParams{
ClientID: "550e8400-e29b-41d4-a716-446655440000",
RedirectUri: "https://example.com",
ResponseType: telnyx.OAuthGetAuthorizeParamsResponseTypeCode,
})
if err != nil {
log.Fatal(err)
}GET /oauth/consent/{consent_token} oauth, err := client.OAuth.Get(context.Background(), "consent_token")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", oauth.Data)client_idlogo_urinamepolicy_uriredirect_urirequested_scopestos_uriverifiedPOST /oauth/grantsallowedconsent_token response, err := client.OAuth.Grants(context.Background(), telnyx.OAuthGrantsParams{
Allowed: true,
ConsentToken: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.example",
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", response.RedirectUri)redirect_uriPOST /oauth/introspecttoken response, err := client.OAuth.Introspect(context.Background(), telnyx.OAuthIntrospectParams{
Token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.example",
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", response.ClientID)activeaudclient_idexpiatissscopeGET /oauth/jwks response, err := client.OAuth.GetJwks(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", response.Keys)keysPOST /oauth/registerclient_namegrant_typeslogo_uripolicy_uriredirect_urisresponse_typesscopetoken_endpoint_auth_methodtos_uri response, err := client.OAuth.Register(context.Background(), telnyx.OAuthRegisterParams{})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", response.ClientID)client_idclient_id_issued_atclient_nameclient_secretgrant_typeslogo_uripolicy_uriredirect_urisresponse_typesscopetoken_endpoint_auth_methodtos_uriPOST /oauth/tokengrant_typeclient_idclient_secretcodecode_verifierredirect_urirefresh_tokenscope response, err := client.OAuth.Token(context.Background(), telnyx.OAuthTokenParams{
GrantType: telnyx.OAuthTokenParamsGrantTypeClientCredentials,
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", response.AccessToken)access_tokenexpires_inrefresh_tokenscopetoken_typeGET /oauth_clients page, err := client.OAuthClients.List(context.Background(), telnyx.OAuthClientListParams{})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", page)allowed_grant_typesallowed_scopesclient_idclient_secretclient_typecreated_atlogo_urinameorg_idpolicy_urirecord_typeredirect_urisrequire_pkcetos_uriupdated_atuser_idPOST /oauth_clientsnameallowed_scopesclient_typeallowed_grant_typeslogo_uripolicy_uriredirect_urisrequire_pkcetos_uri oauthClient, err := client.OAuthClients.New(context.Background(), telnyx.OAuthClientNewParams{
AllowedGrantTypes: []string{"client_credentials"},
AllowedScopes: []string{"admin"},
ClientType: telnyx.OAuthClientNewParamsClientTypePublic,
Name: "My OAuth client",
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", oauthClient.Data)allowed_grant_typesallowed_scopesclient_idclient_secretclient_typecreated_atlogo_urinameorg_idpolicy_urirecord_typeredirect_urisrequire_pkcetos_uriupdated_atuser_idGET /oauth_clients/{id} oauthClient, err := client.OAuthClients.Get(context.Background(), "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", oauthClient.Data)allowed_grant_typesallowed_scopesclient_idclient_secretclient_typecreated_atlogo_urinameorg_idpolicy_urirecord_typeredirect_urisrequire_pkcetos_uriupdated_atuser_idPUT /oauth_clients/{id}allowed_grant_typesallowed_scopeslogo_urinamepolicy_uriredirect_urisrequire_pkcetos_uri oauthClient, err := client.OAuthClients.Update(
context.Background(),
"182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e",
telnyx.OAuthClientUpdateParams{},
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", oauthClient.Data)allowed_grant_typesallowed_scopesclient_idclient_secretclient_typecreated_atlogo_urinameorg_idpolicy_urirecord_typeredirect_urisrequire_pkcetos_uriupdated_atuser_idDELETE /oauth_clients/{id} err := client.OAuthClients.Delete(context.Background(), "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e")
if err != nil {
log.Fatal(err)
}GET /oauth_grants page, err := client.OAuthGrants.List(context.Background(), telnyx.OAuthGrantListParams{})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", page)client_idcreated_atidlast_used_atrecord_typescopesGET /oauth_grants/{id} oauthGrant, err := client.OAuthGrants.Get(context.Background(), "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", oauthGrant.Data)client_idcreated_atidlast_used_atrecord_typescopesDELETE /oauth_grants/{id} oauthGrant, err := client.OAuthGrants.Delete(context.Background(), "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", oauthGrant.Data)client_idcreated_atidlast_used_atrecord_typescopes