$npx -y skills add team-telnyx/ai --skill telnyx-messaging-hosted-pythonSet up hosted SMS numbers, toll-free verification, and RCS messaging. Use when migrating numbers or enabling rich messaging features. This skill provides Python SDK examples.
| 1 | <!-- Auto-generated from Telnyx OpenAPI specs. Do not edit. --> |
| 2 | |
| 3 | # Telnyx Messaging Hosted - Python |
| 4 | |
| 5 | ## Installation |
| 6 | |
| 7 | ```bash |
| 8 | pip install telnyx |
| 9 | ``` |
| 10 | |
| 11 | ## Setup |
| 12 | |
| 13 | ```python |
| 14 | import os |
| 15 | from telnyx import Telnyx |
| 16 | |
| 17 | client = Telnyx( |
| 18 | api_key=os.environ.get("TELNYX_API_KEY"), # This is the default and can be omitted |
| 19 | ) |
| 20 | ``` |
| 21 | |
| 22 | All examples below assume `client` is already initialized as shown above. |
| 23 | |
| 24 | ## Error Handling |
| 25 | |
| 26 | All API calls can fail with network errors, rate limits (429), validation errors (422), |
| 27 | or authentication errors (401). Always handle errors in production code: |
| 28 | |
| 29 | ```python |
| 30 | import telnyx |
| 31 | |
| 32 | try: |
| 33 | result = client.messages.send(to="+13125550001", from_="+13125550002", text="Hello") |
| 34 | except telnyx.APIConnectionError: |
| 35 | print("Network error — check connectivity and retry") |
| 36 | except telnyx.RateLimitError: |
| 37 | # 429: rate limited — wait and retry with exponential backoff |
| 38 | import time |
| 39 | time.sleep(1) # Check Retry-After header for actual delay |
| 40 | except telnyx.APIStatusError as e: |
| 41 | print(f"API error {e.status_code}: {e.message}") |
| 42 | if e.status_code == 422: |
| 43 | print("Validation error — check required fields and formats") |
| 44 | ``` |
| 45 | |
| 46 | Common error codes: `401` invalid API key, `403` insufficient permissions, |
| 47 | `404` resource not found, `422` validation error (check field formats), |
| 48 | `429` rate limited (retry with exponential backoff). |
| 49 | |
| 50 | ## Important Notes |
| 51 | |
| 52 | - **Phone numbers** must be in E.164 format (e.g., `+13125550001`). Include the `+` prefix and country code. No spaces, dashes, or parentheses. |
| 53 | - **Pagination:** List methods return an auto-paginating iterator. Use `for item in page_result:` to iterate through all pages automatically. |
| 54 | |
| 55 | ## Send an RCS message |
| 56 | |
| 57 | `POST /messages/rcs` — Required: `agent_id`, `to`, `messaging_profile_id`, `agent_message` |
| 58 | |
| 59 | Optional: `mms_fallback` (object), `sms_fallback` (object), `type` (enum: RCS), `webhook_url` (url) |
| 60 | |
| 61 | ```python |
| 62 | response = client.messages.rcs.send( |
| 63 | agent_id="Agent007", |
| 64 | agent_message={}, |
| 65 | messaging_profile_id="550e8400-e29b-41d4-a716-446655440000", |
| 66 | to="+13125551234", |
| 67 | ) |
| 68 | print(response.data) |
| 69 | ``` |
| 70 | |
| 71 | Returns: `body` (object), `direction` (string), `encoding` (string), `from` (object), `id` (string), `messaging_profile_id` (string), `organization_id` (string), `received_at` (date-time), `record_type` (string), `to` (array[object]), `type` (string), `wait_seconds` (float) |
| 72 | |
| 73 | ## Generate RCS deeplink |
| 74 | |
| 75 | Generate a deeplink URL that can be used to start an RCS conversation with a specific agent. |
| 76 | |
| 77 | `GET /messages/rcs/deeplinks/{agent_id}` |
| 78 | |
| 79 | ```python |
| 80 | response = client.messages.rcs.generate_deeplink( |
| 81 | agent_id="550e8400-e29b-41d4-a716-446655440000", |
| 82 | ) |
| 83 | print(response.data) |
| 84 | ``` |
| 85 | |
| 86 | Returns: `url` (string) |
| 87 | |
| 88 | ## List all RCS agents |
| 89 | |
| 90 | `GET /messaging/rcs/agents` |
| 91 | |
| 92 | ```python |
| 93 | page = client.messaging.rcs.agents.list() |
| 94 | page = page.data[0] |
| 95 | print(page.agent_id) |
| 96 | ``` |
| 97 | |
| 98 | Returns: `agent_id` (string), `agent_name` (string), `created_at` (date-time), `enabled` (boolean), `profile_id` (uuid), `updated_at` (date-time), `user_id` (string), `webhook_failover_url` (url), `webhook_url` (url) |
| 99 | |
| 100 | ## Retrieve an RCS agent |
| 101 | |
| 102 | `GET /messaging/rcs/agents/{id}` |
| 103 | |
| 104 | ```python |
| 105 | rcs_agent_response = client.messaging.rcs.agents.retrieve( |
| 106 | "id", |
| 107 | ) |
| 108 | print(rcs_agent_response.data) |
| 109 | ``` |
| 110 | |
| 111 | Returns: `agent_id` (string), `agent_name` (string), `created_at` (date-time), `enabled` (boolean), `profile_id` (uuid), `updated_at` (date-time), `user_id` (string), `webhook_failover_url` (url), `webhook_url` (url) |
| 112 | |
| 113 | ## Modify an RCS agent |
| 114 | |
| 115 | `PATCH /messaging/rcs/agents/{id}` |
| 116 | |
| 117 | Optional: `profile_id` (uuid), `webhook_failover_url` (url), `webhook_url` (url) |
| 118 | |
| 119 | ```python |
| 120 | rcs_agent_response = client.messaging.rcs.agents.update( |
| 121 | id="550e8400-e29b-41d4-a716-446655440000", |
| 122 | ) |
| 123 | print(rcs_agent_response.data) |
| 124 | ``` |
| 125 | |
| 126 | Returns: `agent_id` (string), `agent_name` (string), `created_at` (date-time), `enabled` (boolean), `profile_id` (uuid), `updated_at` (date-time), `user_id` (string), `webhook_failover_url` (url), `webhook_url` (url) |
| 127 | |
| 128 | ## Check RCS capabilities (batch) |
| 129 | |
| 130 | `POST /messaging/rcs/bulk_capabilities` — Required: `agent_id`, `phone_numbers` |
| 131 | |
| 132 | ```python |
| 133 | response = client.messaging.rcs.list_bulk_capabilities( |
| 134 | agent_id="TestAgent", |
| 135 | phone_numbers=["+13125551234"], |
| 136 | ) |
| 137 | print(response.data) |
| 138 | ``` |
| 139 | |
| 140 | Returns: `agent_id` (string), `agent_name` (string), `features` (array[string]), `phone_number` (string), `record_type` (enum: rcs.capabilities) |
| 141 | |
| 142 | ## Check RCS capabilities |
| 143 | |
| 144 | `GET /messaging/rcs/capabilities/{agent_id}/{phone_number}` |
| 145 | |
| 146 | ```python |
| 147 | response = client.messaging.rcs.retrieve_capabilities( |
| 148 | phone_number="+13125550001", |
| 149 | agent_id="550e8400-e29b-41d4-a716-446655440000", |
| 150 | ) |
| 151 | print(respon |