Skip to content
Nigora Zap API

    Sign in Create account

    Send a WhatsApp message with Node.js

    By the end, you will have a Node.js script that sends a text message through the Nigora Zap API and prints the response. No extra packages: fetch already ships with Node.

    • A connected instance. If you have not connected one yet, see Connection.
    • The instance ID and token, as in Getting started.
    • Node.js 18 or newer. Check with node --version.

    1. Keep the credentials in the environment

    Section titled “1. Keep the credentials in the environment”

    Do not write the token in the code. Read it from environment variables:

    Terminal window
    export NIGORA_INSTANCIA_ID="cole-aqui-o-id-da-instancia"
    export NIGORA_TOKEN="cole-aqui-o-token"

    Save it as enviar.mjs. The .mjs extension lets you use await directly in the file:

    const { NIGORA_INSTANCIA_ID: instanciaId, NIGORA_TOKEN: token } = process.env
    if (!instanciaId || !token) {
    console.error('Defina NIGORA_INSTANCIA_ID e NIGORA_TOKEN.')
    process.exit(1)
    }
    const resposta = await fetch(`https://api.zap.nigora.com.br/v1/instances/${instanciaId}/messages/text`, {
    method: 'POST',
    headers: {
    Authorization: `Bearer ${token}`,
    'Content-Type': 'application/json',
    },
    body: JSON.stringify({
    to: '5511999999999',
    text: 'Olá! Esta mensagem saiu pela API.',
    }),
    signal: AbortSignal.timeout(15000),
    })
    console.log('Status HTTP:', resposta.status)
    console.log(await resposta.text())
    process.exit(resposta.ok ? 0 : 1)

    Replace 5511999999999 with the phone of the person who will receive the message, with country and area codes. The API ignores everything that is not a digit and adds the 55 when the number has 10 or 11 digits.

    Terminal window
    node enviar.mjs

    Expected result:

    Status HTTP: 202
    {"id":"…","status":"queued"}

    The 202 means the message entered the queue. It has not been delivered yet. Keep the id: it is how you match the response to the message.sent and message.status events, which arrive by webhook.

    Status Code What to do
    400 invalid_phone Fix the phone: it must have 12 or 13 digits including the 55
    401 invalid_token Check the token and the instance ID
    402 instance_suspended The instance is suspended or has a pending payment
    422 contact_opted_out The contact asked not to receive messages

    If the network fails or the request times out, fetch throws an exception and Node prints it in the terminal.