Telnyx Python Library
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
- breaking Malicious versions 4.87.1 and 4.87.2 were briefly published to PyPI, containing malicious code. If installed between 03:51 UTC and 10:13 UTC on March 27, 2026, your system may have been compromised.
- gotcha Telnyx recommends storing API keys as environment variables (`TELNYX_API_KEY`) and explicitly passing them to the client constructor, rather than setting `telnyx.api_key` globally, especially for applications requiring multiple keys or robust security practices.
- gotcha Webhook signature verification now defaults to Ed25519. If you are handling webhooks, it's strongly recommended to verify signatures to ensure authenticity.
- gotcha The library is updated very frequently (sometimes daily), and while it generally follows SemVer, some backwards-incompatible changes may be released as minor versions if they affect static types, internal components, or are not expected to impact the majority of users.
- gotcha The keyword `from` is a reserved word in Python but often an API parameter (e.g., in messaging). The library handles this by allowing `from_` as a keyword argument for such parameters.
- gotcha When sending file data, ensure it is passed as a single parameter. An earlier bug caused issues if file data was incorrectly structured.
Install
-
pip install telnyx -
pip install telnyx[aiohttp]
Imports
- Telnyx
from telnyx import Telnyx
- AsyncTelnyx
from telnyx import AsyncTelnyx
Quickstart
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}")