Node.js SDK

v1.2.0GitHub

Official Node.js SDK for TXTLINK. TypeScript support included.

Installation

bash
npm install @txtlink/sdk

Quick Example

javascript
import { TXTLINK } from '@txtlink/sdk';

const client = new TXTLINK({
  apiKey: process.env.TXTLINK_API_KEY,
});

// Send SMS
const result = await client.sms.send({
  to: '+254712345678',
  message: 'Hello from TXTLINK!',
  senderId: 'TXTLINK',
});

console.log(result);

Advanced Usage

javascript
import { TXTLINK } from '@txtlink/sdk';

const client = new TXTLINK({
  apiKey: process.env.TXTLINK_API_KEY,
  timeout: 30000, // 30 seconds
});

// Send bulk SMS
const results = await client.sms.sendBulk({
  messages: [
    { to: '+254712345678', message: 'Hello 1' },
    { to: '+254712345679', message: 'Hello 2' },
  ],
  senderId: 'TXTLINK',
});

// Handle webhooks
app.post('/webhook', async (req, res) => {
  const signature = req.headers['x-txtlink-signature'];
  const isValid = client.webhooks.verify(req.body, signature);
  
  if (isValid) {
    const event = req.body;
    // Handle event
  }
  
  res.status(200).send('OK');
});

Error Handling

javascript
import { TXTLINK, TXTLINKError } from '@txtlink/sdk';

try {
  const result = await client.sms.send({
    to: '+254712345678',
    message: 'Hello',
    senderId: 'TXTLINK',
  });
} catch (error) {
  if (error instanceof TXTLINKError) {
    console.error('API Error:', error.message);
    console.error('Status Code:', error.statusCode);
    console.error('Error Code:', error.code);
  } else {
    console.error('Unexpected error:', error);
  }
}

TypeScript Support

The SDK includes full TypeScript definitions. No additional types package needed.

TypeScript

All types are exported from the main package. Import types directly:
typescript
import { TXTLINK, SMSResult, WebhookEvent } from '@txtlink/sdk';

Versioning

The SDK follows semantic versioning. We recommend pinning to a specific version in production:

bash
npm install @txtlink/sdk@1.2.0