Messaging • Last Updated 9/20/2023

Twexit API Tutorial: Python SDK

Ready for hassle free migration? In this tutorial, we’ll cover how to migrate services using the Twexit API in Python.

By Risa Takenaka

Twexit Tutorial Python SDK

There are many reasons to switch from Twilio to Telnyx -- better G2 reviews, lower prices for our programmable SMS API, and better technical support around the clock for all of our customers -- just to name a few. You can check out this article for a comprehensive comparison of Telnyx and Twilio across all products and offerings.

We also know that making the switch from your current SMS API provider is no easy task. That’s why we’ve built the Twexit API in different SDKs, including Python, so that you can migrate to Telnyx for your messaging needs with minimal changes to your existing Twilio messaging code.

Easy Twilio Migration

Ready for hassle free migration? You’ve come to the right place.

In this tutorial, we’ll be covering how to migrate services using the Twexit API in Python SDK. First, let’s start with a brief overview of the steps involved:

  1. Setting up Mission Control Portal
  2. Creating a messaging profile
  3. Finding the organization ID
  4. Sending a Message with the Twexit Python SDK

1. Setting up Mission Control Portal

First, sign up or log into your Telnyx portal account. (link) Next, you’ll want to buy active SMS enabled numbers, so click on the “Numbers” section on the left hand side of the portal. You can search for numbers with a certain area code as well as other specifications. Pick a number, add it to your cart, and go ahead and place the order.

Setting Up Mission Control Portal

If you already have active numbers from a different provider, you can go ahead and port numbers onto our platform with FastPort.

2.Creating a messaging profile

Now that you have active numbers on the Telnyx platform, go ahead and click “Messaging” so that we can create and assign a messaging profile to that number. Create your first profile by giving it a name, and selecting the Twexit API.

Creating a Messaging Profile

Next, go back to the numbers section and select your messaging profile in the dropdown.

Selecting messaging profile

3. Finding the organization ID

Next, find your organization ID by running this curl command:

      --url https://api.telnyx.com/v2/whoami \
      --header 'Authorization: Bearer ${auth_v2_api_key}'

This endpoint will respond with your organization ID, in this format:

{
  "data": {
    "organization_id": "ORGANIZATION_ID",
    "user_id": "USER_ID"
  }
}

4. Sending a message

Copy this code, filling in your Telnyx number, organization ID, and auth v2 API key, and changing your current Twilio URL to Telnyx.

import requests
from requests.auth import HTTPBasicAUth
url = f"https://api.telnyx.com/2010-04-01/Accounts/{organization_id}/Messages.json"
auth = HTTPBasicAuth(organization_id, auth_v2_api_key)
data = {
  "From": "+13125550000", # Your Telnyx number
  "To": "+13125550001",
  "Body": "Hello, World!"
}
response = requests.request('POST', url, auth=auth, data=data)
print(response.text)

The response will contain: shell script { "sid": "40317159-f30d-4940-b751-9a6e81727249", "date_created": "Wed, 08 Apr 2020 13:20:33 +0000", "date_updated": "Wed, 08 Apr 2020 13:20:33 +0000", "date_sent": null, "account_sid": "${account_id}", "to": "+13125550000", "from": "+13125550001", "messaging_service_sid": "40017155-44c7-4d18-8fa5-332c1a776db5", "body": "Hello, World!", "status": "queued", "num_segments": "1", "num_media": "0", "direction": "outbound-api", "api_version": "2010-04-01", "price": null, "price_unit": "USD", "error_code": null, "error_message": null, "uri": "/2010-04-01/Accounts/{organization_id}/Messages/40317159-f30d-4940-b751-9a6e81727249.json", "subresource_uris": { "media": "/2010-04-01/Accounts/{organization_id}/Messages/40317159-f30d-4940-b751-9a6e81727249/Media.json" } }

After these three configuration changes and the API change -- 4 lines of code -- you’ll be on your way to sending your first message with Telnyx using the Python SDK!

Share on Social

Sign up for emails of our latest articles and news

Related articles

Sign up and start building.