SMS API

GetMedia

This method retrieves the binary representation of a media file that was added to your license key. This is binary data that is encoded as a string of base-64 digits.

Endpoint

GET:
https://messaging.esendex.us/Messaging.svc/GetMedia?LicenseKey={LICENSEKEY}&MediaId={MEDIAID}

Syntax

GetMedia(LicenseKey, MediaID)

Request Parameters

Parameter NameDescriptionData TypeRequiredSample Value
LicenseKeyYour license key.GUIDTrue00000000-0000-0000-0000-000000000000
MediaIDUnique ID of the media file.LongTrue12345678

Response

Returns: String

Sample:
iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAIAAABLbSncAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAGdYAABnWARjRyu0AAAASSURBVBhXY3gro4IVDSkJGRUAmbtLQUb26xsAAAAASUVORK5CYII=

Code Samples

You can use any programming language you want with our API, as long as it can make a REST or SOAP call. Here are examples for some of the most common platforms.

C#

// https://messaging.esendex.us/Messaging.svc?wsdl was added as a Service Reference and given the name WSDL

using WSDL;

var client = new MessagingClient(MessagingClient.EndpointConfiguration.mms2wsHttpBindingSecure);

// Let's create a media file.
var media = await client.CreateMediaFromUrlAsync(new CreateMediaUrlRequest
{
    Filename = "MyPhoto.jpg",
    LicenseKey = YOUR_LICENSE_KEY,
    Tags = new[] { "Photo" },
    Url = "https://esendex.us/img/docs/example.jpg"
});

if (media is null)
{
    Console.WriteLine("The media could not be created.");
    return;
}

// We can fetch the media by ID at any time. Let's do it now.
var data = await client.GetMediaAsync(YOUR_LICENSE_KEY, media.MediaId);

// The data is a string of base-64 digits. Let's save the file to our computer.
var path = @"C:\NewPhoto.jpg";
var bytes = Convert.FromBase64String(data);
File.WriteAllBytes(path, bytes);
Console.WriteLine("Image saved");

Java

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public final class GetMedia {
    public static void main(String[] args) {
        try {
            URL url = new URL("https://messaging.esendex.us/Messaging.svc/GetMedia?"
                    + "LicenseKey=00000000-0000-0000-0000-000000000000"
                    + "&MediaID=11777");
            try {
                InputStream in = url.openStream();
                StreamSource source = new StreamSource(in);
                printResult(source);
            } catch (java.io.IOException e) {
            }
        } catch (MalformedURLException e) {
        }
    }

    private static void printResult(Source source) {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            StreamResult sr = new StreamResult(bos);
            Transformer trans = TransformerFactory.newInstance().newTransformer();
            Properties oprops = new Properties();
            oprops.put(OutputKeys.OMIT_XML_DECLARATION, "yes");
            trans.setOutputProperties(oprops);
            trans.transform(source, sr);
            System.out.println("**** Response ******");
            System.out.println(bos.toString());

            bos.close();
            System.out.println();
        } catch (Exception e) {
        }
    }
}

JavaScript

const headers = {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
};

// Let's create a media file.
const url = 'https://messaging.esendex.us/Messaging.svc/CreateMediaFromUrl';
const response = await fetch(url, {
    headers,
    body: JSON.stringify({
        'Filename': 'MyPhoto.jpg',
        'LicenseKey': '00000000-0000-0000-0000-000000000000',
        'Tags': ['Photo'],
        'Url': 'https://esendex.us/img/docs/example.jpg'
    }),
    method: 'POST'
});

const media = await response.json();

// We can use this ID to fetch the media later on.
const mediaId = media['MediaId'];

// We can fetch the media by ID at any time. Let's do it now.
const params = new URLSearchParams({
    'LicenseKey': '00000000-0000-0000-0000-000000000000',
    'MediaID': mediaId
});
const url2 = 'https://messaging.esendex.us/Messaging.svc/GetMedia?' + params.toString();
const response2 = await fetch(url2, {
    headers
});
const data = await response2.json();

