WhatsApp official API in Google Sheets: Apps Script
Send messages from Google Sheets via the official WhatsApp API with Apps Script and CRPRO Hub: UrlFetchApp, quotas, pacing, and why receiving there is fragile.
Google Apps Script lets you connect a spreadsheet to the CRPRO Hub API with no server and nothing to install: UrlFetchApp makes the call, and a menu or trigger fires the send. For sending, it works well. For receiving webhooks, it is fragile, and the reason is a limit that the Apps Script documentation makes visible. This article shows the complete send, the quotas that matter, and where to stop insisting on receiving.
What Apps Script does well and badly in this setup
Short answer: it does batch sending from a spreadsheet well, with the key stored in the project itself. It does webhook receiving badly, because the web app documentation lists no way to read request headers.
According to Google's web apps guide, a POST request runs doPost(e), and the e object carries queryString, parameter, parameters, pathInfo, contentLength and postData, with contents, type and length. None of those fields is a header. Since the Hub signs each delivery in the X-Hub-Signature-256 header, without reading headers you cannot verify the signature. The documentation describes no header access, so treat it as unavailable until you prove otherwise.
How to send a message with UrlFetchApp
Short answer: UrlFetchApp.fetch with method post, contentType application/json, the Authorization: Bearer header, the Idempotency-Key header and the body in payload. The Hub answers 202.
- Create the API key in the Hub dashboard with the messages:send scope (authentication). The value is shown only once.
- Store the key and the channel id in Script Properties, with PropertiesService.getScriptProperties(), not in the sheet or in code.
- Pass muteHttpExceptions as true. The documentation says failure response codes throw an error by default; with true you get the response and can read the Hub error body.
- The payload accepts a string; use JSON.stringify and set contentType, because UrlFetchApp defaults to form encoding.
- Use the number with the country code and digits only in to.
// Extensions > Apps Script. Guarde CRPRO_API_KEY e CANAL_ID em Propriedades do script.
function enviarTexto(para, texto, chave) {
const props = PropertiesService.getScriptProperties();
const resposta = UrlFetchApp.fetch(
'https://crprohub.com/api/v1/channels/' + props.getProperty('CANAL_ID') + '/messages',
{
method: 'post',
contentType: 'application/json',
headers: {
Authorization: 'Bearer ' + props.getProperty('CRPRO_API_KEY'),
'Idempotency-Key': chave,
},
payload: JSON.stringify({ to: para, type: 'text', text: { body: texto } }),
muteHttpExceptions: true, // sem isso, 4xx/5xx viram excecao e perdem o corpo
}
);
return { status: resposta.getResponseCode(), corpo: resposta.getContentText() };
}Batch sending: quotas, pacing and idempotency
Short answer: the bottleneck is the Hub pace, 60 messages per minute per channel, and Apps Script run time, 6 minutes. With a 1.1-second pause between sends, about 300 messages fit in one run.
The Apps Script quotas page reports, for personal accounts, 20,000 URL Fetch calls per day and, for Google Workspace accounts, 100,000; 6 minutes of runtime per execution; and 50 MB per call, both for the response and the posted body. The 300-per-run figure is an estimate: 360 seconds divided by 1.1 seconds per send, minus some slack. For larger lists, rerun the function and use the status column to resume where it stopped.
// Colunas: A telefone | B texto | C status (preenchida pelo script)
function enviarDaPlanilha() {
const aba = SpreadsheetApp.getActiveSheet();
const linhas = aba.getDataRange().getValues();
const hoje = Utilities.formatDate(new Date(), 'America/Sao_Paulo', 'yyyyMMdd');
for (let i = 1; i < linhas.length; i++) {
const [telefone, texto, status] = linhas[i];
if (status) continue; // ja tratada
const chave = 'planilha-' + hoje + '-' + (i + 1); // estavel por linha e por dia
const r = enviarTexto(String(telefone), String(texto), chave);
aba.getRange(i + 1, 3).setValue(r.status === 202 ? 'aceita' : 'erro ' + r.status);
Utilities.sleep(1100); // 60 mensagens por minuto por canal
}
}| Limit | Value | Source |
|---|---|---|
| Messages per channel | 60 per minute | CRPRO Hub |
| Requests per organization and credential | 120 per minute | CRPRO Hub |
| URL Fetch per day, personal account | 20,000 | |
| URL Fetch per day, Workspace | 100,000 | |
| Time per execution | 6 minutes |
The example's Idempotency-Key combines the date and the row number. If the run dies midway and you run it again the same day, rows already sent but still without a status reuse the same key, and the Hub returns the original response instead of sending a duplicate. If you change a row's text and repeat the same day, the same key with a different body gives 409 IDEMPOTENCY_KEY_REUSED. And remember that free text only works inside the service window; to open a conversation, the route is a template (see the quickstart).
Receiving a webhook with doPost: what works and what does not
Short answer: you can receive the body with doPost, but without verifying the signature and with delivery risks the official documentation does not clarify. For production, receive elsewhere and write to the sheet from there.
function doPost(e) {
const corpo = e.postData.contents; // texto do corpo
const evento = JSON.parse(corpo);
// Os campos documentados de e nao incluem headers:
// nao ha como conferir X-Hub-Signature-256 aqui.
SpreadsheetApp.getActiveSheet().appendRow([evento.id, evento.event, evento.data && evento.data.from]);
return ContentService.createTextOutput('ok');
}{
"id": "6f1f0f6a-3b2c-4d1e-9a8b-0c1d2e3f4a5b",
"event": "message.received",
"channel_id": "9f6a9c1e-2f3d-4a5b-8c7d-1e2f3a4b5c6d",
"occurred_at": "2026-08-21T12:00:00.000Z",
"data": {
"message_id": "wamid.EXEMPLO123",
"from": "5521999999999",
"type": "text",
"text": "Oi, ainda tem em estoque?"
}
}- No signature: anyone who discovers the web app's public URL can write to your spreadsheet.
- Deployment: the documentation describes deploying as Deploy > New deployment > Web app, the test URL ending in /dev, for editors only, and the choice between running as you or as the accessing user. The setting for who can call the app without a Google login is not on the page consulted; check the deployment dialog.
- Responses: the Hub requires HTTPS, does not follow redirects and wants a 2xx within 10 seconds. If the web app answers with a redirect, the Hub treats it as a failure, because it does not follow redirects; the official page does not say how the web app answers external calls, so test before relying on it. Test with POST /api/v1/webhooks/{id}/test and look at the history in GET /api/v1/webhooks/{id}/deliveries.
- Reserved parameters: do not use c or sid as parameter names, since they can cause a 405 error.
If the test fails, the practical route is a small receiver that validates the signature, as described on the webhooks page, and then calls the web app or writes straight to the sheet.
When to leave the spreadsheet
Short answer: when you need reliable receiving, more than a few hundred messages per run, or a history beyond the tab itself. The spreadsheet is a good start, but it is not an event database.
The Hub keeps message history per channel at GET /api/v1/channels/{id}/messages, with cursor pagination and direction and status filters, so you can use Apps Script only to read that history from time to time instead of depending on a webhook. A time-driven trigger that calls this route and appends new rows is an alternative without the doPost risks.
Frequently asked questions
Can I verify the Hub signature in doPost?
There is no way to. The documented doPost event object includes no headers, and the signature arrives in the X-Hub-Signature-256 header. That is why the article recommends an external receiver.
How many messages can Apps Script send per run?
With a 1.1-second pause to respect 60 per minute per channel and the 6-minute limit per execution, about 300. It is an approximate calculation, not a documented value.
Do I need a paid Google plan?
Not for sending. Personal accounts get 20,000 URL Fetch calls per day and Workspace accounts get 100,000, according to the quotas page.
Where do I store the API key?
In Script Properties, not in the sheet. Remember those properties are accessible to all users of the script, so restrict who edits the project.
Fontes
Documentação oficial da Meta, conferida em 31 de outubro de 2026.