Twilio Media Content Service Client
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
-
Error uploading media: Twilio API Error: 401 - Unauthorized
cause Invalid Twilio API Key, API Secret, or Account SID provided during client initialization. MCS uses basic authentication with API Key/Secret.fixVerify that `TWILIO_API_KEY`, `TWILIO_API_SECRET`, and `TWILIO_ACCOUNT_SID` environment variables are correctly set and correspond to valid Twilio API credentials with appropriate permissions for the Media Content Service. Remember that API Keys are recommended over Auth Tokens for production. -
Error uploading media: Twilio API Error: 404 - Resource Not Found
cause The provided `TWILIO_CHAT_SERVICE_SID` is incorrect or does not exist for your Twilio Account.fixDouble-check the `TWILIO_CHAT_SERVICE_SID` environment variable against your Twilio Console to ensure it refers to an existing and correct Chat Service Instance.
Warnings
- breaking This package (`twilio-mcs-client`) is considered abandoned. Its last publish was over four years ago (as of mid-2026). While it may still function, there will be no further updates, bug fixes, or security patches. Consider migrating to direct REST API calls or newer Twilio SDKs if media handling is integrated with other active services.
- gotcha The `twilio-mcs-client` is a dedicated client for the Media Content Service, which uses a distinct API endpoint and authentication flow. It is *not* the same as the general `twilio-node` SDK for other Twilio services (e.g., sending SMS, managing calls). You cannot use the `twilio-node` client directly for MCS operations.
- gotcha Media uploaded via MCS requires a `Chat Service SID`. If the media is not attached to a Conversation message within five minutes, it will be automatically garbage-collected by Twilio.
Install
-
npm install twilio-mcs-client -
yarn add twilio-mcs-client -
pnpm add twilio-mcs-client
Imports
- Client
const Client = require('twilio-mcs-client');import { Client } from 'twilio-mcs-client'; - TwilioMcsClientOptions
import type { TwilioMcsClientOptions } from 'twilio-mcs-client'; - MediaInstance
import type { MediaInstance } from 'twilio-mcs-client';
Quickstart
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();