Samva
SDKs & Tools

TypeScript SDK

Official TypeScript/JavaScript SDK for Samva API

The official TypeScript SDK provides a type-safe, modern interface for integrating with Samva's messaging platform.

Installation

npm install samva

Or with your preferred package manager:

# yarn
yarn add samva

# bun
bun add samva

# pnpm
pnpm add samva

Quick Start

import { createSamvaClient } from "samva";

const samva = createSamvaClient({
  apiKey: process.env.SAMVA_API_KEY!,
});

// Send an email
const emailResult = await samva.email.send({
  body: {
    to: "user@example.com",
    subject: "Welcome to Samva",
    html: "<h1>Hello!</h1><p>Welcome to our platform.</p>",
    text: "Hello! Welcome to our platform.",
  },
});

console.log("Email sent:", emailResult.data?.id);

// Send SMS
const smsResult = await samva.sms.send({
  body: {
    to: "+14155552671",
    message: "Your verification code is 123456",
  },
});

console.log("SMS sent:", smsResult.data?.id);

Features

  • Type-Safe: Full TypeScript support with auto-completion
  • Auto-Generated: Built from our OpenAPI specification
  • Modern: Async/await support throughout
  • Reliable: Automatic retries for transient failures
  • Lightweight: Minimal dependencies

API Methods

Email

// Send email
await samva.email.send({
  body: {
    to: "user@example.com",
    subject: "Hello",
    html: "<p>Hi there!</p>",
  },
});

// List email templates
const templates = await samva.email.listTemplates({});

// Get email stats
const stats = await samva.email.getStats({
  query: { startDate: "2024-01-01" },
});

SMS

// Send SMS
await samva.sms.send({
  body: {
    to: "+14155552671",
    message: "Your code is 123456",
  },
});

// Get SMS stats
const stats = await samva.sms.getStats({});

WhatsApp

// Send WhatsApp message
await samva.whatsapp.send({
  body: {
    to: "+14155552671",
    message: "Hello from Samva!",
  },
});

// Send template message
await samva.whatsapp.sendTemplate({
  body: {
    to: "+14155552671",
    templateId: "welcome_template",
    variables: { name: "John" },
  },
});

Messages

// List all messages
const messages = await samva.messages.list({
  query: { channel: "email", limit: 20 },
});

// Get message by ID
const message = await samva.messages.get({
  path: { id: "msg_123" },
});

// Get message events
const events = await samva.messages.getEvents({
  path: { id: "msg_123" },
});

Error Handling

const result = await samva.email.send({
  body: {
    to: "user@example.com",
    subject: "Test",
    text: "Hello",
  },
});

if (result.error) {
  console.error("Failed to send:", result.error.message);
} else {
  console.log("Sent:", result.data?.id);
}

Configuration Options

const samva = createSamvaClient({
  apiKey: process.env.SAMVA_API_KEY!,
  // Optional: Override base URL for testing
  baseUrl: "https://api.samva.app",
});

Environment Variables

We recommend storing your API key in environment variables:

# .env
SAMVA_API_KEY=sk_live_your_api_key_here
import { createSamvaClient } from "samva";

const samva = createSamvaClient({
  apiKey: process.env.SAMVA_API_KEY!,
});

Next Steps

On this page