Telnyx Python Library

4.105.1 · active · verified Sun Apr 12

The Telnyx Python library provides convenient access to the Telnyx REST API from any Python 3.9+ application. It includes type definitions for all request parameters and response fields, offering both synchronous and asynchronous clients powered by `httpx`. The library is actively maintained with frequent updates, often daily, reflecting API changes and new features.

Warnings

Install

Imports

Quickstart

Initializes the Telnyx client using an API key from an environment variable and attempts to list messaging profiles to verify connectivity and authentication. Replace `YOUR_TELNYX_API_KEY_HERE` with an actual key or ensure the `TELNYX_API_KEY` environment variable is set.

import os
import telnyx

# It is recommended to set TELNYX_API_KEY as an environment variable
# export TELNYX_API_KEY="YOUR_TELNYX_API_KEY"
api_key = os.environ.get("TELNYX_API_KEY", "YOUR_TELNYX_API_KEY_HERE")

if not api_key or api_key == "YOUR_TELNYX_API_KEY_HERE":
    print("Warning: TELNYX_API_KEY environment variable not set or is placeholder.")
    print("Please set TELNYX_API_KEY or replace 'YOUR_TELNYX_API_KEY_HERE' with your actual API key.")
    # In a production environment, you would likely exit or raise an error.

client = telnyx.Telnyx(api_key=api_key)

try:
    # Example: List messaging profiles to verify connectivity and API key
    print("Attempting to list messaging profiles...")
    messaging_profiles = client.messaging_profiles.list()
    print(f"Successfully retrieved {len(messaging_profiles.data)} messaging profiles.")
    if messaging_profiles.data:
        print(f"First profile ID: {messaging_profiles.data[0].id}")

except telnyx.APIStatusError as e:
    print(f"Telnyx API Error: {e.status_code} - {e.response.json()}")
    print("Please ensure your API key is valid and has the necessary permissions.")
except telnyx.APIConnectionError as e:
    print(f"Telnyx Connection Error: {e}")
    print("Please check your network connection.")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →