G K G Guru

OCaml SMS API Integration

This guide provides comprehensive OCaml code examples for integrating with the SecureSMSC SMS API using Cohttp.

Fast Integration

Get started in minutes with simple REST API calls

Global Reach

Send SMS to 200+ countries worldwide

Secure

Enterprise-grade security with SSL/TLS encryption

Reliable

99.9% uptime with delivery tracking

Quick Start: All examples use the REST API v2 endpoint. Get your API key from the dashboard to begin.

Installation

Install the required HTTP library for making API requests:

Terminal
# Install Cohttp for HTTP requests
# Check your package manager documentation for specific commands

Send Single SMS

Send a single SMS message using the API:

OCaml
# OCaml SMS API Integration
# Using Cohttp for HTTP requests

# API Configuration
api_key = "YOUR_API_KEY"
api_url = "https://whitelabled.securesmsc.com/api/v2/sms/send"

# Request Headers
headers = {
    "Authorization": "Bearer " + api_key,
    "Content-Type": "application/json"
}

# SMS Data
data = {
    "sender_id": "SENDER",
    "recipient": "1234567890",
    "message": "Hello from OCaml!",
    "route": "transactional"
}

# Make POST request to send SMS
# Use Cohttp to send the request
response = http_post(api_url, headers, data)

# Handle response
if response.status == 200:
    print("SMS sent successfully!")
    print("Message ID:", response.body.data.message_id)
else:
    print("Error:", response.body.error)

Send Bulk SMS

Send SMS to multiple recipients in a single request:

OCaml
# Bulk SMS - Send to multiple recipients

api_url = "https://whitelabled.securesmsc.com/api/v2/sms/send-bulk"

# Multiple recipients
data = {
    "sender_id": "SENDER",
    "recipients": ["1234567890", "0987654321", "1122334455"],
    "message": "Bulk message from OCaml",
    "route": "promotional"
}

response = http_post(api_url, headers, data)
print("Bulk SMS Status:", response.status)

Check SMS Status

Query the delivery status of a sent message:

OCaml
# Check SMS delivery status

message_id = "your_message_id"
status_url = "https://whitelabled.securesmsc.com/api/v2/sms/status/" + message_id

response = http_get(status_url, headers)

if response.status == 200:
    print("Delivery Status:", response.body.data.status)
    print("Delivered At:", response.body.data.delivered_at)
else:
    print("Error checking status")

Check Account Balance

Query your current SMS credit balance:

OCaml
# Get account balance

balance_url = "https://whitelabled.securesmsc.com/api/v2/balance"

response = http_get(balance_url, headers)

if response.status == 200:
    print("Available Credits:", response.body.data.balance)
    print("Currency:", response.body.data.currency)
else:
    print("Error fetching balance")

Complete Integration Examples

Error Handling

Implement robust error handling for production use:

OCaml
# OCaml SMS Integration with Error Handling

function send_sms_with_retry(recipient, message, max_retries = 3):
    api_url = "https://whitelabled.securesmsc.com/api/v2/sms/send"
    
    for attempt in range(max_retries):
        try:
            response = http_post(api_url, headers, {
                "sender_id": "SENDER",
                "recipient": recipient,
                "message": message,
                "route": "transactional"
            })
            
            if response.status == 200:
                return {
                    "success": true,
                    "message_id": response.body.data.message_id
                }
            
            # Wait before retry (exponential backoff)
            sleep(2 ^ attempt)
            
        catch error:
            if attempt == max_retries - 1:
                return {"success": false, "error": str(error)}
            sleep(2 ^ attempt)
    
    return {"success": false, "error": "Max retries exceeded"}

# Usage
result = send_sms_with_retry("1234567890", "Hello from OCaml!")
if result.success:
    print("Message ID:", result.message_id)
else:
    print("Failed:", result.error)

Framework Integration

Integrate with popular OCaml frameworks (Dream, Opium):

OCaml - Framework Integration
# SMS Service Module for OCaml

module SmsService:
    api_key = get_env("SMS_API_KEY")
    base_url = "https://whitelabled.securesmsc.com/api/v2"
    
    function get_headers():
        return {
            "Authorization": "Bearer " + api_key,
            "Content-Type": "application/json"
        }
    
    function send(recipient, message, route = "transactional"):
        url = base_url + "/sms/send"
        data = {
            "sender_id": "SENDER",
            "recipient": recipient,
            "message": message,
            "route": route
        }
        
        response = http_post(url, get_headers(), data)
        return response.body
    
    function send_bulk(recipients, message, route = "promotional"):
        url = base_url + "/sms/send-bulk"
        data = {
            "sender_id": "SENDER",
            "recipients": recipients,
            "message": message,
            "route": route
        }
        
        response = http_post(url, get_headers(), data)
        return response.body
    
    function get_status(message_id):
        url = base_url + "/sms/status/" + message_id
        response = http_get(url, get_headers())
        return response.body
    
    function get_balance():
        url = base_url + "/balance"
        response = http_get(url, get_headers())
        return response.body

# Usage in your application
import SmsService

result = SmsService.send("1234567890", "Hello from OCaml!")
print(result)

Next Steps