This guide provides a complete walkthrough for sending SMS messages from your Node.js application using the MessageBird API. We will cover everything from initial project setup to implementing a basic API endpoint, handling configurations securely, and managing potential issues.
This implementation enables applications to programmatically send SMS notifications, alerts, verification codes, or marketing messages, integrating directly with MessageBird's reliable infrastructure.
We'll use Node.js for the backend logic, the official messagebird
Node.js SDK for interacting with the API, and dotenv
for managing environment variables securely. Optionally, we'll demonstrate building a simple API endpoint using Express.js.
Project Overview and Goals
-
Goal: Create a Node.js service capable of sending SMS messages via the MessageBird API.
-
Problem Solved: Provides a straightforward way to integrate SMS functionality into any Node.js application.
-
Technologies:
- Node.js: The runtime environment.
- npm/yarn: Package management.
- MessageBird Node.js SDK: Official library for interacting with the MessageBird API.
- dotenv: For managing environment variables securely.
- (Optional) Express.js: For building a simple API endpoint.
-
Architecture:
+-----------------+ +---------------------+ +-----------------+ +-----------------+ | Your Application| ---> | Node.js Backend | ---> | MessageBird SDK | ---> | MessageBird API | | (e.g., Vite FE)| | (Express optional) | | (messagebird) | | (Sends SMS) | +-----------------+ +---------------------+ +-----------------+ +-----------------+ (Uses API Key & Originator)
-
Outcome: A functional Node.js script or API endpoint that accepts a recipient number and message body, then sends an SMS using your MessageBird account.
-
Prerequisites:
- Node.js and npm (or yarn) installed. Download Node.js
- A MessageBird account. Sign up for free
- A MessageBird Live API Key.
- A MessageBird Purchased Number or Alphanumeric Sender ID (Originator).
1. Setting up the Project
Let's initialize our Node.js project and install the necessary dependencies.
-
Create Project Directory: Open your terminal and create a new directory for your project, then navigate into it.
mkdir node-messagebird-sms cd node-messagebird-sms
-
Initialize Node.js Project: This creates a
package.json
file to manage dependencies and project metadata.npm init -y
(Use
yarn init -y
if you prefer yarn) -
Install Dependencies: We need the
messagebird
SDK anddotenv
for environment variables.npm install messagebird dotenv
(Use
yarn add messagebird dotenv
for yarn) -
Set Up Environment Variables: Create a file named
.env
in the root of your project directory. This file will store sensitive information like your API key. Never commit this file to version control.-
.env
file:MESSAGEBIRD_ACCESS_KEY=YOUR_LIVE_API_KEY MESSAGEBIRD_ORIGINATOR=YOUR_SENDER_ID_OR_NUMBER
-
Replace Placeholders:
YOUR_LIVE_API_KEY
: Obtain this from your MessageBird Dashboard (see Section 4).YOUR_SENDER_ID_OR_NUMBER
: Use a number purchased from MessageBird (in E.164 format, e.g.,+12025550187
) or an approved Alphanumeric Sender ID (e.g.,MyCompany
). See Section 4 for details.
-
-
Configure
.gitignore
: Create a.gitignore
file in the project root to prevent accidentally committing sensitive files and unnecessary directories..gitignore
file:node_modules/ .env npm-debug.log* yarn-debug.log* yarn-error.log*
-
Project Structure: Your basic project structure should now look like this:
node-messagebird-sms/ ├── .env ├── .gitignore ├── node_modules/ ├── package.json ├── package-lock.json (or yarn.lock) └── (Your code files will go here, e.g., send-sms.js)
2. Implementing Core Functionality (Basic Script)
Let's create a simple Node.js script to send an SMS message.
-
Create Script File: Create a file named
send-sms.js
in your project root. -
Write the Code: Add the following code to
send-sms.js
. This script loads environment variables, initializes the MessageBird client, and calls themessages.create
method.send-sms.js
:// Load environment variables from .env file require('dotenv').config(); // Import the MessageBird SDK and initialize it with your API key const accessKey = process.env.MESSAGEBIRD_ACCESS_KEY; if (!accessKey) { console.error('Error: MESSAGEBIRD_ACCESS_KEY is not set in the .env file.'); process.exit(1); // Exit if the key is missing } // Use the correct initialization method: initClient() const messagebird = require('messagebird').initClient(accessKey); // Define message parameters const originator = process.env.MESSAGEBIRD_ORIGINATOR; // IMPORTANT: Replace '+1RECIPIENTNUMBER' with a real phone number // in E.164 format (e.g., +12223334444) that you can use for testing. const recipients = ['+1RECIPIENTNUMBER']; const body = 'Hello from Node.js and MessageBird!'; // Generic message body // Basic validation if (!originator) { console.error('Error: MESSAGEBIRD_ORIGINATOR is not set in the .env file.'); process.exit(1); } if (!recipients || recipients.length === 0 || !recipients[0] || recipients[0] === '+1RECIPIENTNUMBER') { // Check placeholder too console.error('Error: Recipient number is missing, invalid, or still set to the placeholder.'); process.exit(1); } const params = { originator: originator, recipients: recipients, body: body }; // Send the message using the MessageBird API console.log('Sending SMS...'); messagebird.messages.create(params, function (err, response) { if (err) { // Handle errors console.error('Error sending SMS:'); console.error(err); // Log the full error object for details // Provide more user-friendly messages for common errors if possible if (err.errors) { err.errors.forEach(error => console.error(`API Error Code ${error.code}: ${error.description}`)); } } else { // Handle success console.log('SMS sent successfully!'); console.log('Response:'); console.log(JSON.stringify(response, null, 2)); // Pretty print the JSON response // You can check the response details, e.g., message status if (response.recipients && response.recipients.items) { response.recipients.items.forEach(item => { console.log(` Recipient: ${item.recipient}, Status: ${item.status}, Status Datetime: ${item.statusDatetime}`); }); } } });
-
Explanation:
require('dotenv').config();
: Loads variables from your.env
file intoprocess.env
.require('messagebird').initClient(accessKey);
: Crucially, we useinitClient()
to initialize the SDK with the API key loaded from the environment variables. This is the current correct method. Do not use the outdatedrequire('messagebird')(accessKey)
syntax.recipients
: An array of phone numbers in E.164 format. You must replace the placeholder with a real number.body
: The text content of the SMS message.params
: An object containing theoriginator
(sender ID),recipients
, and messagebody
.messagebird.messages.create(params, callback)
: The asynchronous function that sends the request to the MessageBird API.callback(err, response)
: The function executed upon completion.err
contains error details if the request failed,response
contains success details otherwise.
-
Run the Script: Important: Replace
+1RECIPIENTNUMBER
insend-sms.js
with a valid phone number (in E.164 format, like+12223334444
) that you can check. Then, run the script from your terminal:node send-sms.js
You should see output indicating success or failure, and the SMS should arrive shortly if successful.
3. Building an API Layer (Optional with Express)
To integrate SMS sending into a web application (like a Vite frontend), you'll often expose this functionality via an API endpoint. Here's how using Express.js:
-
Install Express:
npm install express
(Use
yarn add express
for yarn) -
Create Server File: Create a file named
server.js
. -
Write the Server Code:
server.js
:require('dotenv').config(); const express = require('express'); const messagebird = require('messagebird').initClient(process.env.MESSAGEBIRD_ACCESS_KEY); const app = express(); const port = process.env.PORT || 3001; // Use environment PORT or default to 3001 // Middleware to parse JSON request bodies app.use(express.json()); // Basic validation middleware (example) const validateSmsRequest = (req, res, next) => { const { recipient, message } = req.body; if (!recipient || typeof recipient !== 'string' || !message || typeof message !== 'string') { return res.status(400).json({ error: 'Missing or invalid parameters. Required: recipient (string), message (string).' }); } // Add more robust validation (e.g., E.164 format check for recipient) here // Consider using a library like libphonenumber-js for better validation (see Section 7) next(); }; // POST endpoint to send SMS app.post('/api/send-sms', validateSmsRequest, (req, res) => { const { recipient, message } = req.body; const originator = process.env.MESSAGEBIRD_ORIGINATOR; if (!originator) { console.error('Error: MESSAGEBIRD_ORIGINATOR is not set.'); return res.status(500).json({ error: 'Server configuration error: Originator not set.' }); } if (!process.env.MESSAGEBIRD_ACCESS_KEY) { console.error('Error: MESSAGEBIRD_ACCESS_KEY is not set.'); return res.status(500).json({ error: 'Server configuration error: Access key not set.' }); } const params = { originator: originator, recipients: [recipient], // Ensure recipient is in an array body: message, }; console.log(`API received request to send SMS to ${recipient}`); messagebird.messages.create(params, function (err, response) { if (err) { console.error('API Error sending SMS:', err); // Provide specific error details if possible const errorDetails = err.errors ? err.errors.map(e => ({ code: e.code, description: e.description })) : [{ code: err.statusCode || 500, description: 'Failed to send SMS via provider.' }]; res.status(err.statusCode || 500).json({ error: 'Failed to send SMS.', details: errorDetails }); } else { console.log('API successfully sent SMS:', response.id); res.status(200).json({ success: true, messageId: response.id, status: response.recipients.items[0].status }); } }); }); // Basic health check endpoint app.get('/health', (req, res) => { res.status(200).send('OK'); }); // Start the server app.listen(port, () => { console.log(`Server listening on port ${port}`); if (!process.env.MESSAGEBIRD_ACCESS_KEY) { console.warn('Warning: MESSAGEBIRD_ACCESS_KEY is not set in the environment.'); } if (!process.env.MESSAGEBIRD_ORIGINATOR) { console.warn('Warning: MESSAGEBIRD_ORIGINATOR is not set in the environment.'); } });
-
Run the Server:
node server.js
-
Test the API Endpoint: Use
curl
or a tool like Postman to send a POST request to your running server:-
curl
Example:curl -X POST http://localhost:3001/api/send-sms \ -H ""Content-Type: application/json"" \ -d '{""recipient"": ""+1RECIPIENTNUMBER"", ""message"": ""API Test Message via Express!""}'
(Replace
+1RECIPIENTNUMBER
with a real E.164 formatted number) -
Expected Success Response (JSON):
{ ""success"": true, ""messageId"": ""a1b2c3d4e5f67890abcdef012345678"", ""status"": ""sent"" }
-
Expected Error Response (JSON):
// Example: Invalid recipient format { ""error"": ""Failed to send SMS."", ""details"": [ { ""code"": 2, ""description"": ""Invalid recipient value."" } ] }
(Status code will be 4xx or 5xx depending on the error)
-
4. Integrating with MessageBird (Credentials & Configuration)
Securely obtaining and managing your MessageBird credentials is vital.
-
Obtain Live API Key:
- Log in to your MessageBird Dashboard.
- Navigate to Developers in the left-hand menu.
- Click on the API access tab.
- If you don't have a key, click Add access key. Ensure the Mode is set to Live.
- Copy the generated Access Key. This is your
MESSAGEBIRD_ACCESS_KEY
. - Store this key securely in your
.env
file and never commit it to your code repository.
-
Configure Originator (Sender ID): The
originator
is what the recipient sees as the sender.- Option A: Purchased Number (Recommended for most cases):
- In the MessageBird Dashboard, navigate to Numbers.
- Click Buy a number.
- Select the desired country and ensure the SMS capability is checked.
- Choose a number and complete the purchase.
- Copy the number in E.164 format (e.g.,
+12025550187
). This is yourMESSAGEBIRD_ORIGINATOR
.
- Option B: Alphanumeric Sender ID:
- These are custom names (e.g.,
MyCompany
, max 11 chars). - Support varies by country. Check the MessageBird Compliance & Sender IDs documentation or navigate to Compliance & Sender IDs in the Dashboard.
- Some countries require pre-registration. Note that for Application-to-Person (A2P) traffic in countries like the US and Canada, using Alphanumeric Sender IDs is often restricted. You will typically need to register a 10-Digit Long Code (10DLC) or use a Toll-Free number.
- If allowed and suitable, enter your desired Alphanumeric Sender ID as the
MESSAGEBIRD_ORIGINATOR
in your.env
file. Note that recipients generally cannot reply to alphanumeric senders.
- These are custom names (e.g.,
- Option A: Purchased Number (Recommended for most cases):
-
Environment Variable Summary:
MESSAGEBIRD_ACCESS_KEY
:- Purpose: Authenticates your requests with the MessageBird API.
- Format: A long alphanumeric string.
- How to Obtain: MessageBird Dashboard -> Developers -> API access -> Create Live Key.
MESSAGEBIRD_ORIGINATOR
:- Purpose: Identifies the sender of the SMS message.
- Format: E.164 phone number (e.g.,
+12025550187
) OR Alphanumeric string (max 11 chars, e.g.,MyCompany
). - How to Obtain: Purchase a number (Dashboard -> Numbers) OR use an approved Alphanumeric Sender ID (check country restrictions/compliance).
5. Error Handling and Logging
Robust error handling is crucial for production applications.
-
Check the
err
Object: Themessagebird.messages.create
callback provides anerr
object on failure. Inspect this object:err.statusCode
: The HTTP status code returned by the API (e.g., 401 for bad key, 422 for validation errors).err.errors
: An array containing detailed error objects from the API, often including acode
anddescription
. Loop through this array for specific reasons.- Example: Checking for insufficient balance (error code 9):
if (err && err.errors) { err.errors.forEach(error => { if (error.code === 9) { // 'No balance' error code console.error(""CRITICAL: Insufficient balance on MessageBird account!""); // Implement alerting here } console.error(`API Error - Code: ${error.code}, Description: ${error.description}`); }); } else if (err) { console.error('Generic API Error:', err.message || 'Unknown error', 'Status:', err.statusCode); }
-
Logging:
- For simple cases,
console.log
andconsole.error
suffice. - For production, use a dedicated logging library like Winston or Pino for structured logging (e.g., JSON format), different log levels (info, warn, error), and routing logs to files or external services.
// Example with Pino (conceptual) // const pino = require('pino')(); // pino.info({ response }, 'SMS sent successfully'); // pino.error({ err }, 'Error sending SMS');
- For simple cases,
-
Retry Mechanisms:
- Network issues or temporary MessageBird API glitches might cause transient failures (e.g., 5xx errors).
- Implement a simple retry strategy with exponential backoff for these cases (e.g., retry after 1s, then 2s, then 4s). Libraries like
async-retry
can help. - Caution: Do not retry on client errors (4xx) like invalid recipients or insufficient balance, as these will consistently fail.
6. Database Schema and Data Layer
For the core task of sending an SMS, no database is required by this service itself.
However, if you were building a larger application (like an SMS customer support system), you would need a database (e.g., MongoDB, PostgreSQL) to store:
- Sent message logs (IDs, recipients, status, timestamps).
- Received messages (if implementing webhooks).
- User data or ticket information.
This guide focuses solely on the sending mechanism, which is stateless.
7. Security Features
Protecting your API endpoint and credentials is vital.
- API Key Security:
- NEVER hardcode API keys in your source code.
- Use environment variables (
.env
locally, secure configuration management in deployment). - Ensure
.env
is in your.gitignore
.
- Input Validation (API Layer):
- Use libraries like
joi
orexpress-validator
to strictly validate incoming request bodies (recipient
,message
). - Ensure
recipient
matches E.164 format (e.g., using regex`^\+[1-9]\d{1,14}$`
). While regex provides a basic check, for more robust international phone number validation, consider using a dedicated library likelibphonenumber-js
, as regex alone can struggle with complex numbering plans and edge cases. - Limit the length of the
message
body to prevent abuse and unexpected costs.
- Use libraries like
- Rate Limiting (API Layer):
- Implement rate limiting on your
/api/send-sms
endpoint using middleware likeexpress-rate-limit
to prevent abuse (e.g., limit requests per IP address). - First, install the package:
npm install express-rate-limit
- Then, apply it in your
server.js
:// In server.js, before defining your routes const rateLimit = require('express-rate-limit'); const apiLimiter = rateLimit({ windowMs: 15 * 60 * 1000, // 15 minutes max: 100, // Limit each IP to 100 requests per windowMs message: 'Too many requests from this IP, please try again after 15 minutes', standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers legacyHeaders: false, // Disable the `X-RateLimit-*` headers }); // Apply the rate limiting middleware to API calls only // Make sure this line is placed before the app.post('/api/send-sms', ...) route definition app.use('/api/send-sms', apiLimiter);
- Implement rate limiting on your
- Authentication/Authorization (API Layer):
- If the API is not intended for public use, protect it. Implement API key authentication, JWT, OAuth, or another appropriate mechanism so only authorized clients can trigger SMS sends.
- Prevent SMS Pumping Fraud: Be aware that malicious actors might abuse open SMS endpoints to send messages to premium-rate numbers. Rate limiting and authentication are key defenses. Monitor your MessageBird usage regularly.
8. Handling Special Cases
Consider these factors when sending SMS:
- Character Limits & Encoding:
- Standard SMS (GSM-7 encoding) is limited to 160 characters.
- Using non-standard characters (like emojis or non-Latin alphabets) switches to UCS-2 encoding, limiting messages to 70 characters.
- Longer messages are automatically split (concatenated) into multiple parts by MessageBird, each billed separately. Be mindful of message length to control costs. The
messagebird
SDK handles this automatically, but the billing impact is real.
- Country Restrictions:
- Originator: Alphanumeric Sender IDs are not supported everywhere (e.g., USA often requires pre-registered 10DLC numbers or Toll-Free numbers for A2P messaging). Using a local virtual number from MessageBird is often the most reliable option. Check MessageBird Compliance & Sender IDs.
- Content: Some countries have regulations on SMS content (e.g., filtering spam, blocking certain keywords).
- Delivery Reports: MessageBird can send delivery status updates via webhooks (DLRs - Delivery Reports). This requires setting up a separate webhook endpoint in your application and configuring it in the MessageBird dashboard or API call (
statusReportUrl
parameter). This is beyond the scope of this basic guide but essential for tracking delivery success/failure reliably. - Time Zones: Timestamps in API responses (
statusDatetime
) are typically UTC. Convert them to the appropriate local time zone for display if necessary.
9. Performance Optimizations
For most use cases, sending individual SMS messages is not performance-intensive.
- Asynchronous Operations: Node.js and the
messagebird
SDK operate asynchronously, preventing the sending process from blocking your main application thread. - Batching: The
recipients
array inmessages.create
can contain up to 50 numbers per API call. If sending the same message to multiple recipients simultaneously, use this batching capability instead of making individual API calls. This is more efficient and reduces API overhead. - Connection Pooling: For very high-volume sending, ensure your Node.js application handles outbound HTTPS connections efficiently (Node.js does this reasonably well by default).
10. Monitoring, Observability, and Analytics
Keep track of your SMS sending activity and application health.
- Logging: As discussed in Section 5, log successful sends (with MessageBird message ID) and, more importantly, any errors encountered.
- Health Checks: Implement a basic health check endpoint (
/health
in the Express example) that monitoring services can poll to ensure your service is running. - MessageBird Dashboard: Regularly check the MessageBird Message Logs to monitor usage, costs, delivery statuses, and any API errors reported by MessageBird.
- Error Tracking: Use services like Sentry or Datadog to automatically capture and alert on application errors, including those from the MessageBird integration.
- Metrics: Track key metrics:
- Number of SMS requested via your API/script.
- Number of successful sends reported by the SDK callback.
- Number of errors reported by the SDK callback (categorize by error type if possible).
- API endpoint latency (if applicable).
11. Troubleshooting and Caveats
Common issues and things to watch out for:
TypeError: require(...) is not a function
: This usually means you used the outdatedrequire('messagebird')(key)
syntax. The current correct method isrequire('messagebird').initClient(key)
. Update your initialization code.401 Unauthorized
Error: YourMESSAGEBIRD_ACCESS_KEY
is incorrect, invalid, or you are using a Test key for Live requests (or vice-versa). Double-check the key in your.env
file and the MessageBird Dashboard.Error: MESSAGEBIRD_ACCESS_KEY is not set
(orORIGINATOR
): The environment variable is missing from.env
ordotenv
failed to load it. Ensure.env
is in the project root andrequire('dotenv').config();
is called before accessingprocess.env
.- Invalid Originator Error (e.g.,
API Error Code 2
):- The
MESSAGEBIRD_ORIGINATOR
is formatted incorrectly. - You are using an Alphanumeric Sender ID in a country where it's not supported or requires pre-registration.
- The number used is not associated with your MessageBird account.
- The
- Invalid Recipient Error (e.g.,
API Error Code 2
):- The recipient number is not in valid E.164 format (must start with
+
and country code). - The number itself is invalid or not reachable.
- The recipient number is not in valid E.164 format (must start with
- Insufficient Balance Error (Code 9): Your MessageBird account balance is too low to send the message. Add credits via the Dashboard.
- Firewall Issues: Ensure your server can make outbound HTTPS requests to
rest.messagebird.com
on port 443. - Rate Limits: If sending many messages quickly, you might hit MessageBird API rate limits or the limits set on your own API endpoint (Section 7). Check error responses for rate limit specific codes/messages.
- SMS Not Received:
- Check MessageBird Dashboard logs for the message status (e.g.,
delivered
,failed
,expired
). - Verify the recipient number is correct and has service.
- Check for country-specific regulations or carrier filtering.
- Ensure you used a Live API key and have credits (Test keys don't send real SMS).
- Check MessageBird Dashboard logs for the message status (e.g.,
12. Deployment and CI/CD
Deploying your Node.js application:
- Platform Choice: Choose a hosting platform (e.g., Heroku, Render, AWS EC2/ECS, Google Cloud Run, DigitalOcean Apps).
- Environment Variables: Crucially, configure your
MESSAGEBIRD_ACCESS_KEY
andMESSAGEBIRD_ORIGINATOR
securely within your chosen platform's environment variable management system. Do not deploy your.env
file. - Build Process: Standard
npm install
(ornpm ci
for CI/CD) andnode server.js
(ornode send-sms.js
if just the script). - CI/CD Pipeline:
- Lint/Format: Check code style (
eslint
,prettier
). - Test: Run unit/integration tests (see Section 13).
- Build: (If needed, e.g., TypeScript compilation). Usually just
npm ci --production
. - Deploy: Push code/container to your hosting platform. Ensure environment variables are injected.
- Lint/Format: Check code style (
- Rollback: Have a plan to revert to a previous working deployment if issues arise. Most platforms offer rollback features.
13. Verification and Testing
Ensure your implementation works correctly.
- Manual Verification:
- Run the script (
node send-sms.js
) or hit the API endpoint (curl ...
). - Use your own phone number as the recipient.
- Confirm the SMS arrives with the correct content.
- Check the MessageBird Dashboard Message Logs for the corresponding entry and status (
sent
,delivered
). - Test error cases: Use an invalid API key, invalid recipient format, or empty message body to verify error handling.
- Run the script (
- Automated Testing (Unit Tests):
- Use a testing framework like Jest or Mocha.
- Mock the
messagebird
SDK to avoid making real API calls during tests. You can usejest.mock('messagebird')
or libraries likesinon
for stubbing. - Test that
messagebird.messages.create
is called with the correct parameters (originator
,recipients
,body
). - Test your error handling logic by simulating error responses from the mocked SDK.
- Test input validation logic for the API endpoint.
- Example (Jest - Conceptual):
// __mocks__/messagebird.js const mockCreate = jest.fn((params, callback) => { // Simulate success by default callback(null, { id: 'mock-message-id', recipients: { items: [{ status: 'sent', recipient: params.recipients[0] }] } }); }); // Export the mock function itself for assertion access in tests module.exports = { initClient: jest.fn().mockReturnValue({ messages: { create: mockCreate } }), _mockCreate: mockCreate // Exporting the mock function for direct access }; // your-test-file.test.js jest.mock('messagebird'); // Instructs Jest to use the mock from __mocks__/messagebird.js const messagebirdMock = require('messagebird'); // Import the mocked module // Assume your core SMS sending logic is extracted into a function // const { sendSmsFunction } = require('../your-code'); // Mock function simulating the core logic for testing purposes const sendSmsFunction = (recipient, message) => { const mb = require('messagebird').initClient(process.env.MESSAGEBIRD_ACCESS_KEY); const params = { originator: process.env.MESSAGEBIRD_ORIGINATOR, recipients: [recipient], body: message, }; mb.messages.create(params, (err, res) => { if (err) console.error(""Mocked send error:"", err); // else console.log(""Mocked send success:"", res); }); }; describe('SMS Sending Logic', () => { const OLD_ENV = process.env; beforeEach(() => { // Reset mocks and environment variables before each test jest.resetModules(); // Important to reset module cache process.env = { ...OLD_ENV }; // Make a copy messagebirdMock._mockCreate.mockClear(); }); afterAll(() => { process.env = OLD_ENV; // Restore old environment }); test('should call messagebird.messages.create with correct params', () => { // Set necessary environment variables for this specific test process.env.MESSAGEBIRD_ACCESS_KEY = 'test-key'; // Mock key process.env.MESSAGEBIRD_ORIGINATOR = '+15551234567'; const testRecipient = '+1234567890'; const testMessage = 'Test message'; sendSmsFunction(testRecipient, testMessage); // Assert that the mock 'create' function was called expect(messagebirdMock._mockCreate).toHaveBeenCalledTimes(1); // Assert it was called with the expected parameters expect(messagebirdMock._mockCreate).toHaveBeenCalledWith( expect.objectContaining({ recipients: [testRecipient], body: testMessage, originator: '+15551234567' }), expect.any(Function) // The callback function ); }); test('should handle errors from messagebird.messages.create', () => { // Set necessary environment variables process.env.MESSAGEBIRD_ACCESS_KEY = 'test-key'; process.env.MESSAGEBIRD_ORIGINATOR = '+15551234567'; // Configure the mock to simulate an error const mockError = new Error(""Simulated API Error""); mockError.statusCode = 422; mockError.errors = [{ code: 2, description: ""Invalid recipient"" }]; messagebirdMock._mockCreate.mockImplementationOnce((params, callback) => { callback(mockError, null); }); // Mock console.error to check if it's called const consoleSpy = jest.spyOn(console, 'error').mockImplementation(); sendSmsFunction('+invalid-number', 'Error test'); expect(messagebirdMock._mockCreate).toHaveBeenCalledTimes(1); // Check if console.error was called (indicating error handling) expect(consoleSpy).toHaveBeenCalled(); consoleSpy.mockRestore(); // Clean up the spy }); });
- Verification Checklist:
- Project dependencies installed (
messagebird
,dotenv
,express
if used). -
.env
file created and contains correct Live API Key and Originator. -
.env
file is included in.gitignore
. -
dotenv
is configured and loaded before accessingprocess.env
. -
messagebird
SDK is initialized correctly usinginitClient()
. -
originator
is set correctly (purchased number or valid alphanumeric). -
recipients
parameter is an array of strings in E.164 format. -
body
parameter contains the desired message content. - Script/API handles both
err
andresponse
in the callback. - (API) Input validation is implemented for recipient and message.
- (API) Rate limiting is implemented.
- Test SMS successfully sent and received on a real device (after replacing placeholder recipient).
- Test error case (e.g., invalid recipient) returns an appropriate error response.
- Message Log in MessageBird Dashboard shows the test message(s).
- Deployment configuration correctly sets environment variables.
- Project dependencies installed (
This guide provides a solid foundation for sending SMS using Node.js and MessageBird. Remember to handle credentials securely and implement robust error handling for production applications.