console.log(data);

JSON Response

"String content"

PHP with cURL

// Example 1: The Base64 representation of the image will be printed as the response.

<?php

$url = 'https://messaging.esendex.us/Messaging.svc/GetMedia?LicenseKey=00000000-0000-0000-0000-000000000000&MediaID=11777';
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Accept: application/json'));

$response = curl_exec($ch);

curl_close($ch);

print_r($response);

?>




// Example 2: Option to return the actual media. The media will automatically saved to the location you specified.

<?php

$url = 'https://messaging.esendex.us/Messaging.svc/GetMedia?LicenseKey=00000000-0000-0000-0000-000000000000&MediaID=11777';
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Accept: application/json'));

$response = curl_exec($ch);

curl_close($ch);

$data = base64_decode($response);
$path = 'C:\Users\Public\TestImage.jpg';

file_put_contents($path, $data);

echo "Image Saved to: ", $path;

?>




// Example 3: Option to display the actual media. The media will automatically display when running the code and the image will be saved to the location you specified.

<?php

$url = 'https://messaging.esendex.us/Messaging.svc/GetMedia?LicenseKey=00000000-0000-0000-0000-000000000000&MediaID=11777';
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Accept: application/json'));

$response = curl_exec($ch);

curl_close($ch);

$data = base64_decode($response);
$path = 'C:\Users\Public\TestImage.jpg';

file_put_contents($path, $data);

echo $data;

?>

Python

import httpx

headers = {"Accept": "application/json"}

with httpx.Client(headers=headers) as client:
    # Let's create a media file.
    response = client.post(
        url="https://messaging.esendex.us/Messaging.svc/CreateMediaFromUrl",
        json={
            "Filename": "MyPhoto.jpg",
            "LicenseKey": "00000000-0000-0000-0000-000000000000",
            "Tags": ["Photo"],
            "Url": "https://esendex.us/img/docs/example.jpg",
        },
    )
    response.raise_for_status()

    if response.text == "":
        raise RuntimeError("The media could not be created.")

    # We can use this ID to fetch the media later on.
    media_id = response.json()["MediaId"]

    # We can fetch the media by ID at any time. Let's do it now.
    response = client.get(
        url="https://messaging.esendex.us/Messaging.svc/GetMedia",
        params={
            "LicenseKey": "00000000-0000-0000-0000-000000000000",
            "MediaID": media_id,
        },
    )
    response.raise_for_status()

    if response.text == "":
        raise RuntimeError("The media could not be found.")

    print(response.json())

Ruby

require 'json'
require 'net/http'

headers = { Accept: 'application/json', 'Content-Type': 'application/json' }

# Let's create a media file.
uri = URI('https://messaging.esendex.us/Messaging.svc/CreateMediaFromUrl')
data = {
  'Filename': 'MyPhoto.jpg',
  'LicenseKey': '00000000-0000-0000-0000-000000000000',
  'Tags': ['Photo'],
  'Url': 'https://esendex.us/img/docs/example.jpg'
}.to_json
response = Net::HTTP.post(uri, data, headers)
raise response.message if response.is_a?(Net::HTTPClientError) || response.is_a?(Net::HTTPServerError)
raise 'The media could not be created.' if response.body == ''

# We can use this ID to fetch the media later on.
media_id = JSON.parse(response.body)['MediaId']

# We can fetch the media by ID at any time. Let's do it now.
uri = URI('https://messaging.esendex.us/Messaging.svc/GetMedia')
params = {
  'LicenseKey': '00000000-0000-0000-0000-000000000000',
  'MediaId': media_id
}
uri.query = URI.encode_www_form(params)
response = Net::HTTP.get(uri, headers)
raise response.message if response.is_a?(Net::HTTPClientError) || response.is_a?(Net::HTTPServerError)
raise 'The media could not be found.' if response == ''

puts response

XML Response

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">String content</string>

Let’s start sending, together.