Skip to content

green-api/whatsapp-api-client-js-v2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GREEN-API WhatsApp SDK Library v2

A TypeScript/JavaScript SDK for interacting with the GREEN-API WhatsApp gateway.

Installation

npm install @green-api/whatsapp-api-client-js-v2
# or
yarn add @green-api/whatsapp-api-client-js-v2

API

Documentation for the REST API is located at link. The library is a wrapper for the REST API, so the documentation at the link above is also applicable to the library itself.

Authorization

To send a message or perform other GREEN-API methods, the WhatsApp account in the phone application must be in the authorized state. To authorize the instance, go to console and scan the QR code using the WhatsApp application.

Getting Started

To use the SDK, you need to create an instance of the GreenApiClient with your GREEN-API instance credentials:

import { GreenApiClient } from '@green-api/whatsapp-api-client-js-v2';

const client = new GreenApiClient({
    idInstance: 12345,
    apiTokenInstance: 'your-api-token'
});

For Partner API access, use the GreenApiPartnerClient:

import { GreenApiPartnerClient } from '@green-api/whatsapp-api-client-js-v2';

const partnerClient = new GreenApiPartnerClient({
    partnerToken: 'your-partner-token',
    partnerApiUrl: 'https://api.green-api.com' // Optional, defaults to this URL
});

Usage Examples

Sending a Text Message

await client.sendMessage({
    chatId: '[email protected]',
    message: 'Hello from GREEN-API SDK!'
});

Sending a File by URL

await client.sendFileByUrl({
    chatId: '[email protected]',
    file: {
        url: 'https://example.com/file.pdf',
        fileName: 'document.pdf'
    },
    caption: 'Check this file'
});

Creating a Poll

await client.sendPoll({
    chatId: '[email protected]',
    message: 'What\'s your favorite color?',
    options: [
        {optionName: 'Red'},
        {optionName: 'Blue'},
        {optionName: 'Green'}
    ],
    multipleAnswers: false
});

Managing Groups

// Create a group
const group = await client.createGroup({
    groupName: 'My Test Group',
    chatIds: ['[email protected]', '[email protected]']
});

// Add a participant
await client.addGroupParticipant({
    groupId: group.chatId,
    participantChatId: '[email protected]'
});

Receiving Notifications

// Receive notification with 30 sec timeout
const notification = await client.receiveNotification(30);
if (notification) {
    console.log('Received notification:', notification.body.typeWebhook);

    // Process the notification
    if (notification.body.typeWebhook === 'incomingMessageReceived') {
        // Handle incoming message
        console.log('Message:', notification.body.messageData);
    }

    // Delete the notification from queue after processing
    await client.deleteNotification(notification.receiptId);
}

// Download file from a message
const fileData = await client.downloadFile({
    chatId: '[email protected]',
    idMessage: 'MESSAGE_ID_WITH_FILE'
});
console.log('File URL:', fileData.downloadUrl);

Working with WhatsApp Statuses (Beta)

// Send text status
await client.sendTextStatus({
    message: "Hello from GREEN-API SDK!",
    backgroundColor: "#228B22", // Green background
    font: "SERIF",
    participants: ["[email protected]"] // Optional: limit visibility to specific contacts
});

// Send media status
await client.sendMediaStatus({
    urlFile: "https://example.com/image.jpg",
    fileName: "image.jpg",
    caption: "Check out this view!",
    participants: ["[email protected]"]
});

// Get status statistics
const stats = await client.getStatusStatistic({
    idMessage: "BAE5F4886F6F2D05"
});
console.log(`Status was viewed by ${stats.length} contacts`);

// Get incoming statuses from contacts
const statuses = await client.getIncomingStatuses({minutes: 60}); // Last hour
statuses.forEach(status => {
    console.log(`Status from ${status.senderName} at ${new Date(status.timestamp * 1000)}`);
});

Partner API (Instance Management)

// Get all instances
const instances = await partnerClient.getInstances();
console.log(`Total instances: ${instances.length}`);
console.log(`Active instances: ${instances.filter(i => !i.deleted).length}`);

// Create a new instance
const instance = await partnerClient.createInstance({
    name: "Marketing Campaign",
    incomingWebhook: "yes",
    outgoingWebhook: "yes",
    delaySendMessagesMilliseconds: 3000
});
console.log(`Created instance with ID: ${instance.idInstance}`);
console.log(`API Token: ${instance.apiTokenInstance}`);

// Delete an instance
const result = await partnerClient.deleteInstanceAccount({
    idInstance: instance.idInstance
});
if (result.deleteInstanceAccount) {
    console.log("Instance successfully deleted");
}

Editing and Deleting Messages

// Edit a message
const editResult = await client.editMessage({
    chatId: '[email protected]',
    idMessage: 'BAE5367237E13A87',
    message: 'This is the edited message text'
});
console.log('Edited message ID:', editResult.idMessage);

// Delete a message for everyone
await client.deleteMessage({
    chatId: '[email protected]',
    idMessage: 'BAE5F4886F6F2D05'
});

// Delete a message only for sender
await client.deleteMessage({
    chatId: '[email protected]',
    idMessage: 'BAE5F4886F6F2D05',
    onlySenderDelete: true
});

SDK methods

The SDK provides the following groups of methods:

  1. Message Sending Methods

    • sendMessage
    • sendFileByUrl
    • sendFileByUpload
    • sendPoll
    • forwardMessages
    • sendLocation
    • sendContact
    • uploadFile
  2. Account Management Methods

    • reboot
    • logout
    • getStateInstance
    • getQR
    • getSettings
    • setSettings
    • getWaSettings
    • setProfilePicture
    • getAuthorizationCode
  3. Message Queue Methods

    • showMessagesQueue
    • clearMessagesQueue
  4. Service Methods

    • readChat
    • checkWhatsapp
    • getAvatar
    • getContacts
    • getContactInfo
    • archiveChat
    • unarchiveChat
    • setDisappearingChat
    • editMessage
    • deleteMessage
  5. Group Management Methods

    • createGroup
    • updateGroupName
    • getGroupData
    • addGroupParticipant
    • removeGroupParticipant
    • setGroupAdmin
    • removeAdmin
    • setGroupPicture
    • leaveGroup
  6. Journal Methods

    • getMessage
    • getChatHistory
    • lastIncomingMessages
    • lastOutgoingMessages
  7. Message Receiving Methods

    • receiveNotification
    • deleteNotification
    • downloadFile
  8. Status Methods (Beta)

    • sendTextStatus
    • sendVoiceStatus
    • sendMediaStatus
    • deleteStatus
    • getStatusStatistic
    • getIncomingStatuses
    • getOutgoingStatuses
  9. Partner API Methods

    • getInstances
    • createInstance
    • deleteInstanceAccount

License

MIT

About

whatsapp-api-client-js-v2

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published