TypeScript SDK
Integrate Smobi into your TypeScript/JavaScript applications
Installation
Install the SDK using npm or yarn:
$ npm install smobi
Or with yarn:
$ yarn add smobi
Basic Setup
Initialize the Client
1 import { SmobiClient } from "smobi"; 2 3 const client = new SmobiClient({ 4 apiKey: "your-api-key-here", // Replace with your actual API key 5 baseUrl: "https://api.smobi.com" // Optional, defaults to production 6 });
Environment Variables
For security, store your API key in environment variables:
1 import { SmobiClient } from "smobi"; 2 import * as dotenv from 'dotenv'; 3 4 // Load environment variables 5 dotenv.config(); 6 7 const client = new SmobiClient({ 8 apiKey: process.env.SMOBI_API_KEY, 9 baseUrl: process.env.SMOBI_BASE_URL || 'https://api.smobi.com' 10 });
Sending Messages
Simple Text Message
1 async function sendTextMessage() { 2 try { 3 const result = await client.messages.send({ 4 clientRef: "unique-client-reference", 5 recipientPhoneNumber: "15552223333", 6 content: { 7 rcs: { 8 content: { 9 type: "TEXT", 10 content: "Hello from Smobi!", 11 globalSuggestions: [ 12 { 13 id: "reply-1", 14 text: "Hello!", 15 type: "REPLY" 16 } 17 ] 18 } 19 }, 20 sms: { 21 content: "Hello from Smobi!" 22 } 23 } 24 }); 25 26 console.log("Message sent:", result.smobiMessageId); 27 console.log("Status:", result.status); 28 } catch (error) { 29 console.error("Error sending message:", error); 30 } 31 }
Rich Card Message
1 async function sendRichCard() { 2 try { 3 const result = await client.messages.send({ 4 clientRef: "product-card-test", 5 recipientPhoneNumber: "15552223333", 6 content: { 7 rcs: { 8 content: { 9 type: "RICH_CARD", 10 item: { 11 title: "Blue Cotton T-Shirt", 12 description: "Premium comfort with style. Available in S-XL sizes.", 13 mediaUrl: "https://example.com/product-image.jpg", 14 orientation: "VERTICAL", 15 thumbnailPosition: "RIGHT", 16 height: "SHORT", 17 localSuggestions: [ 18 { 19 id: "buy-now", 20 text: "Buy Now", 21 type: "OPEN_URL", 22 url: "https://example.com/buy" 23 } 24 ] 25 }, 26 globalSuggestions: [ 27 { 28 id: "reply-1", 29 text: "Reply to message", 30 type: "REPLY" 31 }, 32 { 33 id: "url-1", 34 text: "Visit our website", 35 type: "OPEN_URL", 36 url: "https://example.com" 37 } 38 ] 39 } 40 }, 41 sms: { 42 content: "Check out our Blue Cotton T-Shirt! View it here: https://example.com/products/blue-tshirt" 43 } 44 } 45 }); 46 47 console.log("Rich card sent:", result.smobiMessageId); 48 } catch (error) { 49 console.error("Error sending rich card:", error); 50 } 51 }
Carousel Message
1 async function sendCarousel() { 2 try { 3 const result = await client.messages.send({ 4 clientRef: "carousel-test", 5 recipientPhoneNumber: "15552223333", 6 content: { 7 rcs: { 8 content: { 9 type: "CAROUSEL", 10 width: "MEDIUM", 11 items: [ 12 { 13 title: "Product 1", 14 description: "Description for product 1", 15 mediaUrl: "https://example.com/product1.jpg", 16 orientation: "VERTICAL", 17 thumbnailPosition: "RIGHT", 18 height: "SHORT" 19 }, 20 { 21 title: "Product 2", 22 description: "Description for product 2", 23 mediaUrl: "https://example.com/product2.jpg", 24 orientation: "VERTICAL", 25 thumbnailPosition: "RIGHT", 26 height: "SHORT" 27 } 28 ], 29 globalSuggestions: [ 30 { 31 id: "browse-more", 32 text: "Browse More", 33 type: "OPEN_URL", 34 url: "https://example.com/products" 35 } 36 ] 37 } 38 }, 39 sms: { 40 content: "Check out our products: https://example.com/products" 41 } 42 } 43 }); 44 45 console.log("Carousel sent:", result.smobiMessageId); 46 } catch (error) { 47 console.error("Error sending carousel:", error); 48 } 49 }
Workflow Execution
Workflow definition IDs can be found in the workflow builder UI, by using the Copy ID action next to the workflow name.
Execute One
1 async function executeWorkflow() { 2 try { 3 const result = await client.workflows.executeWorkflowSingle( 4 "workflow-definition-id", // Your workflow ID 5 "15552223333", // Phone number 6 { 7 variables: { 8 customerName: "John Doe", 9 orderId: "12345", 10 discountCode: "SAVE20" 11 } 12 } 13 ); 14 15 console.log("Workflow executed:", result.workflowExecutionId); 16 console.log("Success:", result.success); 17 } catch (error) { 18 console.error("Error executing workflow:", error); 19 } 20 }
Execute a Batch
1 async function executeBatchWorkflow() { 2 try { 3 const result = await client.workflows.executeWorkflowBatch( 4 "workflow-definition-id", 5 { 6 executions: [ 7 { 8 phoneNumber: "15552223333", 9 variables: { 10 "customer.name": { name: "customer.name", value: "John Doe" }, 11 "customer.phone": { name: "customer.phone", value: "15552223333" } 12 } 13 }, 14 { 15 phoneNumber: "15552223334", 16 variables: { 17 "customer.name": { name: "customer.name", value: "Jane Smith" }, 18 "customer.phone": { name: "customer.phone", value: "15552223334" } 19 } 20 } 21 ] 22 } 23 ); 24 25 console.log("Batch executed successfully"); 26 console.log("Created:", result.executions.results.created); 27 console.log("Errors:", result.executions.results.errors); 28 } catch (error) { 29 console.error("Error executing batch workflow:", error); 30 } 31 }
Message Management
Get Message Status
1 async function getMessageStatus(messageId: string) { 2 try { 3 const message = await client.messages.get(messageId); 4 5 console.log("Message ID:", message.smobiMessageId); 6 console.log("Status:", message.status); 7 console.log("Channel:", message.channel); 8 console.log("Content:", message.content); 9 } catch (error) { 10 console.error("Error getting message:", error); 11 } 12 }
Check Channel Support
1 async function checkChannelSupport(phoneNumber: string, senderId: string) { 2 try { 3 const support = await client.channels.checkSupport(phoneNumber, senderId); 4 5 console.log("RCS Support:", support.rcs); 6 console.log("SMS Support:", support.sms); 7 } catch (error) { 8 console.error("Error checking channel support:", error); 9 } 10 }
Next Steps
- Workflow Builder - Learn how to create workflows
- Webhooks Setup - Set up real-time notifications
- API Reference - Explore the full API documentation
- Troubleshooting - Common issues and solutions