Whatixo.Documentation English Ouvrir Whatixo
Documentation / API Whatixo

Exemples complets

Un petit client réutilisable dans votre langage, puis des cas d’usage concrets à adapter.

Un client en quelques lignes

// whatixo.js — Node.js 18+
const BASE = 'https://api.whatixo.com/api';

export async function whatixo(path, body = {}) {
  const res = await fetch(BASE + path, {
    method: 'POST',
    headers: {
      'x-api-key': process.env.WHATIXO_API_KEY,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(body),
  });
  const data = await res.json().catch(() => ({}));
  if (!res.ok) {
    const err = new Error(data.error || `HTTP ${res.status}`);
    err.status = res.status; err.code = data.code;
    throw err;
  }
  return data;
}

export const DEVICE_ID = process.env.WHATIXO_DEVICE_ID;
.envBASH
WHATIXO_API_KEY=wtx_votre_cle_secrete
WHATIXO_DEVICE_ID=8f2c1a7e-4b9d-4e21-9a6f-3c5d7e1b2a90

Confirmation de commande avec facture

import { whatixo, DEVICE_ID } from './whatixo.js';

export async function notifyOrder(order) {
  // 1. Enregistrer / mettre à jour le client dans le CRM (le tag peut lancer une séquence)
  await whatixo('/whatsapp/contacts/upsert', {
    phone: order.customer.phone,
    name: order.customer.name,
    tags: ['clients'],
    attributes: { derniere_commande: String(order.id) },
  });

  // 2. Message de confirmation
  await whatixo('/whatsapp/messages/send', {
    deviceId: DEVICE_ID,
    to: order.customer.phone,
    text: `Bonjour ${order.customer.name} 👋\nVotre commande *n°${order.id}* de ${order.total} $ est confirmée.`,
  });

  // 3. La facture en PDF
  await whatixo('/whatsapp/messages/send', {
    deviceId: DEVICE_ID,
    to: order.customer.phone,
    type: 'document',
    mediaUrl: order.invoiceUrl,          // https://…/facture-1042.pdf
    fileName: `Facture-${order.id}.pdf`,
  });
}

Code de vérification (OTP) par WhatsApp

Remplacez le SMS par WhatsApp pour vérifier le numéro de vos utilisateurs.

otp.jsJS
import crypto from 'node:crypto';
import { whatixo, DEVICE_ID } from './whatixo.js';

const pending = new Map(); // en production : Redis ou base de données

export async function sendOtp(phone) {
  const code = String(crypto.randomInt(100000, 1000000));
  const hash = crypto.createHash('sha256').update(code).digest('hex');
  pending.set(phone, { hash, expires: Date.now() + 10 * 60_000, tries: 0 });
  await whatixo('/whatsapp/messages/send', {
    deviceId: DEVICE_ID,
    to: phone,
    text: `Votre code de vérification : *${code}*\nIl expire dans 10 minutes. Ne le partagez avec personne.`,
  });
}

export function verifyOtp(phone, code) {
  const entry = pending.get(phone);
  if (!entry || entry.expires < Date.now() || ++entry.tries > 5) return false;
  const ok = crypto.createHash('sha256').update(String(code)).digest('hex') === entry.hash;
  if (ok) pending.delete(phone);
  return ok;
}

Rappels de rendez-vous (tâche planifiée)

rappels.py — à lancer chaque matin (cron)PYTHON
from datetime import date, timedelta
import time
from whatixo import whatixo, DEVICE_ID, WhatixoError

demain = date.today() + timedelta(days=1)
rdvs = charger_rendez_vous(demain)  # votre fonction : [{"nom", "telephone", "heure"}]

for rdv in rdvs:
    try:
        whatixo("/whatsapp/messages/send", {
            "deviceId": DEVICE_ID,
            "to": rdv["telephone"],
            "text": f"Bonjour {rdv['nom']}, rappel de votre rendez-vous demain à {rdv['heure']} ⏰\n"
                    "Répondez 1 pour confirmer, 2 pour annuler.",
        })
    except WhatixoError as e:
        print("Échec", rdv["telephone"], e.status, e)
    time.sleep(5)  # rythme raisonnable entre deux envois

Pour gérer les réponses « 1 » et « 2 » sans code, créez deux workflows à mot-clé (1 et 2, règle est exactement) qui répondent et taguent le contact.