Twilio Media Content Service Client

0.4.3 · abandoned · verified Tue Apr 21

This package provides a client library for interacting with the Twilio Media Content Service (MCS). MCS is a specialized Twilio offering that handles the storage and processing of media content, primarily for use with other Twilio products like Twilio Conversations. It enables developers to upload files, obtain media SIDs, and manage media assets, which can then be attached to messages for features such as Chat Media Messages. The service supports functionalities like generating image and video thumbnails, as well as document and video previews. The `twilio-mcs-client` library acts as a direct interface to the MCS REST API, which operates independently of the main `twilio-node` SDK. As of version 0.4.3, the package was last published approximately four years ago (as of mid-2026), suggesting it is no longer actively developed or may be in a long-term maintenance state with no new feature releases. Users should be aware that active development has ceased.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to initialize the Twilio Media Content Service (MCS) client and upload a simple text file. It creates a dummy file, reads its content into a buffer, and then uses the client's `media.create` method to upload it to a specified Chat Service SID. The script then logs the resulting Media SID and URL. Authentication uses a Twilio API Key and Secret, alongside the Account SID and Chat Service SID, all provided via environment variables. This pattern is essential for integrating media management with Twilio Conversations.

import { Client } from 'twilio-mcs-client';
import { promises as fs } from 'fs';
import * as path from 'path';

// Ensure you have these environment variables set
const accountSid = process.env.TWILIO_ACCOUNT_SID ?? '';
const apiKey = process.env.TWILIO_API_KEY ?? '';
const apiSecret = process.env.TWILIO_API_SECRET ?? '';
const chatServiceSid = process.env.TWILIO_CHAT_SERVICE_SID ?? ''; // Your Chat Service SID

async function uploadMediaFile() {
  if (!accountSid || !apiKey || !apiSecret || !chatServiceSid) {
    console.error('Missing one or more required environment variables: TWILIO_ACCOUNT_SID, TWILIO_API_KEY, TWILIO_API_SECRET, TWILIO_CHAT_SERVICE_SID');
    process.exit(1);
  }

  // 1. Create a dummy file for upload
  const fileName = 'example.txt';
  const filePath = path.join(__dirname, fileName);
  const fileContent = 'This is a test file to be uploaded to Twilio Media Content Service.';
  const contentType = 'text/plain';

  try {
    await fs.writeFile(filePath, fileContent);
    console.log(`Created dummy file: ${filePath}`);

    // 2. Read the file into a buffer
    const fileBuffer = await fs.readFile(filePath);

    // 3. Instantiate the Twilio MCS Client
    // The MCS client's base URL uses API key and secret for authentication, distinct from general Twilio client
    const client = new Client(apiKey, apiSecret, { accountSid: accountSid });

    // 4. Upload the media
    console.log(`Uploading ${fileName} (Type: ${contentType}) to Service SID: ${chatServiceSid}...`);
    const mediaInstance = await client.media.create(
      chatServiceSid,
      { 
        body: fileBuffer,
        contentType: contentType,
        filename: fileName
      }
    );

    console.log('Media uploaded successfully!');
    console.log('Media SID:', mediaInstance.sid);
    console.log('Media URL:', mediaInstance.url);

  } catch (error) {
    console.error('Error uploading media:', error);
    if (error instanceof Error && error.message.includes('401')) {
      console.error('Authentication failed. Check your API Key/Secret and Account SID.');
    }
    if (error instanceof Error && error.message.includes('404')) {
      console.error('Service SID not found or incorrect. Check TWILIO_CHAT_SERVICE_SID.');
    }
  } finally {
    // Clean up the dummy file
    try {
      await fs.unlink(filePath);
      console.log(`Cleaned up dummy file: ${filePath}`);
    } catch (cleanupError) {
      console.error('Error cleaning up dummy file:', cleanupError);
    }
  }
}

uploadMediaFile();

view raw JSON →