Telnyx Development: Server-side SDKs — Full Documentation
Complete page content for Server-side SDKs (Development section) of the Telnyx developer docs (https://developers.telnyx.com). Root index: https://developers.telnyx.com/llms.txt · Lightweight index for this subsection: https://developers.telnyx.com/development/llms/development-server-side-sdks-llms-txt.md
Node.js
Source: https://developers.telnyx.com/development/sdk/node.md
Python
Source: https://developers.telnyx.com/development/sdk/python.md
PHP
Source: https://developers.telnyx.com/development/sdk/php.md
<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Credentials\CredentialProvider;
use Aws\Exception\AwsException;
$telnyxAPIKey = getenv('TELNYX_API_KEY');
if (!$telnyxAPIKey) {
die('TELNYX_API_KEY environment variable not set');
}
$region = 'us-central-1';
$endpoint = "https://{$region}.telnyxcloudstorage.com";
// 1. Initializing the AWS client with specific options
$s3Client = new S3Client([
'region' => $region,
'version' => 'latest',
'endpoint' => $endpoint,
'credentials' => [
'key' => $telnyxAPIKey,
'secret' => $telnyxAPIKey,
],
'use_path_style_endpoint' => true
]);
$bucketName = "test-bucket-" . $region . '-' . date('H-i') . '-' . rand(0, 1000000);
echo "Generated bucket name: " . $bucketName . PHP_EOL;
// 2. Create a bucket
try {
$s3Client->createBucket([
'Bucket' => $bucketName
]);
echo "Created bucket: {$bucketName}" . PHP_EOL;
} catch (AwsException $e) {
die("Unable to create bucket: " . $e->getMessage());
}
// 3. Upload two objects with random data
for ($i = 0; $i < 2; $i++) {
$content = random_bytes(1024 * 32); // 32KB of random data
$objName = "{$i}.txt";
try {
$s3Client->putObject([
'Bucket' => $bucketName,
'Key' => $objName,
'Body' => $content
]);
echo "Uploaded file ({$objName}) to bucket: {$bucketName}" . PHP_EOL;
} catch (AwsException $e) {
die("Unable to upload file ({$objName}): " . $e->getMessage());
}
}
// 4. List objects in the bucket
try {
$result = $s3Client->listObjects([
'Bucket' => $bucketName
]);
foreach ($result['Contents'] as $item) {
echo "Listed object: " . $item['Key'] . PHP_EOL;
}
} catch (AwsException $e) {
die("Unable to list objects: " . $e->getMessage());
}
// 5. Download the first object
try {
$result = $s3Client->getObject([
'Bucket' => $bucketName,
'Key' => '1.txt'
]);
$data = $result['Body']->getContents();
echo "Downloaded file size: " . strlen($data) . PHP_EOL;
} catch (AwsException $e) {
die("Unable to download object: " . $e->getMessage());
}
// 6. Create a presigned URL for the first file
$url = "https://api.telnyx.com/v2/storage/buckets/{$bucketName}/1.txt/presigned_url";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['TTL' => 30]));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $telnyxAPIKey,
'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpcode != 200) {
die("Unexpected status code: {$httpcode} | response: {$response}");
}
$presignedData = json_decode($response, true);
$presignedURL = $presignedData['data']['presigned_url'];
echo "Generated presigned URL: {$presignedURL}" . PHP_EOL;
// 7. Download the file using the presigned URL
$ch = curl_init($presignedURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo "Downloaded presigned URL data size: " . strlen($result) . PHP_EOL;
?>
Java
Source: https://developers.telnyx.com/development/sdk/java.md
Add Dependency
software.amazon.awssdk
s3
2.20.0 <!-- Or any 2.x version -->
Create S3 Bucket
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.CreateBucketRequest;
public class CreateBucket {
public static void main(String[] args) {
String bucketName = "--your-bucket-name--";
Region region = Region.US_EAST_1;
String telnyxUrl = "https://us-central-1.telnyxcloudstorage.com";
String telnyxApiKey = "-- api key --";
// Create an S3 client
S3Client s3 = S3Client.builder()
.region(region)
.endpointOverride(URI.create(telnyxUrl))
// Only perform CRC checks `when_required`
.requestChecksumCalculation(RequestChecksumCalculation.WHEN_REQUIRED)
.responseChecksumValidation(ResponseChecksumValidation.WHEN_REQUIRED)
.credentialsProvider(
StaticCredentialsProvider.create(AwsBasicCredentials.create(telnyxApiKey, "does not matter")))
.build();
// create bucket
CreateBucketRequest createBucketRequest = CreateBucketRequest.builder()
.bucket(bucketName)
.build();
s3.createBucket(createBucketRequest);
System.out.println("Bucket created successfully: " + bucketName);
// Close the S3 client
s3.close();
}
}
Upload an Object
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import java.net.URI;
import java.nio.file.Paths;
public class UploadObjectToS3 {
public static void main(String[] args) {
String bucketName = "--your-bucket-name--";
String keyName = "your-object-key";
String filePath = "--path to file for upload--";
Region region = Region.US_EAST_1;
String telnyxUrl = "https://us-central-1.telnyxcloudstorage.com";
String telnyxApiKey = "--your api key --";
// Create an S3 client
S3Client s3 = S3Client.builder()
.region(region)
.endpointOverride(URI.create(telnyxUrl))
.credentialsProvider(
StaticCredentialsProvider.create(AwsBasicCredentials.create(telnyxApiKey, "does not matter")))
.build();
// upload object
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
.bucket(bucketName)
.key(keyName)
.build();
// Upload the file to S3
s3.putObject(putObjectRequest, RequestBody.fromFile(Paths.get(filePath)));
// Close the S3 client
s3.close();
}
}
List Objects
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
import software.amazon.awssdk.services.s3.model.ListObjectsV2Response;
import software.amazon.awssdk.services.s3.model.S3Object;
import java.net.URI;
public class ListObjects {
public static void main(String[] args) {
String bucketName = "--your-bucket-name--";
Region region = Region.US_EAST_1;
String telnyxUrl = "https://us-central-1.telnyxcloudstorage.com";
String telnyxApiKey = "--your api key --";
// Create an S3 client
S3Client s3 = S3Client.builder()
.region(region)
.endpointOverride(URI.create(telnyxUrl))
.credentialsProvider(
StaticCredentialsProvider.create(AwsBasicCredentials.create(telnyxApiKey, "does not matter")))
.build();
// Create a ListObjectsV2Request
ListObjectsV2Request listObjectsRequest = ListObjectsV2Request.builder()
.bucket(bucketName)
.build();
// Get the list of objects in the bucket
ListObjectsV2Response listObjectsResponse = s3.listObjectsV2(listObjectsRequest);
for (S3Object s3Object : listObjectsResponse.contents()) {
System.out.println( s3Object.key());
}
// Close the S3 client
s3.close();
}
}
Download Object
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.ResponseBytes;
import software.amazon.awssdk.core.sync.ResponseTransformer;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
public class DownloadObject {
public static void main(String[] args) throws IOException {
String bucketName = "--your-bucket-name--";
Region region = Region.US_EAST_1;
String telnyxUrl = "https://us-central-1.telnyxcloudstorage.com";
String telnyxApiKey = "--your api key --";
String keyName = "your-object-key";
S3Client s3 = S3Client.builder()
.region(region)
.endpointOverride(URI.create(telnyxUrl))
.credentialsProvider(
StaticCredentialsProvider.create(AwsBasicCredentials.create(telnyxApiKey, "does not matter")))
.build();
// Create a GetObjectRequest
GetObjectRequest getObjectRequest = GetObjectRequest.builder()
.bucket(bucketName)
.key(keyName)
.build();
// Download the object and transform the response to a byte array
ResponseBytes objectBytes = s3.getObject(getObjectRequest, ResponseTransformer.toBytes());
// Write the file to the specified path
File downloadedFile = new File("-- path to where to save the file --");
try (FileOutputStream fos = new FileOutputStream(downloadedFile)) {
fos.write(objectBytes.asByteArray());
System.out.println("File downloaded successfully to -- path to where to save the file --");
}
// Close the S3 client
s3.close();
}
}
Generate Presigned URLs for Upload and Download
In order for this part to work, we will need to add json decoding library and http client. Any libraries will do, but for this example we picked: gson and okhttp3.
com.squareup.okhttp3
okhttp
4.9.2
com.google.code.gson
gson
2.8.7
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import okhttp3.*;
import java.io.IOException;
import java.util.Map;
public class GeneratePresignedURLAndDownloadObject {
public static void main(String[] args) throws IOException {
OkHttpClient httpClient = new OkHttpClient();
Gson gson = new Gson();
String presignedUrlRequestJson = gson.toJson(Map.of("TTL", 30));
RequestBody presignedUrlRequestBody = RequestBody.create(MediaType.parse("application/json"), presignedUrlRequestJson);
Request presignedUrlRequest = new Request.Builder()
.url("https://api.telnyx.com/v2/storage/buckets/-- name of the bucket --/--name of the object--/presigned_url")
.header("Authorization", "Bearer --your api key---")
.post(presignedUrlRequestBody)
.build();
try (Response response = httpClient.newCall(presignedUrlRequest).execute()) {
if (!response.isSuccessful()) {
throw new IOException("Failed to create presigned URL: " + response);
}
String responseBody = response.body().string();
Map responseBodyMap = gson.fromJson(responseBody, new TypeToken>() {}.getType());
String presignedUrl = ((Map) responseBodyMap.get("data")).get("presigned_url");
System.out.println("Presigned URL: " + presignedUrl);
// 6. Download the file using the presigned URL
Request downloadRequest = new Request.Builder()
.url(presignedUrl)
.build();
try (Response downloadResponse = httpClient.newCall(downloadRequest).execute()) {
if (!downloadResponse.isSuccessful()) {
throw new IOException("Failed to download file using presigned URL: " + downloadResponse);
}
System.out.println("Downloaded via presigned URL: " + downloadResponse.body().string());
}
}
}
}
Ruby
Source: https://developers.telnyx.com/development/sdk/ruby.md
Go
Source: https://developers.telnyx.com/development/sdk/golang.md