Sample code for 30+ languages & platforms
Dart Requires Chilkat v11.0.0+

Send an MMS with Twilio using a media URL

See more Twilio Examples

Send an image and an optional caption from an MMS-capable Twilio number. Supply an MMS-supported destination in E.164 format and replace the example media URL with an HTTPS URL Twilio can fetch. The host must handle HEAD and GET and return the correct Content-Type for the file.

Live send. With live credentials and valid inputs, this request submits a billable message. Save the Message SID to check delivery later. Supported media types and size limits.

Twilio Messages API reference

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  var success = false;

  // Assumes Chilkat is unlocked. See Global Unlock Sample.
  final http = CkHttp();
  http.requireSslCertVerify = true;
  http.basicAuth = true;
  // Load the owning Account SID and an API key from application configuration.
  final accountSid = 'YOUR_ACCOUNT_SID';
  http.login = 'YOUR_API_KEY_SID';
  http.password = 'YOUR_API_KEY_SECRET';
  final url = CkStringBuilder();
  url.append('https://api.twilio.com/2010-04-01/Accounts/');
  url.append(accountSid);
  url.append('/Messages.json');

  // Add raw field values. Chilkat form-encodes them, including the + in a phone number.
  final req = CkHttpRequest();
  req.httpVerb = 'POST';
  req.contentType = 'application/x-www-form-urlencoded';
  req.addParam('From', 'YOUR_MMS_ENABLED_NUMBER');
  req.addParam('To', 'RECIPIENT_E164_NUMBER');
  // Supply a URL for the file itself so Twilio can retrieve the media.
  req.addParam('MediaUrl', 'https://media.example.com/photo.jpg');
  req.addParam('Body', 'Photo attached.');

  final resp = CkHttpResponse();
  final reply = CkJsonObject();
  success = true;
  try {
    http.httpReq(url.getAsString(), req, resp);
  } on ChilkatException {
    success = false;
  }
  if (resp.statusCode == 0) {
    print(http.lastErrorText);
    return;
  }

  var parsed = true;
  try {
    reply.load(resp.bodyStr);
  } on ChilkatException {
    parsed = false;
  }
  if ((!success) || (resp.statusCode != 201)) {
    print('HTTP ${resp.statusCode}');
    if ((parsed) && (reply.hasMember('code'))) {
      print('Twilio error ${reply.intOf('code')}: ${reply.stringOf('message')}');
    }

    return;
  }

  if (!parsed) {
    print('Expected a JSON response.');
    return;
  }

  if ((reply.jsonTypeOf('sid') != 1) || (reply.jsonTypeOf('status') != 1)) {
    print('The create response is missing the message SID or status.');
    return;
  }

  print('Message SID: ${reply.stringOf('sid')}');
  print('Initial status: ${reply.stringOf('status')}');
}