Skip to content

MsGineClient

Complete API reference for the MsGineClient class.

Constructor

new MsGineClient(config)

Creates a new MsGine SDK client instance.

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

const client = new MsGineClient({
  apiToken: process.env.MSGINE_API_TOKEN!,
  baseUrl: 'https://api.msgine.net/api/v1', // optional
  timeoutMs: 30000, // optional
  retryConfig: { // optional
    maxRetries: 3,
    initialDelayMs: 1000,
    maxDelayMs: 10000,
    backoffMultiplier: 2
  }
})

Parameters

ParameterTypeRequiredDescription
config.apiTokenstringYesYour MsGine API token
config.baseUrlstringNoAPI base URL (default: https://api.msgine.net/api/v1)
config.timeoutMsnumberNoRequest timeout in milliseconds (default: 30000)
config.retryConfigRetryConfigNoCustom retry configuration
config.onErrorfunctionNoError callback handler

Methods

sendSms(params)

Send an SMS message to one or more recipients.

typescript
const result = await client.sendSms({
  to: '+256701521269',
  message: 'Hello from MsGine!',
  from: 'MyApp', // optional
  callbackUrl: 'https://myapp.com/webhook' // optional
})

Parameters

ParameterTypeRequiredDescription
tostring | string[]YesRecipient phone number(s) in E.164 format
messagestringYesMessage content (max 1600 characters)
fromstringNoSender ID (default: your account sender ID)
callbackUrlstringNoWebhook URL for delivery status updates

Returns

Returns a Promise<SendSmsResponse>:

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

Example

typescript
try {
  const result = await client.sendSms({
    to: ['+256701521269', '+256701234567'],
    message: 'Welcome to MsGine!',
    from: 'MyApp'
  })

  console.log('Message sent:', result.id)
  console.log('Status:', result.status)
  console.log('Cost:', result.cost, result.currency)
} catch (error) {
  console.error('Failed to send SMS:', error)
}

getMessageStatus(messageId)

Retrieve the delivery status of a sent message.

typescript
const status = await client.getMessageStatus('msg_1234567890')

Parameters

ParameterTypeRequiredDescription
messageIdstringYesThe message ID returned from sendSms

Returns

Returns a Promise<MessageStatus>:

typescript
{
  id: string
  status: 'pending' | 'sent' | 'delivered' | 'failed'
  to: string[]
  updatedAt: string
  failureReason?: string
}

getAccount()

Retrieve your account information and current balance.

typescript
const account = await client.getAccount()

Returns

Returns a Promise<AccountInfo>:

typescript
{
  id: string
  email: string
  balance: number
  currency: string
  plan: string
  createdAt: string
}

listMessages(options)

List sent messages with pagination.

typescript
const messages = await client.listMessages({
  page: 1,
  limit: 20,
  status: 'delivered'
})

Parameters

ParameterTypeRequiredDescription
pagenumberNoPage number (default: 1)
limitnumberNoItems per page (default: 20, max: 100)
statusMessageStatusNoFilter by status

Returns

Returns a Promise<MessageListResponse>:

typescript
{
  data: SendSmsResponse[]
  pagination: {
    page: number
    limit: number
    total: number
    pages: number
  }
}

Configuration

Retry Configuration

Customize how the SDK retries failed requests:

typescript
const client = new MsGineClient({
  apiToken: process.env.MSGINE_API_TOKEN!,
  retryConfig: {
    maxRetries: 3,           // Maximum number of retries
    initialDelayMs: 1000,    // Initial delay between retries
    maxDelayMs: 10000,       // Maximum delay between retries
    backoffMultiplier: 2     // Exponential backoff multiplier
  }
})

Custom Error Handler

Add a custom error handler for logging and monitoring:

typescript
const client = new MsGineClient({
  apiToken: process.env.MSGINE_API_TOKEN!,
  onError: (error) => {
    // Send to monitoring service
    monitoringService.trackError({
      name: error.name,
      message: error.message,
      code: error.code,
      statusCode: error.statusCode
    })
  }
})

Type Definitions

See the complete Type Reference for all available types and interfaces.

Error Handling

See the Error Handling Guide for detailed information about handling errors.

Next Steps

Released under the MIT License.