waiaconnect

Documentazione / Completo

Percorso dell'integratore: attivazione e webhook 100 % via API

GET /v1/onboarding-links/{id}

Markdown

Programmi con l'IA? Collegala al tuo account via MCP: controlla la tua configurazione, crea webhook e modelli con la tua conferma e scrive il codice. Scopri come →

Se attivi i tuoi clienti dal tuo sistema, senza entrare nel pannello, questo è il percorso completo, in cinque passi.

1. Un link per cliente. POST /v1/onboarding-links con il tuo externalId (il tuo id per quel cliente) e webhook: { url }. Ti restituiamo l'url per il tuo cliente e il segreto whsec_… del webhook una sola volta: salvalo subito, firma tutto ciò che riguarda quel cliente. Un externalId già usato da un altro numero del tuo account dà 409 in questo momento, non alla fine dell'attivazione.

2. Il tuo cliente si collega. Apre l'url, entra con il suo account Meta e sceglie il numero. Alla fine, il numero nasce con il suo webhook (l'URL del link) e lì arriva onboarding.completed, con connection.id (conn_…), il tuo externalId, reconnected e il webhookId. Un numero nuovo porta anche connection.created, come sempre.

3. Se non finisce, lo sai lo stesso. Ogni tentativo interrotto (fallito, annullato o abbandonato) manda onboarding.failed all'URL del webhook del link, firmato con lo stesso segreto: reason (meta_error, coex_gate (Meta non ha abilitato la Coesistenza per il portafoglio scelto dal tuo cliente), incomplete, timeout, cancelled, abandoned…), metaErrorCode, metaMessage e metaSessionId (il session_id che chiede il supporto di Meta) se Meta li ha dati, e linkUsable (se il tuo cliente può riprovare con lo stesso link). attemptId identifica il tentativo. «Annulla e ricomincia» non lo invia: non è un esito, il tentativo successivo porta quello reale. Se preferisci chiedere: GET /v1/onboarding-links?externalId=… o GET /v1/onboarding-links/{id} restituiscono outcome (pending, completed o failed, con lo stesso motivo).

4. Il webhook del numero è tuo. Lo vedi e lo modifichi con /v1/webhooks?externalId=…: URL, eventi, intestazione fissa, ruotare il segreto, provarlo.

5. La disattivazione. Quando il numero si scollega (dal pannello, perché l'azienda ci ha tolto l'accesso su Meta o perché il permesso è stato revocato), il suo webhook viene spento (active: false, disabledReason: "connection_disconnected") e smette di ricevere. Se si ricollega lo stesso numero, si riaccende da solo; quelli che hai messo in pausa tu o che abbiamo spento per errori, no. Per scollegarlo dal tuo sistema: DELETE /v1/connections/{id} (conn_… o il tuo externalId). È come «Scollega» nel pannello: smette di passare per WAIA Connect, non cancella niente in Meta (il numero resta nell'account WhatsApp del tuo cliente) e libera subito il posto nel tuo piano (slots nella risposta; il prezzo del piano non cambia). I tuoi webhook di account ricevono connection.disconnected, e anche il webhook del numero se lo chiede (lo riceve anche se lo scollegamento lo spegne). Ripeterlo restituisce 200 con lo stesso corpo; un numero di un altro account, 404.

# 1) one link per end-customer: your id + the webhook its number will have
curl -X POST https://api.waiaconnect.com/v1/onboarding-links \
  -H "Authorization: Bearer wc_live_YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"externalId": "pharmacy-42", "label": "Pharmacy 42", "mode": "coexistence",
       "webhook": {"url": "https://api.yourcompany.com/hooks/connect"}}'
# → 201 { "publicId": "onb_…", "url": "https://…/c#<token>", "expiresAt": "…",
#         "webhook": { "secret": "whsec_…", "createdOnCompletion": true, … } }   ← store the secret NOW

# 2) send "url" to your customer. Then, on https://api.yourcompany.com/hooks/connect:
#    { "type": "onboarding.completed", "connection": { "id": "conn_…", "externalId": "pharmacy-42", … },
#      "data": { "onboardingLinkId": "onb_…", "reconnected": false, "webhookId": "wh_…", … } }
#    { "type": "onboarding.failed", "connection": { "id": null, … },
#      "data": { "externalId": "pharmacy-42", "reason": "meta_error", "metaErrorCode": 2655093,
#                "metaMessage": "The business is already sharing…", "metaSessionId": "01a1…",
#                "linkUsable": true, … } }

# 3) or ask (polling) by your id
curl "https://api.waiaconnect.com/v1/onboarding-links?externalId=pharmacy-42" -H "Authorization: Bearer wc_live_YOUR_API_KEY"
# → { "links": [ { "id": "onb_…", "status": "used", "connectionId": "conn_…",
#       "outcome": { "state": "completed", "connectionId": "conn_…", "webhookId": "wh_…", … } } ] }
curl https://api.waiaconnect.com/v1/onboarding-links/onb_… -H "Authorization: Bearer wc_live_YOUR_API_KEY"

# 4) the number's webhook, by your id (PATCH to change url/events, rotate-secret, test)
curl "https://api.waiaconnect.com/v1/webhooks?externalId=pharmacy-42" -H "Authorization: Bearer wc_live_YOUR_API_KEY"

