Skip to content

Quick Start

Send your first SMS message in minutes with the MsGine SDK.

1. Get Your API Token

First, you'll need an API token:

  1. Sign up for a MsGine account
  2. Navigate to the Developer Dashboard
  3. Generate a new API token
  4. Copy the token (it won't be shown again)

2. Set Up Environment Variables

Store your API token securely using environment variables:

bash
# .env
MSGINE_API_TOKEN=your-api-token-here

TIP

Never commit your API token to version control. Add .env to your .gitignore file.

3. Initialize the Client

Create a new MsGine client instance:

typescript
import { MsGineClient } from '@msgine/sdk'
import * as dotenv from 'dotenv'

dotenv.config()

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

4. Send Your First SMS

Now you're ready to send a message:

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

console.log('Message sent:', result.id)
console.log('Status:', result.status)
console.log('Cost:', result.cost, result.currency)

Complete Example

Here's a complete working example:

typescript
import { MsGineClient, MsGineError } from '@msgine/sdk'
import * as dotenv from 'dotenv'

dotenv.config()

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

async function sendMessage() {
  try {
    const result = await client.sendSms({
      to: '+256701521269',
      message: 'Your verification code is 123456'
    })

    console.log('✅ Message sent successfully!')
    console.log('Message ID:', result.id)
    console.log('Status:', result.status)
    console.log('Recipients:', result.to)
    console.log('Cost:', result.cost, result.currency)
    console.log('Sent at:', result.createdAt)
  } catch (error) {
    if (error instanceof MsGineError) {
      console.error('❌ API Error:', error.message)
      console.error('Status Code:', error.statusCode)
      console.error('Error Code:', error.code)
    } else {
      console.error('❌ Unexpected error:', error)
    }
  }
}

sendMessage()

Understanding the Response

The sendSms method returns a SendSmsResponse object:

typescript
{
  id: string              // Unique message identifier
  sid: string | null      // Provider-specific ID
  channel: string         // Message channel (e.g., "sms")
  to: string[]           // Array of recipient phone numbers
  from: string           // Sender ID
  content: string        // Message content
  status: MessageStatus  // Current message status
  cost: number          // Message cost
  currency: string      // Currency code (e.g., "USD")
  createdAt: string     // ISO 8601 timestamp
  updatedAt?: string    // ISO 8601 timestamp
}

Message Statuses

Messages can have the following statuses:

  • pending - Message is queued for delivery
  • sent - Message has been sent to the carrier
  • delivered - Message was successfully delivered
  • failed - Message delivery failed

What's Next?

Now that you've sent your first message, explore more features:

Common Issues

"Unauthorized" Error

If you get an authorization error, check that:

  • Your API token is correct
  • The token is properly loaded from environment variables
  • The token hasn't expired

"Invalid Phone Number" Error

Phone numbers must be in E.164 format:

  • +256701521269 (correct)
  • 0701521269 (missing country code)
  • 256701521269 (missing + prefix)

See the Troubleshooting Guide for more help.

Released under the MIT License.