Send MMS with AWS SNS and Next.js: A Developer Guide - code-examples -

Frequently Asked Questions

You can send MMS messages by creating a Next.js API route that interacts with the AWS Pinpoint SMS and Voice V2 API. This API, along with the AWS SDK for JavaScript v3, allows you to send messages containing text and media hosted on Amazon S3.
The Amazon Pinpoint SMS and Voice V2 API is the AWS service used for sending MMS messages programmatically. While the AWS console might still reference SNS for message history, the Pinpoint API is what's used for sending MMS from your application.
Amazon S3 is used to store the media files (images, videos) included in MMS messages. It's chosen for its scalability, durability, and seamless integration with other AWS services, making it ideal for handling media in your MMS workflow.
The `sms-voice` namespace in the AWS SDK for JavaScript v3 is specifically for interacting with the Amazon Pinpoint SMS and Voice V2 API. Use this namespace when you need to send MMS messages or other SMS/voice communications programmatically.
While this guide uses Next.js for its ease of API route creation and full-stack capabilities, you can adapt the core logic to work with other React frameworks. You'll still need the AWS SDK for JavaScript v3 and interact with the Pinpoint API.
For local development, you can set `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` in a `.env.local` file. However, for production, it is best to use IAM roles for enhanced security. The AWS SDK will automatically use roles if available in your environment. Never commit credentials to version control.
Your IAM user or role needs the `sms-voice:SendMediaMessage` permission to send MMS via Pinpoint and `s3:GetObject` to allow the service to retrieve media from your designated S3 bucket.
Send a POST request to your API endpoint with a JSON body containing `destinationPhoneNumber` (in E.164 format), an optional `messageBody`, and `mediaUrl` which must be an S3 URI pointing to your media file.
The `mediaUrl` must be an S3 URI, for example, `s3://your-bucket-name/image.jpg`. It must follow this specific format so the Pinpoint API can retrieve the file.
Your S3 bucket, Origination Number, and the AWS region specified in your application/SDK client *must* all be the same. Mismatched regions will cause errors when trying to send MMS messages.
This error indicates missing IAM permissions. Verify your IAM user/role has `sms-voice:SendMediaMessage` and `s3:GetObject` permissions, ensuring the S3 resource ARN in the policy accurately specifies your bucket.
This could mean your origination phone number (SNS_ORIGINATION_NUMBER) is incorrect, doesn't exist in the specified AWS region, or the media file specified by the S3 URI (mediaUrl) is not found or the bucket is incorrect.
Check that the `destinationPhoneNumber` is in E.164 format, the `mediaUrl` is a valid S3 URI formatted as an array (`[s3Uri]`), and verify the media file meets AWS size and type limits. Ensure the origination number is also correct.
New AWS accounts are often in a sandbox, restricting sending to verified phone numbers. Verify the destination numbers in the AWS SNS console or request to be moved out of the sandbox via AWS Support for production use.