$npx -y skills add team-telnyx/ai --skill telnyx-messaging-hosted-javaSet up hosted SMS numbers, toll-free verification, and RCS messaging. Use when migrating numbers or enabling rich messaging features. This skill provides Java SDK examples.
| 1 | <!-- Auto-generated from Telnyx OpenAPI specs. Do not edit. --> |
| 2 | |
| 3 | # Telnyx Messaging Hosted - Java |
| 4 | |
| 5 | ## Installation |
| 6 | |
| 7 | ```text |
| 8 | <!-- Maven --> |
| 9 | <dependency> |
| 10 | <groupId>com.telnyx.sdk</groupId> |
| 11 | <artifactId>telnyx</artifactId> |
| 12 | <version>6.36.0</version> |
| 13 | </dependency> |
| 14 | |
| 15 | // Gradle |
| 16 | implementation("com.telnyx.sdk:telnyx:6.36.0") |
| 17 | ``` |
| 18 | |
| 19 | ## Setup |
| 20 | |
| 21 | ```java |
| 22 | import com.telnyx.sdk.client.TelnyxClient; |
| 23 | import com.telnyx.sdk.client.okhttp.TelnyxOkHttpClient; |
| 24 | |
| 25 | TelnyxClient client = TelnyxOkHttpClient.fromEnv(); |
| 26 | ``` |
| 27 | |
| 28 | All examples below assume `client` is already initialized as shown above. |
| 29 | |
| 30 | ## Error Handling |
| 31 | |
| 32 | All API calls can fail with network errors, rate limits (429), validation errors (422), |
| 33 | or authentication errors (401). Always handle errors in production code: |
| 34 | |
| 35 | ```java |
| 36 | import com.telnyx.sdk.errors.TelnyxServiceException; |
| 37 | |
| 38 | try { |
| 39 | var result = client.messages().send(params); |
| 40 | } catch (TelnyxServiceException e) { |
| 41 | System.err.println("API error " + e.statusCode() + ": " + e.getMessage()); |
| 42 | if (e.statusCode() == 422) { |
| 43 | System.err.println("Validation error — check required fields and formats"); |
| 44 | } else if (e.statusCode() == 429) { |
| 45 | // Rate limited — wait and retry with exponential backoff |
| 46 | Thread.sleep(1000); |
| 47 | } |
| 48 | } |
| 49 | ``` |
| 50 | |
| 51 | Common error codes: `401` invalid API key, `403` insufficient permissions, |
| 52 | `404` resource not found, `422` validation error (check field formats), |
| 53 | `429` rate limited (retry with exponential backoff). |
| 54 | |
| 55 | ## Important Notes |
| 56 | |
| 57 | - **Phone numbers** must be in E.164 format (e.g., `+13125550001`). Include the `+` prefix and country code. No spaces, dashes, or parentheses. |
| 58 | - **Pagination:** List methods return a page. Use `.autoPager()` for automatic iteration: `for (var item : page.autoPager()) { ... }`. For manual control, use `.hasNextPage()` and `.nextPage()`. |
| 59 | |
| 60 | ## Send an RCS message |
| 61 | |
| 62 | `POST /messages/rcs` — Required: `agent_id`, `to`, `messaging_profile_id`, `agent_message` |
| 63 | |
| 64 | Optional: `mms_fallback` (object), `sms_fallback` (object), `type` (enum: RCS), `webhook_url` (url) |
| 65 | |
| 66 | ```java |
| 67 | import com.telnyx.sdk.models.messages.RcsAgentMessage; |
| 68 | import com.telnyx.sdk.models.messages.rcs.RcSendParams; |
| 69 | import com.telnyx.sdk.models.messages.rcs.RcSendResponse; |
| 70 | |
| 71 | RcSendParams params = RcSendParams.builder() |
| 72 | .agentId("Agent007") |
| 73 | .agentMessage(RcsAgentMessage.builder().build()) |
| 74 | .messagingProfileId("550e8400-e29b-41d4-a716-446655440000") |
| 75 | .to("+13125551234") |
| 76 | .build(); |
| 77 | RcSendResponse response = client.messages().rcs().send(params); |
| 78 | ``` |
| 79 | |
| 80 | 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) |
| 81 | |
| 82 | ## Generate RCS deeplink |
| 83 | |
| 84 | Generate a deeplink URL that can be used to start an RCS conversation with a specific agent. |
| 85 | |
| 86 | `GET /messages/rcs/deeplinks/{agent_id}` |
| 87 | |
| 88 | ```java |
| 89 | import com.telnyx.sdk.models.messages.rcs.RcGenerateDeeplinkParams; |
| 90 | import com.telnyx.sdk.models.messages.rcs.RcGenerateDeeplinkResponse; |
| 91 | |
| 92 | RcGenerateDeeplinkResponse response = client.messages().rcs().generateDeeplink("550e8400-e29b-41d4-a716-446655440000"); |
| 93 | ``` |
| 94 | |
| 95 | Returns: `url` (string) |
| 96 | |
| 97 | ## List all RCS agents |
| 98 | |
| 99 | `GET /messaging/rcs/agents` |
| 100 | |
| 101 | ```java |
| 102 | import com.telnyx.sdk.models.messaging.rcs.agents.AgentListPage; |
| 103 | import com.telnyx.sdk.models.messaging.rcs.agents.AgentListParams; |
| 104 | |
| 105 | AgentListPage page = client.messaging().rcs().agents().list(); |
| 106 | ``` |
| 107 | |
| 108 | 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) |
| 109 | |
| 110 | ## Retrieve an RCS agent |
| 111 | |
| 112 | `GET /messaging/rcs/agents/{id}` |
| 113 | |
| 114 | ```java |
| 115 | import com.telnyx.sdk.models.messaging.rcs.agents.AgentRetrieveParams; |
| 116 | import com.telnyx.sdk.models.rcsagents.RcsAgentResponse; |
| 117 | |
| 118 | RcsAgentResponse rcsAgentResponse = client.messaging().rcs().agents().retrieve("550e8400-e29b-41d4-a716-446655440000"); |
| 119 | ``` |
| 120 | |
| 121 | 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) |
| 122 | |
| 123 | ## Modify an RCS agent |
| 124 | |
| 125 | `PATCH /messaging/rcs/agents/{id}` |
| 126 | |
| 127 | Optional: `profile_id` (uuid), `webhook_failover_url` (url), `webhook_url` (url) |
| 128 | |
| 129 | ```java |
| 130 | import com.telnyx.sdk.models.messaging.rcs.agents.AgentUpdateParams; |
| 131 | import com.telnyx.sdk.models.rcsagents.RcsAgentResponse; |
| 132 | |
| 133 | RcsAgentResponse rcsAgentResponse = client.messaging().rcs().agents().update("550e8400-e29b-41d4-a716- |