The Telnyx Python library provides convenient access to the Telnyx API from applications written in the Python language. It includes a pre-defined set of classes for API resources that initialize themselves dynamically from API responses which makes it compatible with a wide range of versions of the Telnyx API.
See the API Reference and the Setup Guides.
You don't need this source code unless you want to modify the package. If you just want to use the package, just run:
pip install --upgrade telnyx
Install from source with:
python setup.py install
The telnyx
package is distributed as a wheel (pre-compiled package) for easy
installation. The wheel is built only for x86/x86_64 processors. When
installing the package on a different architecture, like ARM, the pip
installer will fall back to installing from source. As a result, you will
need to ensure you have the additional dependencies installed. This will
affect you if you're using a Raspberry Pi, for example.
For ARM specifically, as an alternative to a source install, you could look into using https://www.piwheels.org/ for ARM compiled wheels.
- Python 3.8+ (PyPy supported)
- build-essentials (gcc, make)
- python-dev
- libffi-dev
These packages are listed as they are named on Ubuntu.
The library needs to be configured with your account's API Key which is
available in your Telnyx Dashboard. Set telnyx.api_key
to its
value:
import telnyx
telnyx.api_key = "KEY01234_yoursecretkey"
# Retrieve single Messaging Profile
telnyx.MessagingProfile.retrieve("123")
# List Messaging Profiles
profiles = telnyx.MessagingProfile.list()
# Retrieve next page of list results
profiles.next_page()
# Loop over all page results
for page in profiles.auto_paging_iter():
print(page)
You can read more about our API Keys here.
For apps that need to use multiple keys during the lifetime of a process, it's also possible to set a per-request key and/or account:
import telnyx
# list messaging profiles
telnyx.MessagingProfile.list(
api_key="super-secret...",
)
# retrieve single messaging profile
telnyx.MessagingProfile.retrieve(
"123",
api_key="other-secret...",
)
The library can be configured to use urlfetch
, requests
, pycurl
, or
urllib2
with telnyx.default_http_client
:
client = telnyx.http_client.UrlFetchClient()
client = telnyx.http_client.RequestsClient()
client = telnyx.http_client.PycurlClient()
client = telnyx.http_client.Urllib2Client()
telnyx.default_http_client = client
Without a configured client, by default the library will attempt to load
libraries in the order above (i.e. urlfetch
is preferred with urllib2
used
as a last resort). We usually recommend that people use requests
.
A proxy can be configured with telnyx.proxy
:
telnyx.proxy = "https://user:[email protected]:1234"
Number of automatic retries on requests that fail due to an intermittent network problem can be configured:
telnyx.max_network_retries = 2
The Telnyx API includes from
as an attribute that can be set on messages.
from
is also a reserved word in Python. If you would like to use keyword
arguments where an argument is a reserved word you can add the suffix _
e.g.
telnyx.Message.create(
to="+18665550001",
from_="+18445550001",
text="Foo"
)
The argument will be automatically rewritten to from
in the keyword arguments dict.
Pro Tip: You can alternatively unpack a dictionary like so:
message = { "from": "+18445550001", "to": "+18665550001", "text": "Foo", } telnyx.Message.create(**message)
The library can be configured to emit logging that will give you better insight
into what it's doing. The info
logging level is usually most appropriate for
production use, but debug
is also available for more verbosity.
There are a few options for enabling it:
-
Set the environment variable
TELNYX_LOG
to the valuedebug
orinfo
$ export TELNYX_LOG=debug
-
Set
telnyx.log
:import telnyx telnyx.log = 'debug'
-
Enable it through Python's logging module:
import logging logging.basicConfig() logging.getLogger('telnyx').setLevel(logging.DEBUG)
If you're writing a plugin that uses the library, we'd appreciate it if you
identified using telnyx.set_app_info()
:
telnyx.set_app_info("MyAwesomePlugin", version="1.2.34", url="https://myawesomeplugin.info")
This information is passed along when the library makes calls to the Telnyx API.
The test suite depends on telnyx-mock, so make sure to fetch and run it from a background terminal (telnyx-mock's README also contains instructions for installing via Homebrew and other methods):
go get -u github.com/team-telnyx/telnyx-mock
telnyx-mock
Install pipenv, then install all dependencies for the project:
pipenv install --dev
Run all tests on all supported Python versions:
make test
Run all tests for a specific Python version (modify -e
according to your Python target):
pipenv run tox -e py38
Run all tests in a single file:
pipenv run tox -e py38 -- tests/api_resources/abstract/test_updateable_api_resource.py
Run a single test suite:
pipenv run tox -e py38 -- tests/api_resources/abstract/test_updateable_api_resource.py::TestUpdateableAPIResource
Run a single test:
pipenv run tox -e py38 -- tests/api_resources/abstract/test_updateable_api_resource.py::TestUpdateableAPIResource::test_save
Run the linter with:
make lint
The library uses Black for code formatting. Code must be formatted with Black before PRs are submitted, otherwise CI will fail. Run the formatter with:
make fmt
-
Define a class for the object that the endpoint interacts with under
telnyx/api_resources/
. The path name singularized should typically match the record type of the object returned e.g./v2/available_phone_numbers
returns a list of objects with the record_typeavailable_phone_number
. Inherit from the classes that define the behavior available on the endpoint,one or more ofCreateableAPIResource
,DeletableAPIResource
,ListableAPIResource
,UpdateableAPIResource
. -
Import your class in
telnyx/api_resources/__init__.py
. -
Add your new class to the
OBJECT_CLASSES
block intelnyx/util.py
. -
Add tests for your new class under
tests/api_resources/
.
- Update version in
setup.py
(in thesetup()
call, theversion
kwarg)telnyx/__init__.py
(the__version__
string)
- Create new branch, add changes, commit, and push
- Ensure commit passes tests in Travis
- Tag that commit with
git tag -a v{VERSION} -m "Release v{VERSION}"
, and push the taggit push --follow-tags
- Ensure checked out copy is entirely clean (best to create a new environment...)
make dists
- If you haven't done it before, download the upload API keys from LastPass (search for "pypi") and put the contents between "PYPIRC FILE" tags into
~/.pypirc-telnyx
. make testupload
, check that it looks OK on PyPI and that it's installable viapip
.make liveupload
, repeat checks for live version.- Ta-da.
The contributors and maintainers of Telnyx Python would like to extend their deep gratitude to the authors of Stripe Python, upon which this project is based. Thank you for developing such elegant, usable, and extensible code and for sharing it with the community.