Set up your development environment
Before you start coding, did you complete the starting process? If so that means you have:
- Signed up for your Telnyx account
- Obtained your API Key so Telnyx can authenticate your integration’s API requests
What is next? If you are ready to interact with our APIs directly, please explore our full reference of APIs and check out our Postman collection. If you would like to utilize a server SDK to simplify API interactions, proceed forward with the language of your choice!
| Python | PHP | Node | Java | .NET | Ruby |
Python
Install Telnyx Python SDK
You'll need to ensure that you have Python installed on your computer. If Python isn’t installed, follow the official installation instructions for your operating system to install it. You can check this by running the following:
python --version
NoteAfter pasting the above content, Kindly check and remove any new line added
You’ll need to have pip installed. If pip isn’t installed, follow the official pip installation instructions for your operating system to install it. You can check this by running:
pip --version
NoteAfter pasting the above content, Kindly check and remove any new line added
Install the Telnyx Python SDK by running the following:
pip install telnyx
NoteAfter pasting the above content, Kindly check and remove any new line added
Check out the source on GitHub.
PHP
Install Telnyx PHP SDK
Make sure that you have at least PHP version 5.3 installed on your computer, as this is the minimum version of PHP that the SDK requires; we recommended you to always use the latest version of PHP. If PHP isn’t installed, follow the official installation instructions for your operating system to install it. You can check this by running the following:
php --version
NoteAfter pasting the above content, Kindly check and remove any new line added
Your terminal should reply with something like this:
PHP 7.2.24-0ubuntu0.18.04.1 (cli) (built: Oct 28 2019 12:07:07) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.24-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies
with Xdebug v2.6.0, Copyright (c) 2002-2018, by Derick Rethans
NoteAfter pasting the above content, Kindly check and remove any new line added
You’ll also need to have composer installed. Composer is PHP's package manager. While it's not a part of the PHP installation itself, it’s a de-facto standard that virtually every modern PHP library uses. If composer isn’t installed, follow the official composer installation instructions for your operating system to install it. You can check this by running:
composer --version
NoteAfter pasting the above content, Kindly check and remove any new line added
SDKs and other libraries are always installed for each project as a dependency and stored in a project-specific directory called vendor
that Composer manages.
There are two ways to add a package with Composer:
Using the Require Command
Create a new project directory or open an existing project directory in your terminal. Then, type the following command:
composer require telnyx/telnyx-php
NoteAfter pasting the above content, Kindly check and remove any new line added
This command installs the latest version of the SDK and automatically creates or updates the files composer.json
and composer.lock
.
Using composer.json
Create a new project directory or open an existing project directory. In this directory, create a file called composer.json
; if you already have this file, you can add the SDK to it; if you only have the SDK in it, the file should look like this:
{
"require": {
"telnyx/telnyx-php": "^0.0.1"
}
}
NoteAfter pasting the above content, Kindly check and remove any new line added
Keep in mind that you have to specify the version of the SDK in the file. You can see the available SDK versions on Packagist and learn more about different ways to specify versions, for example with ranges, in the Composer documentation.
After saving the file, open a terminal in the directory into which you've stored it and type this command:
composer install
NoteAfter pasting the above content, Kindly check and remove any new line added
Check out the source on GitHub.
Node
Install Telnyx Node SDK
You'll need to ensure that you have Node.js installed on your computer. If Node isn’t installed, follow the official installation instructions for your operating system to install it. You can check this by running the following:
node --version
NoteAfter pasting the above content, Kindly check and remove any new line added
You’ll need to have npm installed (npm is installed with Node.js). You can check this by running:
npm --version
NoteAfter pasting the above content, Kindly check and remove any new line added
Install the Telnyx Node SDK by running the following:
npm install telnyx
NoteAfter pasting the above content, Kindly check and remove any new line added
Check out the source on GitHub.
Java
Install Telnyx Java SDK
You'll need to ensure that you have at least Java JDK 1.8 installed on your computer. If Java isn’t installed, follow the installation instructions for your operating system. You can check this by running the following:
java -version
NoteAfter pasting the above content, Kindly check and remove any new line added
Check out the source on GitHub, or see Package on Maven Central.
Install the Telnyx SDK using Maven or Gradle.
The latest version can be found on our releases page.
Maven
Add the snip to your pom.xml file.
<dependency>
<groupId>com.telnyx.sdk</groupId>
<artifactId>telnyx</artifactId>
<version>{version}</version>
</dependency>
NoteAfter pasting the above content, Kindly check and remove any new line added
Gradle
Run the command
compile "com.telnyx.sdk:telnyx:{version}"
NoteAfter pasting the above content, Kindly check and remove any new line added
Send Message Example
Getting Started
- Sign up for a Telnyx Account
- Get your API Key
- Complete the messaging Learn and Build to:
- Get your first phone number
- Create your messaging profile
- Fill in the variables:
YOUR_TELNYX_NUMBER
,YOUR_TELNYX_API_KEY
,YOUR_MOBILE_NUMBER
in the code below
Demo below instantiates the Telnyx client
import com.telnyx.sdk.ApiClient;
import com.telnyx.sdk.ApiException;
import com.telnyx.sdk.Configuration;
import com.telnyx.sdk.auth.*;
import com.telnyx.sdk.model.*;
import com.telnyx.sdk.api.MessagesApi;
public class Example {
private static final String YOUR_TELNYX_NUMBER = "+19842550944";
private static final String YOUR_MOBILE_NUMBER = "+19198675309";
private static final String YOUR_TELNYX_API_KEY = "YOU_API_KEY";
public static void main(String[] args) {
ApiClient defaultClient = Configuration.getDefaultApiClient();
defaultClient.setBasePath("https://api.telnyx.com/v2");
// Configure HTTP bearer authorization: bearerAuth
HttpBearerAuth bearerAuth = (HttpBearerAuth) defaultClient.getAuthentication("bearerAuth");
bearerAuth.setBearerToken(YOUR_TELNYX_API_KEY);
MessagesApi apiInstance = new MessagesApi(defaultClient);
// CreateMessageRequest | Message payload
CreateMessageRequest createMessageRequest = new CreateMessageRequest()
.from(YOUR_TELNYX_NUMBER)
.to(YOUR_MOBILE_NUMBER)
.text("Hello From Telnyx");
try {
MessageResponse result = apiInstance.createMessage(createMessageRequest);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling MessagesApi#createMessage");
System.err.println("Status code: " + e.getCode());
System.err.println("Reason: " + e.getResponseBody());
System.err.println("Response headers: " + e.getResponseHeaders());
e.printStackTrace();
}
}
}
NoteAfter pasting the above content, Kindly check and remove any new line added
Webhook Verification
This example is dependent on the Bouncy Castle library. Visit our Webhook Verification Demo for more information.
[...]
@RestController
public class WebhookController {
private static final Dotenv dotenv = Dotenv.load();
private final String TELNYX_PUBLIC_KEY = dotenv.get("TELNYX_PUBLIC_KEY");
@PostMapping("/messaging/inbound")
public ResponseEntity<Void> webhook(@RequestBody String payload, @RequestHeader HttpHeaders headers) {
Security.addProvider(new BouncyCastleProvider());
String signature, timestamp;
try {
signature = headers.get("telnyx-signature-ed25519").get(0);
timestamp = headers.get("telnyx-timestamp").get(0);
}
catch (Exception e) {
return new ResponseEntity(HttpStatus.UNAUTHORIZED);
}
String signedPayload = timestamp + "|" + payload;
byte[] signedPayloadBytes = signedPayload.getBytes(StandardCharsets.UTF_8);
Ed25519PublicKeyParameters ed25519PublicKeyParameters = new Ed25519PublicKeyParameters(Base64.getDecoder().decode(TELNYX_PUBLIC_KEY), 0);
Signer verifier = new Ed25519Signer();
verifier.init(false, ed25519PublicKeyParameters);
verifier.update(signedPayloadBytes, 0, signedPayloadBytes.length);
boolean isVerified = verifier.verifySignature(Base64.getDecoder().decode(signature));
if(isVerified) {
return new ResponseEntity(HttpStatus.OK);
} else {
return new ResponseEntity(HttpStatus.UNAUTHORIZED);
}
}
}
NoteAfter pasting the above content, Kindly check and remove any new line added
.NET
Install Telnyx .NET SDK
You'll need to ensure that you have DotNet installed on your computer. If DotNet (core or framework) isn’t installed, follow the official installation instructions for your operating system to install it. You can check this by running the following:
Telnyx targets the .NET Standard 2.0. You should be running a supported verison of dotnet/Xamarin/Mono/Framework to leverage the SDK.
dotnet --version
NoteAfter pasting the above content, Kindly check and remove any new line added
Install the Telnyx.net SDK by running the following:
dotnet add package Telnyx.net
NoteAfter pasting the above content, Kindly check and remove any new line added
Check out the source on GitHub, or see Package on NuGet
Send Message Async Example
The C# SDK supports Task based API calls.
By updating the Main method to support async calls, we can build a quick example.
Demo below instantiates the Telnyx client and sends a "Hello, World!" message to the outbound phone number then prints the message ID.
Be sure to update the From
& To
numbers to your Telnyx number and destination phone number respectively.
using System;
using Telnyx;
using System.Threading.Tasks;
namespace demo_dotnet_telnyx
{
class Program
{
private static string TELNYX_API_KEY = System.Environment.GetEnvironmentVariable("TELNYX_API_KEY");
static async Task Main(string[] args)
{
Console.WriteLine("Hello World!");
TelnyxConfiguration.SetApiKey(TELNYX_API_KEY);
MessagingSenderIdService service = new MessagingSenderIdService();
NewMessagingSenderId options = new NewMessagingSenderId
{
From = "+19198675309", // alphanumeric sender id
To = "+19198675310",
Text = "Hello, World!"
};
MessagingSenderId messageResponse = await service.CreateAsync(options);
Console.WriteLine(messageResponse.Id);
}
}
}
NoteAfter pasting the above content, Kindly check and remove any new line added
Ruby
Install Telnyx Ruby SDK
You'll need to ensure that you have Ruby installed on your computer. If Ruby isn’t installed, follow the official installation instructions for your operating system to install it. You can check this by running the following:
ruby --version
NoteAfter pasting the above content, Kindly check and remove any new line added
You’ll need to have RubyGems installed. If RubyGems isn’t installed, follow the official RubyGems installation instructions for your operating system to install it. You can check this by running:
gem --version
NoteAfter pasting the above content, Kindly check and remove any new line added
Install the Telnyx Ruby Gem by running the following:
gem install telnyx
NoteAfter pasting the above content, Kindly check and remove any new line added
Check out the source on GitHub.