A lot of WhatsApp happens in groups, like a course cohort or the crew for an event. Setting them up by hand on a phone gets old fast.
This tutorial uses the HelloHop API from Node.js to:
- create a WhatsApp group,
- add people and read the result for each person,
- post a message to the group.
The script uses the fetch built into Node 20 and later. There is nothing to install.
HelloHop is an independent WhatsApp API, not affiliated with Meta or WhatsApp.
Before you start
- A HelloHop channel with a linked WhatsApp number. The Quickstart covers linking.
- The channel's API key (the channel token from the console) and its channel ID.
- Node.js 20 or later:
node --version. - The phone numbers of a few people who expect to be added. Use your own second phone for a first run.
export HELLOHOP_API_KEY='<CHANNEL_TOKEN>' export HELLOHOP_CHANNEL_ID='<CHANNEL_ID>' export PARTICIPANTS='66XXXXXXXXX,66YYYYYYYYY' Phone numbers go in full international form, digits only, with the country code. A WhatsApp JID such as 66XXXXXXXXX@s.whatsapp.net also works.
The API in four calls
Every route here sits under https://api.hellohop.app/v1 and takes Authorization: Bearer <CHANNEL_TOKEN>. {name} in the paths is your channel ID.
| Step | Method and path | Body |
|---|---|---|
| Create a group | POST /sessions/{name}/groups | {"subject": "...", "participants": [...]} (only subject is required) |
| List groups | GET /sessions/{name}/groups | none |
| Add people | POST /sessions/{name}/groups/{group}/participants | {"action": "add", "participants": [...]} |
| Send a text | POST /sessions/{name}/messages/text | {"to": "<group JID>", "text": "..."} |
A group is addressed by its JID, which looks like 120363000000000001@g.us. Bodies are strict: a field a route does not take is refused with a 400, so send only the fields shown. Full reference: Groups and Messages.
The script
Save this as groups.mjs:
// groups.mjs: create a WhatsApp group, add people, post a message. Node 20+. const API_BASE = process.env.HELLOHOP_API_BASE ?? 'https://api.hellohop.app/v1'; const API_KEY = process.env.HELLOHOP_API_KEY; // your channel token const CHANNEL = process.env.HELLOHOP_CHANNEL_ID; // the channel ID from the console const PEOPLE = (process.env.PARTICIPANTS ?? '').split(',').map((s) => s.trim()).filter(Boolean); if (!API_KEY || !CHANNEL) { console.error('Set HELLOHOP_API_KEY and HELLOHOP_CHANNEL_ID first.'); process.exit(1); } class HelloHopError extends Error { constructor(method, path, status, body) { super(`${method} ${path} answered ${status} ${body.code ?? ''}: ${body.error ?? ''}`); this.status = status; this.code = body.code; this.body = body; } } async function hellohop(method, path, body) { const res = await fetch(`${API_BASE}/sessions/${CHANNEL}${path}`, { method, headers: { Authorization: `Bearer ${API_KEY}`, ...(body ? { 'Content-Type': 'application/json' } : {}), }, body: body ? JSON.stringify(body) : undefined, }); const data = await res.json().catch(() => ({})); if (!res.ok) throw new HelloHopError(method, path, res.status, data); return data; } // 1. Create the group. Only `subject` is required (1 to 25 characters). async function createGroup(subject) { try { return await hellohop('POST', '/groups', { subject }); } catch (err) { // A 5xx on a write does not prove nothing happened. Look before you retry. if (err.status >= 500) { const { groups } = await hellohop('GET', '/groups'); const existing = groups.find((g) => g.name === subject); if (existing) return existing; } throw err; } } // 2. Add people. Each person gets their own result, so check every entry. async function addPeople(groupJid, participants) { const { results } = await hellohop('POST', `/groups/${groupJid}/participants`, { action: 'add', participants, }); for (const r of results) { if (r.add_request) console.log(`${r.jid}: not added; WhatsApp answered with an add request (add_request)`); else if (r.status === 'ok') console.log(`added ${r.jid}`); else if (r.status === 'unknown') console.log(`${r.jid}: outcome unknown, read the group before retrying`); else console.log(`${r.jid}: ${r.status} (${r.error ?? 'no detail'})`); } return results; } // 3. Post to the group: a group JID works as `to` on the normal text route. async function postToGroup(groupJid, text) { return hellohop('POST', '/messages/text', { to: groupJid, text }); } const group = await createGroup('Launch crew'); console.log(`created ${group.jid} (${group.name})`); if (PEOPLE.length > 0) await addPeople(group.jid, PEOPLE); const sent = await postToGroup(group.jid, 'Welcome, everyone. This group was set up from Node.js.'); // 200: status "server_ack". 202: the channel queued the send (status "queued"). console.log(`message ${sent.id} status ${sent.status}${sent.position ? `, queue position ${sent.position}` : ''}`); Run it:
node groups.mjs The output looks like this:
created 120363000000000001@g.us (Launch crew) added 66XXXXXXXXX 66YYYYYYYYY: error_403 (...) message 3EB0C767D26A1D8E2A41 status server_ack HELLOHOP_API_BASE is optional. It defaults to the real API, and you can point it at a local mock to run the script without touching WhatsApp, which is how I checked the request shapes.
Walking through it
The request helper
hellohop() adds the bearer header, sends JSON when there is a body, and throws on any status outside 2xx. Errors come back as JSON with a stable code and a human-readable error. Branch on code, never on the error text. The Errors page lists every code.
The group JID goes into the path as is. The @ is a legal character in a path segment, and the docs' own examples send it unencoded.
Creating the group
Only subject is required, 1 to 25 characters. You can pass participants on create too (up to 50). The script adds people in a separate call so you can see each person's result.
A successful create answers 201 with the group object: jid, name, participant_count and more.
The catch block handles one specific case. For writes, a 500, 502 or 503 does not prove that nothing happened: the group may exist even though you never saw the answer. So the script lists the groups and looks for the subject before giving up. The docs say it outright: never re-create a group without reading or listing first. If you create many groups with the same subject, use a unique subject per run (or keep the JIDs you create) so the lookup cannot pick the wrong one.
Adding people
The participants route takes an action (add, remove, promote or demote) and 1 to 50 participants. Name each person once; a repeat is refused.
The answer has one entry per person, so one failure does not sink the others:
{ "results": [ { "jid": "66XXXXXXXXX", "status": "ok", "matched_by": "a" }, { "jid": "66YYYYYYYYY", "status": "error_403", "error": "..." } ] } -
ok: added. -
error_<code>: the code WhatsApp returned for that person. 403 means not allowed or the person's privacy settings block the add, 408 means they left recently, 409 means they are already in the group, 401 means they blocked your number, 404 means the number is not on WhatsApp. -
unknown: HelloHop could not tell which person an answer belonged to. The change may have applied, so read the group (GET /sessions/{name}/groups/{group}) before you retry that person. - An entry can also carry
add_requestwhen WhatsApp answered with an add request for that person instead of adding them. Branch on the key, not on the status.
Each person you add counts as one group add, and so does creating the group. Remove, promote and demote do not. On the sandbox channel, a couple of runs of this script can use up the day's group-add allowance.
Posting to the group
Sending to a group uses the same text route as a direct message. Put the group JID in to.
- 200 with
status: "server_ack": WhatsApp's servers took the message. Delivery and read receipts arrive later asmessage.statuswebhook events. - 202 with
status: "queued"and aposition: the channel holds sends in a queue, for example outside reply hours you have set. The send has been accepted but has not gone out yet.
Go easy on group adds
Adding a lot of people to groups from a new number is a common way to get that number restricted.
- Add people who expect it, such as someone who signed up or asked to join.
- Keep the volume low on a new number and raise it slowly.
- The free sandbox channel has a small daily allowance for group adds, and HelloHop's optional warm-up protection caps them per day by the age of your number. Over an allowance you get a 429 with a
codethat says which one. See Limits. - A 429
whatsapp_rate_limitedmeans WhatsApp itself is throttling the number. Back off and try later.
For people who are not expecting an add, the group's invite link is a gentler option: GET /sessions/{name}/groups/{group}/invite-link returns {"invite_link": "https://chat.whatsapp.com/..."}, which you can share and let them join on their own.
Where to go next
- Change the subject, description or settings (who can send, who can edit info) with the
PUTroutes on the Groups page. - Listen for joins and leaves with the
group.participantswebhook event. See Webhook events. - Before you put this behind a button, read Errors for which codes are safe to retry.
