DEV Community

Ssekindi kasim
Ssekindi kasim

Posted on

Send Messages in 3 Lines of TypeScript

Introducing MsGine SDK: Type-Safe Messaging for Node.js

We're excited to announce the release of @msgine/sdk - the official TypeScript SDK for the MsGine SMS API.

The Problem

If you've ever integrated a Messaging API, you've probably experienced:

  • Untyped responses causing runtime surprises
  • Writing retry logic from scratch
  • Cryptic error messages that don't help
  • Validating input manually

We built this SDK to solve these problems once and for all.

The Solution

Install

npm install @msgine/sdk
Enter fullscreen mode Exit fullscreen mode

Send Your First SMS

import { MsGineClient } from '@msgine/sdk';

const client = new MsGineClient({
  apiToken: process.env.MSGINE_API_TOKEN!,
});

const result = await client.sendSms({
  to: '+256701521269',
  message: 'Hello from MsGine!',
});

console.log('Message ID:', result.id);
console.log('Cost:', result.cost, result.currency);
Enter fullscreen mode Exit fullscreen mode

Features

TypeScript Support

const result: SendSmsResponse = await client.sendSms({...});

result.id;        // string ✅
result.to;        // string[] ✅
result.cost;      // number ✅
result.status;    // MessageStatus ✅
Enter fullscreen mode Exit fullscreen mode

Automatic Retries

const client = new MsGineClient({
  apiToken: '...',
  retry: {
    maxRetries: 3,
    initialDelay: 1000,
    backoffMultiplier: 2,
  },
});
Enter fullscreen mode Exit fullscreen mode

Runtime Validation

try {
  await client.sendSms({
    to: '',  // Invalid!
    message: 'Hello',
  });
} catch (error) {
  if (error instanceof MsGineValidationError) {
    console.log(error.errors);
    // Clear message: "Phone number is required"
  }
}
Enter fullscreen mode Exit fullscreen mode

Batch Sending

Send multiple messages efficiently:

const results = await client.sendSmsBatch([
  { to: '+256701521269', message: 'Hello Alice!' },
  { to: '+256701521270', message: 'Hello Bob!' },
]);

const totalCost = results.reduce((sum, r) => sum + r.cost, 0);
console.log(`Total cost: ${totalCost} UGX`);
Enter fullscreen mode Exit fullscreen mode

Detailed Errors

try {
  await client.sendSms({...});
} catch (error) {
  if (error instanceof MsGineError) {
    console.log('Status:', error.statusCode);  // 401
    console.log('Code:', error.code);          // "UNAUTHORIZED"
    console.log('Message:', error.message);    // "Invalid API token"
    console.log('Request ID:', error.requestId); // For support
  }
}
Enter fullscreen mode Exit fullscreen mode

Example

Here's a complete example with error handling:

import { 
  MsGineClient, 
  MsGineError, 
  MsGineValidationError,
  MessageStatus 
} from '@msgine/sdk';

const client = new MsGineClient({
  apiToken: process.env.MSGINE_API_TOKEN!,
  timeout: 30000,
  retry: {
    maxRetries: 3,
  },
});

async function sendVerificationCode(phone: string, code: string) {
  try {
    const result = await client.sendSms({
      to: phone,
      message: `Your verification code is: ${code}`,
    });

    if (result.status === MessageStatus.PENDING) {
      console.log(` Code sent to ${phone}`);
      console.log(`   Message ID: ${result.id}`);
      console.log(`   Cost: ${result.cost} ${result.currency}`);
      return true;
    }
  } catch (error) {
    if (error instanceof MsGineValidationError) {
      console.error('Invalid input:', error.message);
    } else if (error instanceof MsGineError) {
      console.error(`API Error: ${error.message}`);
    }
    return false;
  }
}

// Usage
await sendVerificationCode('+256701521269', '123456');
Enter fullscreen mode Exit fullscreen mode

Get Started

# Install
npm install @msgine/sdk

# Create .env
MSGINE_API_TOKEN=your-token-here
Enter fullscreen mode Exit fullscreen mode

Then start sending messages!

Links

What's Next?

We're planning:

  • Message templates
  • Scheduled sending
  • Delivery webhooks
  • MMS support

What would you find most useful? Let us know in the comments!


typescript #nodejs #javascript #webdev #api

Top comments (0)