# 5) offboarding, by your id (or conn_…): the same as «Disconnect» in the dashboard
curl -X DELETE https://api.waiaconnect.com/v1/connections/pharmacy-42 -H "Authorization: Bearer wc_live_YOUR_API_KEY"
# → 200 { "id": "conn_…", "externalId": "pharmacy-42", "status": "disconnected",
#         "disconnectedAt": "…", "slots": { "used": 2, "limit": 5 }, "connection": { … } }
#    its webhook switches off (disabledReason: "connection_disconnected"); your account webhooks get
#    connection.disconnected. Again → 200, same body. Reconnecting the number switches it back on.
const H = { "Authorization": "Bearer wc_live_YOUR_API_KEY", "Content-Type": "application/json" };

// 1) one link per end-customer: your id + the webhook its number will have
const link = await (await fetch("https://api.waiaconnect.com/v1/onboarding-links", {
  method: "POST", headers: H,
  body: JSON.stringify({ externalId: "pharmacy-42", label: "Pharmacy 42", mode: "coexistence",
    webhook: { url: "https://api.yourcompany.com/hooks/connect" } })
})).json();
await saveSecret("pharmacy-42", link.webhook.secret); // shown ONCE — it signs this customer's events
await sendToCustomer(link.url);

// 2) your webhook handler (verify the signature first — see «Verify the signature»)
app.post("/hooks/connect", express.raw({ type: "application/json" }), async (req, res) => {
  const evt = JSON.parse(req.body);
  if (evt.type === "onboarding.completed") await markConnected(evt.data.externalId, evt.connection.id);
  if (evt.type === "onboarding.failed") await tellCustomer(evt.data.externalId, evt.data.reason, evt.data.metaErrorCode, evt.data.linkUsable);
  res.sendStatus(200);
});

// 3) or poll by your id
const { links } = await (await fetch("https://api.waiaconnect.com/v1/onboarding-links?externalId=pharmacy-42", { headers: H })).json();
console.log(links[0]?.outcome); // { state: "pending" | "completed" | "failed", reason, metaErrorCode, metaSessionId, … }

// 5) offboarding, by your id (or conn_…) — idempotent: again → 200, same body
const off = await (await fetch("https://api.waiaconnect.com/v1/connections/pharmacy-42", { method: "DELETE", headers: H })).json();
console.log(off.status, off.slots); // "disconnected" { used: 2, limit: 5 }
<?php
function connect($method, $url, $body = null) {
  $ch = curl_init($url);
  curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => $method,
    CURLOPT_HTTPHEADER => ["Authorization: Bearer wc_live_YOUR_API_KEY", "Content-Type: application/json"],
    CURLOPT_POSTFIELDS => $body ? json_encode($body) : null,
  ]);
  return json_decode(curl_exec($ch), true);
}
// 1) one link per end-customer: your id + the webhook its number will have
$link = connect("POST", "https://api.waiaconnect.com/v1/onboarding-links", [
  "externalId" => "pharmacy-42", "label" => "Pharmacy 42", "mode" => "coexistence",
  "webhook" => ["url" => "https://api.yourcompany.com/hooks/connect"],
]);
save_secret("pharmacy-42", $link["webhook"]["secret"]); // shown ONCE

// 2) in your webhook handler: onboarding.completed / onboarding.failed (data.reason, data.metaErrorCode)
// 3) or poll by your id
$links = connect("GET", "https://api.waiaconnect.com/v1/onboarding-links?externalId=pharmacy-42")["links"];
$outcome = $links[0]["outcome"] ?? null; // state: pending | completed | failed

// 5) offboarding, by your id (or conn_…) — idempotent: again → 200, same body
$off = connect("DELETE", "https://api.waiaconnect.com/v1/connections/pharmacy-42"); // status, disconnectedAt, slots
import requests
H = {"Authorization": "Bearer wc_live_YOUR_API_KEY"}

# 1) one link per end-customer: your id + the webhook its number will have
link = requests.post("https://api.waiaconnect.com/v1/onboarding-links", headers=H, json={
    "externalId": "pharmacy-42", "label": "Pharmacy 42", "mode": "coexistence",
    "webhook": {"url": "https://api.yourcompany.com/hooks/connect"},
}).json()
save_secret("pharmacy-42", link["webhook"]["secret"])  # shown ONCE

# 2) in your webhook handler: onboarding.completed / onboarding.failed (data.reason, data.metaErrorCode)
# 3) or poll by your id
links = requests.get("https://api.waiaconnect.com/v1/onboarding-links", headers=H, params={"externalId": "pharmacy-42"}).json()["links"]
outcome = links[0]["outcome"] if links else None  # state: pending | completed | failed

# 5) offboarding, by your id (or conn_…) — idempotent: again -> 200, same body
off = requests.delete("https://api.waiaconnect.com/v1/connections/pharmacy-42", headers=H).json()  # status, disconnectedAt, slots

Prima della prima campagna di un numero nuovo: GET /v1/connections/{id}/sending-status dice se può avviare conversazioni e cosa manca al tuo cliente su Meta (vedi Stato di invio).

Senza webhook nel link, onboarding.completed e onboarding.failed vanno ai webhook dell'account (in onboarding.failed, connection.id è null: il tuo externalId è in data).

Un webhook con events esplicito che non include questi tipi non li riceve; uno con tutti gli eventi inizia a riceverli.

Nei link di attivazione, connectionId ora esce come conn_…; l'id interno resta in legacyConnectionId, che verrà ritirato.

outcome.state = failed anche quando il link è scaduto (link_expired) o l'hai annullato (link_revoked) prima che il cliente finisse.

Il tuo cliente vende con il catalogo di WhatsApp? I carrelli arrivano dallo stesso webhook: vedi Carrelli del catalogo.