SDK & Librerie

SDK ufficiali e snippet per integrare UniMsg con i principali linguaggi di programmazione.

PHP

Installazione

composer require unimsg/php-sdk

Utilizzo

<?php
require 'vendor/autoload.php';

use UniMsg\Client;

$client = new Client([
    'client_id' => 'demo_xxxxxxxxxxxx',
    'client_secret' => 'sk_test_xxxxxxxxxxxx'
]);

// Invio SMS
$result = $client->sms->send([
    'to' => '+39123456789',
    'message' => 'Ciao da UniMsg!',
    'sender' => 'MyApp'
]);

if ($result->success) {
    echo "Messaggio inviato: " . $result->data->message_id;
}

// Invio WhatsApp Template
$result = $client->whatsapp->sendTemplate([
    'to' => '+39123456789',
    'template' => 'order_confirmation',
    'language' => 'it',
    'parameters' => ['Mario', 'ORD-123', '€99.00']
]);

// Invio Email
$result = $client->email->send([
    'to' => 'test@example.com',
    'from' => 'noreply@myapp.com',
    'subject' => 'Benvenuto!',
    'html' => '<h1>Ciao!</h1>'
]);

// Controllo saldo
$balance = $client->account->getBalance();
echo "Crediti: " . $balance->data->available;

Python

Installazione

pip install unimsg

Utilizzo

from unimsg import UniMsgClient

client = UniMsgClient(
    client_id='demo_xxxxxxxxxxxx',
    client_secret='sk_test_xxxxxxxxxxxx'
)

# Invio SMS
result = client.sms.send(
    to='+39123456789',
    message='Ciao da UniMsg!',
    sender='MyApp'
)

if result.success:
    print(f"Messaggio inviato: {result.data.message_id}")

# Invio WhatsApp Template
result = client.whatsapp.send_template(
    to='+39123456789',
    template='order_confirmation',
    language='it',
    parameters=['Mario', 'ORD-123', '€99.00']
)

# Invio Email
result = client.email.send(
    to='test@example.com',
    from_email='noreply@myapp.com',
    subject='Benvenuto!',
    html='<h1>Ciao!</h1>'
)

# Controllo saldo
balance = client.account.get_balance()
print(f"Crediti: {balance.data.available}")

Node.js

Installazione

npm install @unimsg/node-sdk

Utilizzo

const UniMsg = require('@unimsg/node-sdk');

const client = new UniMsg({
    clientId: 'demo_xxxxxxxxxxxx',
    clientSecret: 'sk_test_xxxxxxxxxxxx'
});

// Invio SMS
const result = await client.sms.send({
    to: '+39123456789',
    message: 'Ciao da UniMsg!',
    sender: 'MyApp'
});

if (result.success) {
    console.log(`Messaggio inviato: ${result.data.messageId}`);
}

// Invio WhatsApp Template
const waResult = await client.whatsapp.sendTemplate({
    to: '+39123456789',
    template: 'order_confirmation',
    language: 'it',
    parameters: ['Mario', 'ORD-123', '€99.00']
});

// Invio Email
const emailResult = await client.email.send({
    to: 'test@example.com',
    from: 'noreply@myapp.com',
    subject: 'Benvenuto!',
    html: '<h1>Ciao!</h1>'
});

// Controllo saldo
const balance = await client.account.getBalance();
console.log(`Crediti: ${balance.data.available}`);

cURL Examples

Per linguaggi senza SDK, usa direttamente l'API REST:

Ottenere Token

curl -X POST https://api.unimsg.app/oauth/token \
  -d "grant_type=client_credentials" \
  -d "client_id=demo_xxxxxxxxxxxx" \
  -d "client_secret=sk_test_xxxxxxxxxxxx"

Inviare SMS

curl -X POST https://api.unimsg.app/v1/sms/send \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"to": "+39123456789", "message": "Test", "sender": "MyApp"}'

Inviare Email

curl -X POST https://api.unimsg.app/v1/email/send \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "to": "test@example.com",
    "from": "noreply@myapp.com",
    "subject": "Test",
    "html": "<p>Hello!</p>"
  }'

Webhook Helper

Utility per verificare webhook:

PHP

use UniMsg\Webhook;

$webhook = new Webhook('your_webhook_secret');

try {
    $event = $webhook->verify(
        file_get_contents('php://input'),
        $_SERVER['HTTP_X_UNIMSG_SIGNATURE'],
        $_SERVER['HTTP_X_UNIMSG_TIMESTAMP']
    );

    // Evento verificato
    switch ($event->type) {
        case 'message.delivered':
            // ...
            break;
    }
} catch (WebhookVerificationException $e) {
    http_response_code(401);
    exit;
}

Node.js

const { WebhookHelper } = require('@unimsg/node-sdk');

const webhook = new WebhookHelper('your_webhook_secret');

app.post('/webhook', (req, res) => {
    try {
        const event = webhook.verify(
            req.rawBody,
            req.headers['x-unimsg-signature'],
            req.headers['x-unimsg-timestamp']
        );

        // Evento verificato
        console.log(event.type, event.data);
        res.status(200).send('OK');
    } catch (e) {
        res.status(401).send('Invalid');
    }
});

Community Libraries

Librerie sviluppate dalla community (non supportate ufficialmente):

Linguaggio Repository Autore
Go github.com/example/unimsg-go Community
Ruby gem install unimsg Community
Java com.unimsg:sdk Community
Nota: Le librerie community non sono mantenute da UniMsg. Verifica compatibilità e sicurezza prima dell'uso in produzione.