Send SMS with MessageBird in Node.js: Complete Guide for Vite, React & Vue [2025]
Complete guide to sending SMS messages with MessageBird API in Node.js. Includes MessageBird SDK setup, authentication, Express.js endpoints, error handling, and seamless integration with Vite, React, and Vue frontend applications.
This MessageBird Node.js guide teaches you how to send SMS messages using the MessageBird API in your Node.js applications. Whether you're building with Vite, React, or Vue, this tutorial covers MessageBird SDK installation, authentication, and creating secure Express.js API endpoints for SMS integration.
This implementation enables you to programmatically send SMS notifications, alerts, two-factor authentication codes, or marketing messages through MessageBird's reliable infrastructure. You'll use Node.js for backend logic, the official messagebird SDK for API interaction, and dotenv for secure environment variable management. We'll also demonstrate building a simple Express.js API endpoint for frontend integration.
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.
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.
A MessageBird Purchased Number or Alphanumeric Sender ID (Originator).
1. MessageBird Node.js Setup: Install SDK and Configure Environment
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.
Bash
mkdir node-messagebird-sms
cd node-messagebird-sms
Initialize Node.js Project:
This creates a package.json file to manage dependencies and project metadata.
Bash
npm init -y
(Use yarn init -y if you prefer yarn)
Install Dependencies:
We need the messagebird SDK and dotenv for environment variables.
Bash
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.
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.
2. Send SMS with MessageBird API: Core Implementation
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 the messages.create method.
send-sms.js:
JavaScript
// Load environment variables from .env filerequire('dotenv').config();
// Import the MessageBird SDK and initialize it with your API keyconst 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 parametersconst 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 validationif (!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 tooconsole.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 APIconsole.log('Sending SMS...');
messagebird.messages.create(params, function (err, response) {
if (err) {
// Handle errorsconsole.error('Error sending SMS:');
console.error(err); // Log the full error object for details// Provide more user-friendly messages for common errors if possibleif (err.errors) {
err.errors.forEach(error =>console.error(`API Error Code ${error.code}: ${error.description}`));
}
} else {
// Handle successconsole.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 statusif (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 into process.env.
require('messagebird').initClient(accessKey);: Crucially, we use initClient() to initialize the SDK with the API key loaded from the environment variables. This is the current correct method. Do not use the outdated require('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 the originator (sender ID), recipients, and message body.
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 in send-sms.js with a valid phone number (in E.164 format, like +12223334444) that you can check. Then, run the script from your terminal:
Bash
node send-sms.js
You should see output indicating success or failure, and the SMS should arrive shortly if successful.
3. Create Express.js API Endpoint for MessageBird SMS
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:
Bash
npm install express
(Use yarn add express for yarn)
Create Server File:
Create a file named server.js.
Write the Server Code:
server.js:
JavaScript
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)constvalidateSmsRequest = (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 arraybody: 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 possibleconst 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:
Bash
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:
Bash
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)
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 your MESSAGEBIRD_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.
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. MessageBird Error Handling: Best Practices for Production
Robust error handling is crucial for production applications.
Check the err Object: The messagebird.messages.create callback provides an err 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 a code and description. Loop through this array for specific reasons.
Example: Checking for insufficient balance (error code 9):
JavaScript
if (err && err.errors) {
err.errors.forEach(error => {
if (error.code === 9) { // 'No balance' error codeconsole.error("CRITICAL: Insufficient balance on MessageBird account!");
// Implement alerting here
}
console.error(`API Error - Code: ${error.code}, Description: ${error.description}`);
});
} elseif (err) {
console.error('Generic API Error:', err.message || 'Unknown error', 'Status:', err.statusCode);
}
Logging:
For simple cases, console.log and console.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.
JavaScript
// Example with Pino (conceptual)// const pino = require('pino')();// pino.info({ response }, 'SMS sent successfully');// pino.error({ err }, 'Error sending SMS');
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 or express-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 like libphonenumber-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.
Rate Limiting (API Layer):
Implement rate limiting on your /api/send-sms endpoint using middleware like express-rate-limit to prevent abuse and SMS pumping fraud (e.g., limit requests per IP address).
First, install the package:
Bash
npm install express-rate-limit
Then, apply it in your server.js:
JavaScript
// In server.js, before defining your routesconst rateLimit = require('express-rate-limit');
const apiLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutesmax: 100, // Limit each IP to 100 requests per windowMsmessage: 'Too many requests from this IP, please try again after 15 minutes',
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headerslegacyHeaders: 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);
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. SMS Character Limits, Country Restrictions, and Delivery Reports
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. Optimize MessageBird SMS Performance: Batching and Async Operations
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 in messages.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 outdated require('messagebird')(key) syntax. The current correct method is require('messagebird').initClient(key). Update your initialization code.
401 Unauthorized Error: Your MESSAGEBIRD_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 (or ORIGINATOR): The environment variable is missing from .env or dotenv failed to load it. Ensure .env is in the project root and require('dotenv').config(); is called before accessing process.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.
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.
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).
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 and MESSAGEBIRD_ORIGINATOR securely within your chosen platform's environment variable management system. Do not deploy your .env file.
Build Process: Standard npm install (or npm ci for CI/CD) and node server.js (or node 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.
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.
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 use jest.mock('messagebird') or libraries like sinon 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):
JavaScript
// __mocks__/messagebird.jsconst mockCreate = jest.fn((params, callback) => {
// Simulate success by defaultcallback(null, { id: 'mock-message-id', recipients: { items: [{ status: 'sent', recipient: params.recipients[0] }] } });
});
// Export the mock function itself for assertion access in testsmodule.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.jsconst 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 purposesconstsendSmsFunction = (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', () => {
constOLD_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 calledexpect(messagebirdMock._mockCreate).toHaveBeenCalledTimes(1);
// Assert it was called with the expected parametersexpect(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 errorconst mockError = newError("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 calledconst 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 accessing process.env.
messagebird SDK is initialized correctly using initClient().
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 and response 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).
To send SMS with MessageBird in Node.js: (1) Install the MessageBird SDK with npm install messagebird dotenv, (2) initialize the client using require('messagebird').initClient(apiKey), (3) call messagebird.messages.create() with your originator, recipients array (E.164 format), and message body. Always store your MessageBird API key securely in environment variables using dotenv.
What is the correct MessageBird Node.js initialization method?
Use require('messagebird').initClient(accessKey) to initialize the SDK. The older require('messagebird')(accessKey) syntax is outdated and will cause a "TypeError: require(...) is not a function" error. Always use initClient() for the current SDK version.
How do I integrate MessageBird SMS with Vite, React, or Vue?
Create an Express.js API endpoint (POST /api/send-sms) in your Node.js backend that calls MessageBird's API. Your Vite frontend (React/Vue) sends HTTP requests to this endpoint with recipient and message data. Never expose MessageBird API keys in frontend code—always handle SMS sending server-side.
What format do MessageBird recipient phone numbers require?
MessageBird requires phone numbers in E.164 format: start with +, followed by country code and national number (e.g., +12025551234). The recipients parameter must be an array of strings. Invalid formats cause API Error Code 2 ("Invalid recipient value").
How do I get a MessageBird API key?
Log into your MessageBird Dashboard, navigate to Developers → API access, and create a new Live API key. Copy the generated access key immediately and store it in your .env file as MESSAGEBIRD_ACCESS_KEY. Never commit API keys to version control—add .env to .gitignore.
What should I use as MessageBird originator?
Use either a purchased phone number in E.164 format (e.g., +12025550187) or an alphanumeric sender ID (max 11 characters, e.g., "MyCompany"). Alphanumeric IDs aren't supported in all countries—the US typically requires 10DLC registration or toll-free numbers for A2P messaging. Purchase numbers via Dashboard → Numbers.
How do I handle MessageBird API errors in Node.js?
Check the err object in the messagebird.messages.create() callback. Access err.statusCode for HTTP status codes and err.errors array for detailed error objects with code and description properties. Common codes include 2 (invalid recipient), 9 (insufficient balance), and 401 (unauthorized API key).
Can I send SMS to multiple recipients with MessageBird Node.js SDK?
Yes, pass up to 50 phone numbers in the recipients array parameter. MessageBird sends the same message to all recipients in a single API call, which is more efficient than individual calls. Each recipient is billed separately, but API overhead is reduced through batching.
This comprehensive guide covers everything you need to send SMS with MessageBird in Node.js, from SDK installation to production deployment. For advanced use cases, explore bulk broadcast messaging and delivery status callbacks. Remember to handle credentials securely and implement robust error handling for production applications.
Frequently asked questions
Install the 'messagebird' and 'dotenv' npm packages, create a '.env' file with your API key and sender ID, then use the 'messagebird.messages.create' method to send messages. The code example in the article provides a complete script to get you started quickly.
It's the official library for interacting with the MessageBird API from Node.js applications. This SDK simplifies sending SMS messages, managing contacts, and other MessageBird services within your Node.js projects. It handles authentication and API calls.
The most common cause is using the outdated 'require('messagebird')(key)' syntax. You must initialize the SDK with 'require('messagebird').initClient(key)' instead. Double-check the initialization code in the article.
Use them when you want a recognizable brand name as the sender (e.g., 'MyCompany'). However, check MessageBird's compliance rules, as they're not supported everywhere. Numbers are usually more reliable.
Yes, include up to 50 recipients in a single 'messagebird.messages.create' call. The API handles splitting long messages automatically, but be mindful of potential extra costs per segment.
The 'messagebird.messages.create' callback provides an 'err' object with details like 'statusCode' and 'errors'. Inspect this object and log or handle specific errors as shown in the article's error handling section.
Standard GSM-7 encoded messages have a 160-character limit. Using special characters or emojis switches to UCS-2 encoding, reducing the limit to 70 characters. MessageBird automatically concatenates longer messages into multiple parts.
Log into your MessageBird Dashboard, go to 'Developers' -> 'API access', and generate a Live API key. Store this key securely in your '.env' file and never expose it in your code repository.
You can use either a purchased phone number (recommended) or an approved Alphanumeric Sender ID. Add your chosen originator to the '.env' file as 'MESSAGEBIRD_ORIGINATOR'.
This indicates an incorrect or invalid API key. Verify the 'MESSAGEBIRD_ACCESS_KEY' in your '.env' file matches the one in your MessageBird Dashboard and that it is a Live key.
You need 'MESSAGEBIRD_ACCESS_KEY' for API authentication and 'MESSAGEBIRD_ORIGINATOR' for the sender ID. Store these securely in a '.env' file.
Manually test by sending an SMS to your own number. For automated testing, mock the 'messagebird' SDK using Jest or similar tools to simulate API calls and responses without sending real messages.
The environment variable is likely missing from your '.env' file, or the 'dotenv' package isn't loading it correctly. Check the file exists and ensure 'require('dotenv').config();' is called before using the variable.
Use the 'express-rate-limit' middleware with your Express.js app to control the number of SMS requests allowed per IP or user within a specific time window, preventing abuse of your service.
Check the message status in your MessageBird Dashboard logs, verify the recipient number, check for country-specific regulations, and ensure your account has sufficient balance.
This MessageBird Node.js guide teaches you how to send SMS messages using the MessageBird API in your Node.js applications. Whether you're building with Vite, React, or Vue, this tutorial covers MessageBird SDK installation, authentication, and creating secure Express.js API endpoints for SMS integration.
This implementation enables you to programmatically send SMS notifications, alerts, two-factor authentication codes, or marketing messages through MessageBird's reliable infrastructure. You'll use Node.js for backend logic, the official
messagebirdSDK for API interaction, anddotenvfor secure environment variable management. We'll also demonstrate building a simple Express.js API endpoint for frontend integration.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:
Architecture:
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:
1. MessageBird Node.js Setup: Install SDK and Configure Environment
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-smsInitialize Node.js Project: This creates a
package.jsonfile to manage dependencies and project metadata.(Use
yarn init -yif you prefer yarn)Install Dependencies: We need the
messagebirdSDK anddotenvfor environment variables.(Use
yarn add messagebird dotenvfor yarn)Set Up Environment Variables: Create a file named
.envin the root of your project directory. This file will store sensitive information like your API key. Never commit this file to version control..envfile: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.gitignorefile in the project root to prevent accidentally committing sensitive files and unnecessary directories..gitignorefile:Project Structure: Your basic project structure should now look like this:
2. Send SMS with MessageBird API: Core Implementation
Let's create a simple Node.js script to send an SMS message.
Create Script File: Create a file named
send-sms.jsin 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.createmethod.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.envfile 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.errcontains error details if the request failed,responsecontains success details otherwise.Run the Script: Important: Replace
+1RECIPIENTNUMBERinsend-sms.jswith a valid phone number (in E.164 format, like+12223334444) that you can check. Then, run the script from your terminal:You should see output indicating success or failure, and the SMS should arrive shortly if successful.
3. Create Express.js API Endpoint for MessageBird SMS
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:
(Use
yarn add expressfor 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:
Test the API Endpoint: Use
curlor a tool like Postman to send a POST request to your running server:curlExample:curl -X POST http://localhost:3001/api/send-sms \ -H "Content-Type: application/json" \ -d '{"recipient": "+1RECIPIENTNUMBER", "message": "API Test Message via Express!"}'(Replace
+1RECIPIENTNUMBERwith 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. MessageBird API Authentication: Get Your API Key and Originator
Securely obtaining and managing your MessageBird credentials is vital.
Obtain Live API Key:
MESSAGEBIRD_ACCESS_KEY..envfile and never commit it to your code repository.Configure Originator (Sender ID): The
originatoris what the recipient sees as the sender.+12025550187). This is yourMESSAGEBIRD_ORIGINATOR.MyCompany, max 11 chars).MESSAGEBIRD_ORIGINATORin your.envfile. Note that recipients generally cannot reply to alphanumeric senders.Environment Variable Summary:
MESSAGEBIRD_ACCESS_KEY:MESSAGEBIRD_ORIGINATOR:+12025550187) OR Alphanumeric string (max 11 chars, e.g.,MyCompany).5. MessageBird Error Handling: Best Practices for Production
Robust error handling is crucial for production applications.
Check the
errObject: Themessagebird.messages.createcallback provides anerrobject 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 acodeanddescription. Loop through this array for specific reasons.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:
console.logandconsole.errorsuffice.// Example with Pino (conceptual) // const pino = require('pino')(); // pino.info({ response }, 'SMS sent successfully'); // pino.error({ err }, 'Error sending SMS');Retry Mechanisms:
async-retrycan help.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:
This guide focuses solely on the sending mechanism, which is stateless.
7. Security Features
Protecting your API endpoint and credentials is vital.
.envlocally, secure configuration management in deployment)..envis in your.gitignore.joiorexpress-validatorto strictly validate incoming request bodies (recipient,message).recipientmatches 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.messagebody to prevent abuse and unexpected costs./api/send-smsendpoint using middleware likeexpress-rate-limitto prevent abuse and SMS pumping fraud (e.g., limit requests per IP address).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);8. SMS Character Limits, Country Restrictions, and Delivery Reports
Consider these factors when sending SMS:
messagebirdSDK handles this automatically, but the billing impact is real.statusReportUrlparameter). This is beyond the scope of this basic guide but essential for tracking delivery success/failure reliably.statusDatetime) are typically UTC. Convert them to the appropriate local time zone for display if necessary.9. Optimize MessageBird SMS Performance: Batching and Async Operations
For most use cases, sending individual SMS messages is not performance-intensive.
messagebirdSDK operate asynchronously, preventing the sending process from blocking your main application thread.recipientsarray inmessages.createcan 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.10. Monitoring, Observability, and Analytics
Keep track of your SMS sending activity and application health.
/healthin the Express example) that monitoring services can poll to ensure your service is running.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 UnauthorizedError: YourMESSAGEBIRD_ACCESS_KEYis incorrect, invalid, or you are using a Test key for Live requests (or vice-versa). Double-check the key in your.envfile and the MessageBird Dashboard.Error: MESSAGEBIRD_ACCESS_KEY is not set(orORIGINATOR): The environment variable is missing from.envordotenvfailed to load it. Ensure.envis in the project root andrequire('dotenv').config();is called before accessingprocess.env.API Error Code 2):MESSAGEBIRD_ORIGINATORis formatted incorrectly.API Error Code 2):+and country code).rest.messagebird.comon port 443.delivered,failed,expired).12. Deployment and CI/CD
Deploying your Node.js application:
MESSAGEBIRD_ACCESS_KEYandMESSAGEBIRD_ORIGINATORsecurely within your chosen platform's environment variable management system. Do not deploy your.envfile.npm install(ornpm cifor CI/CD) andnode server.js(ornode send-sms.jsif just the script).eslint,prettier).npm ci --production.13. Verification and Testing
Ensure your implementation works correctly.
node send-sms.js) or hit the API endpoint (curl ...).sent,delivered).messagebirdSDK to avoid making real API calls during tests. You can usejest.mock('messagebird')or libraries likesinonfor stubbing.messagebird.messages.createis called with the correct parameters (originator,recipients,body).// __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 }); });messagebird,dotenv,expressif used)..envfile created and contains correct Live API Key and Originator..envfile is included in.gitignore.dotenvis configured and loaded before accessingprocess.env.messagebirdSDK is initialized correctly usinginitClient().originatoris set correctly (purchased number or valid alphanumeric).recipientsparameter is an array of strings in E.164 format.bodyparameter contains the desired message content.errandresponsein the callback.Frequently Asked Questions
How do I send SMS with MessageBird in Node.js?
To send SMS with MessageBird in Node.js: (1) Install the MessageBird SDK with
npm install messagebird dotenv, (2) initialize the client usingrequire('messagebird').initClient(apiKey), (3) callmessagebird.messages.create()with your originator, recipients array (E.164 format), and message body. Always store your MessageBird API key securely in environment variables using dotenv.What is the correct MessageBird Node.js initialization method?
Use
require('messagebird').initClient(accessKey)to initialize the SDK. The olderrequire('messagebird')(accessKey)syntax is outdated and will cause a "TypeError: require(...) is not a function" error. Always useinitClient()for the current SDK version.How do I integrate MessageBird SMS with Vite, React, or Vue?
Create an Express.js API endpoint (POST
/api/send-sms) in your Node.js backend that calls MessageBird's API. Your Vite frontend (React/Vue) sends HTTP requests to this endpoint with recipient and message data. Never expose MessageBird API keys in frontend code—always handle SMS sending server-side.What format do MessageBird recipient phone numbers require?
MessageBird requires phone numbers in E.164 format: start with
+, followed by country code and national number (e.g.,+12025551234). The recipients parameter must be an array of strings. Invalid formats cause API Error Code 2 ("Invalid recipient value").How do I get a MessageBird API key?
Log into your MessageBird Dashboard, navigate to Developers → API access, and create a new Live API key. Copy the generated access key immediately and store it in your
.envfile asMESSAGEBIRD_ACCESS_KEY. Never commit API keys to version control—add.envto.gitignore.What should I use as MessageBird originator?
Use either a purchased phone number in E.164 format (e.g.,
+12025550187) or an alphanumeric sender ID (max 11 characters, e.g., "MyCompany"). Alphanumeric IDs aren't supported in all countries—the US typically requires 10DLC registration or toll-free numbers for A2P messaging. Purchase numbers via Dashboard → Numbers.How do I handle MessageBird API errors in Node.js?
Check the
errobject in themessagebird.messages.create()callback. Accesserr.statusCodefor HTTP status codes anderr.errorsarray for detailed error objects withcodeanddescriptionproperties. Common codes include 2 (invalid recipient), 9 (insufficient balance), and 401 (unauthorized API key).Can I send SMS to multiple recipients with MessageBird Node.js SDK?
Yes, pass up to 50 phone numbers in the
recipientsarray parameter. MessageBird sends the same message to all recipients in a single API call, which is more efficient than individual calls. Each recipient is billed separately, but API overhead is reduced through batching.This comprehensive guide covers everything you need to send SMS with MessageBird in Node.js, from SDK installation to production deployment. For advanced use cases, explore bulk broadcast messaging and delivery status callbacks. Remember to handle credentials securely and implement robust error handling for production applications.